Files
gitea/ts/gitea.classes.organization.ts

116 lines
3.5 KiB
TypeScript

import type { GiteaClient } from './gitea.classes.giteaclient.js';
import type { IGiteaOrganization, IGiteaSecret, IListOptions } from './gitea.interfaces.js';
import { GiteaRepository } from './gitea.classes.repository.js';
import { GiteaSecret } from './gitea.classes.secret.js';
import { autoPaginate } from './gitea.helpers.js';
export class GiteaOrganization {
// Raw data
public readonly id: number;
public readonly name: string;
public readonly fullName: string;
public readonly description: string;
public readonly visibility: string;
public readonly repoCount: number;
public readonly avatarUrl: string;
/** @internal */
constructor(
private client: GiteaClient,
raw: IGiteaOrganization,
) {
this.id = raw.id;
this.name = raw.name || '';
this.fullName = raw.full_name || this.name;
this.description = raw.description || '';
this.visibility = raw.visibility || 'public';
this.repoCount = raw.repo_count || 0;
this.avatarUrl = raw.avatar_url || '';
}
// ---------------------------------------------------------------------------
// Repos
// ---------------------------------------------------------------------------
async getRepos(opts?: IListOptions): Promise<GiteaRepository[]> {
return autoPaginate(
(page, perPage) => this.client.requestGetOrgRepos(this.name, { ...opts, page, perPage }),
opts,
).then(repos => repos.map(r => new GiteaRepository(this.client, r)));
}
// ---------------------------------------------------------------------------
// Secrets
// ---------------------------------------------------------------------------
async getSecrets(): Promise<GiteaSecret[]> {
const secrets = await this.client.requestGetOrgSecrets(this.name);
return secrets.map(s => new GiteaSecret(s));
}
async setSecret(key: string, value: string): Promise<void> {
await this.client.requestSetOrgSecret(this.name, key, value);
}
async deleteSecret(key: string): Promise<void> {
await this.client.requestDeleteOrgSecret(this.name, key);
}
// ---------------------------------------------------------------------------
// Mutation
// ---------------------------------------------------------------------------
/**
* Update organization properties (description, visibility, etc.)
*/
async update(data: {
description?: string;
visibility?: string;
fullName?: string;
}): Promise<void> {
await this.client.requestPatchOrg(this.name, {
description: data.description,
visibility: data.visibility,
full_name: data.fullName,
});
}
/**
* Upload an avatar image for this organization.
* @param imageBase64 - Base64-encoded image data
*/
async setAvatar(imageBase64: string): Promise<void> {
await this.client.requestPostOrgAvatar(this.name, imageBase64);
}
/**
* Remove the organization's avatar.
*/
async deleteAvatar(): Promise<void> {
await this.client.requestDeleteOrgAvatar(this.name);
}
/**
* Delete this organization.
*/
async delete(): Promise<void> {
await this.client.requestDeleteOrg(this.name);
}
// ---------------------------------------------------------------------------
// Serialization
// ---------------------------------------------------------------------------
toJSON(): IGiteaOrganization {
return {
id: this.id,
name: this.name,
full_name: this.fullName,
description: this.description,
visibility: this.visibility,
repo_count: this.repoCount,
avatar_url: this.avatarUrl,
};
}
}