import ApiClient, { encodeQueryString } from './client'; import { OrgPermissions, Pipeline, PipelineConfig, PipelineFeed, PipelineLog, PipelineStep, Registry, Repo, RepoPermissions, RepoSettings, Secret, } from './types'; import { Cron } from './types/cron'; type RepoListOptions = { all?: boolean; flush?: boolean; }; type PipelineOptions = { branch: string; variables: Record; }; export default class WoodpeckerClient extends ApiClient { getRepoList(opts?: RepoListOptions): Promise { const query = encodeQueryString(opts); return this._get(`/api/user/repos?${query}`) as Promise; } getRepo(owner: string, repo: string): Promise { return this._get(`/api/repos/${owner}/${repo}`) as Promise; } getRepoPermissions(owner: string, repo: string): Promise { return this._get(`/api/repos/${owner}/${repo}/permissions`) as Promise; } getRepoBranches(owner: string, repo: string): Promise { return this._get(`/api/repos/${owner}/${repo}/branches`) as Promise; } activateRepo(owner: string, repo: string): Promise { return this._post(`/api/repos/${owner}/${repo}`); } updateRepo(owner: string, repo: string, repoSettings: RepoSettings): Promise { return this._patch(`/api/repos/${owner}/${repo}`, repoSettings); } deleteRepo(owner: string, repo: string, remove = true): Promise { const query = encodeQueryString({ remove }); return this._delete(`/api/repos/${owner}/${repo}?${query}`); } repairRepo(owner: string, repo: string): Promise { return this._post(`/api/repos/${owner}/${repo}/repair`); } createPipeline(owner: string, repo: string, options: PipelineOptions): Promise { return this._post(`/api/repos/${owner}/${repo}/pipelines`, options) as Promise; } getPipelineList(owner: string, repo: string, opts?: Record): Promise { const query = encodeQueryString(opts); return this._get(`/api/repos/${owner}/${repo}/pipelines?${query}`) as Promise; } getPipeline(owner: string, repo: string, number: number | 'latest'): Promise { return this._get(`/api/repos/${owner}/${repo}/pipelines/${number}`) as Promise; } getPipelineConfig(owner: string, repo: string, number: number): Promise { return this._get(`/api/repos/${owner}/${repo}/pipelines/${number}/config`) as Promise; } getPipelineFeed(opts?: Record): Promise { const query = encodeQueryString(opts); return this._get(`/api/user/feed?${query}`) as Promise; } cancelPipeline(owner: string, repo: string, number: number, ppid: number): Promise { return this._delete(`/api/repos/${owner}/${repo}/pipelines/${number}/${ppid}`); } approvePipeline(owner: string, repo: string, pipeline: string): Promise { return this._post(`/api/repos/${owner}/${repo}/pipelines/${pipeline}/approve`); } declinePipeline(owner: string, repo: string, pipeline: string): Promise { return this._post(`/api/repos/${owner}/${repo}/pipelines/${pipeline}/decline`); } restartPipeline( owner: string, repo: string, pipeline: string, opts?: Record, ): Promise { const query = encodeQueryString(opts); return this._post(`/api/repos/${owner}/${repo}/pipelines/${pipeline}?${query}`); } getLogs(owner: string, repo: string, pipeline: number, step: number): Promise { return this._get(`/api/repos/${owner}/${repo}/logs/${pipeline}/${step}`) as Promise; } getArtifact(owner: string, repo: string, pipeline: string, step: string, file: string): Promise { return this._get(`/api/repos/${owner}/${repo}/files/${pipeline}/${step}/${file}?raw=true`); } getArtifactList(owner: string, repo: string, pipeline: string): Promise { return this._get(`/api/repos/${owner}/${repo}/files/${pipeline}`); } getSecretList(owner: string, repo: string): Promise { return this._get(`/api/repos/${owner}/${repo}/secrets`) as Promise; } createSecret(owner: string, repo: string, secret: Partial): Promise { return this._post(`/api/repos/${owner}/${repo}/secrets`, secret); } updateSecret(owner: string, repo: string, secret: Partial): Promise { return this._patch(`/api/repos/${owner}/${repo}/secrets/${secret.name}`, secret); } deleteSecret(owner: string, repo: string, secretName: string): Promise { return this._delete(`/api/repos/${owner}/${repo}/secrets/${secretName}`); } getRegistryList(owner: string, repo: string): Promise { return this._get(`/api/repos/${owner}/${repo}/registry`) as Promise; } createRegistry(owner: string, repo: string, registry: Partial): Promise { return this._post(`/api/repos/${owner}/${repo}/registry`, registry); } updateRegistry(owner: string, repo: string, registry: Partial): Promise { return this._patch(`/api/repos/${owner}/${repo}/registry/${registry.address}`, registry); } deleteRegistry(owner: string, repo: string, registryAddress: string): Promise { return this._delete(`/api/repos/${owner}/${repo}/registry/${registryAddress}`); } getCronList(owner: string, repo: string): Promise { return this._get(`/api/repos/${owner}/${repo}/cron`) as Promise; } createCron(owner: string, repo: string, cron: Partial): Promise { return this._post(`/api/repos/${owner}/${repo}/cron`, cron); } updateCron(owner: string, repo: string, cron: Partial): Promise { return this._patch(`/api/repos/${owner}/${repo}/cron/${cron.id}`, cron); } deleteCron(owner: string, repo: string, cronId: number): Promise { return this._delete(`/api/repos/${owner}/${repo}/cron/${cronId}`); } runCron(owner: string, repo: string, cronId: number): Promise { return this._post(`/api/repos/${owner}/${repo}/cron/${cronId}`) as Promise; } getOrgPermissions(owner: string): Promise { return this._get(`/api/orgs/${owner}/permissions`) as Promise; } getOrgSecretList(owner: string): Promise { return this._get(`/api/orgs/${owner}/secrets`) as Promise; } createOrgSecret(owner: string, secret: Partial): Promise { return this._post(`/api/orgs/${owner}/secrets`, secret); } updateOrgSecret(owner: string, secret: Partial): Promise { return this._patch(`/api/orgs/${owner}/secrets/${secret.name}`, secret); } deleteOrgSecret(owner: string, secretName: string): Promise { return this._delete(`/api/orgs/${owner}/secrets/${secretName}`); } getGlobalSecretList(): Promise { return this._get(`/api/secrets`) as Promise; } createGlobalSecret(secret: Partial): Promise { return this._post(`/api/secrets`, secret); } updateGlobalSecret(secret: Partial): Promise { return this._patch(`/api/secrets/${secret.name}`, secret); } deleteGlobalSecret(secretName: string): Promise { return this._delete(`/api/secrets/${secretName}`); } getSelf(): Promise { return this._get('/api/user'); } getToken(): Promise { return this._post('/api/user/token') as Promise; } // eslint-disable-next-line promise/prefer-await-to-callbacks on(callback: (data: { pipeline?: Pipeline; repo?: Repo; step?: PipelineStep }) => void): EventSource { return this._subscribe('/stream/events', callback, { reconnect: true, }); } streamLogs( owner: string, repo: string, pipeline: number, step: number, // eslint-disable-next-line promise/prefer-await-to-callbacks callback: (data: PipelineLog) => void, ): EventSource { return this._subscribe(`/stream/logs/${owner}/${repo}/${pipeline}/${step}`, callback, { reconnect: true, }); } }