import ApiClient, { encodeQueryString } from './client'; import { Agent, Cron, Org, OrgPermissions, Pipeline, PipelineConfig, PipelineFeed, PipelineLog, PullRequest, QueueInfo, Registry, Repo, RepoPermissions, RepoSettings, Secret, User, } from './types'; type RepoListOptions = { all?: boolean; }; // PipelineOptions is the data for creating a new pipeline type PipelineOptions = { branch: string; variables: Record; }; type DeploymentOptions = { id: string; environment: 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; } lookupRepo(owner: string, name: string): Promise { return this._get(`/api/repos/lookup/${owner}/${name}`) as Promise; } getRepo(repoId: number): Promise { return this._get(`/api/repos/${repoId}`) as Promise; } getRepoPermissions(repoId: number): Promise { return this._get(`/api/repos/${repoId}/permissions`) as Promise; } getRepoBranches(repoId: number, page: number): Promise { return this._get(`/api/repos/${repoId}/branches?page=${page}`) as Promise; } getRepoPullRequests(repoId: number, page: number): Promise { return this._get(`/api/repos/${repoId}/pull_requests?page=${page}`) as Promise; } activateRepo(forgeRemoteId: string): Promise { return this._post(`/api/repos?forge_remote_id=${forgeRemoteId}`) as Promise; } updateRepo(repoId: number, repoSettings: RepoSettings): Promise { return this._patch(`/api/repos/${repoId}`, repoSettings); } deleteRepo(repoId: number, remove = true): Promise { const query = encodeQueryString({ remove }); return this._delete(`/api/repos/${repoId}?${query}`); } repairRepo(repoId: number): Promise { return this._post(`/api/repos/${repoId}/repair`); } createPipeline(repoId: number, options: PipelineOptions): Promise { return this._post(`/api/repos/${repoId}/pipelines`, options) as Promise; } // Deploy triggers a deployment for an existing pipeline using the // specified target environment. deployPipeline(repoId: number, pipelineNumber: string, options: DeploymentOptions): Promise { const vars = { ...options.variables, event: 'deployment', deploy_to: options.environment, }; const query = encodeQueryString(vars); return this._post(`/api/repos/${repoId}/pipelines/${pipelineNumber}?${query}`) as Promise; } getPipelineList(repoId: number, opts?: Record): Promise { const query = encodeQueryString(opts); return this._get(`/api/repos/${repoId}/pipelines?${query}`) as Promise; } getPipeline(repoId: number, pipelineNumber: number | 'latest'): Promise { return this._get(`/api/repos/${repoId}/pipelines/${pipelineNumber}`) as Promise; } getPipelineConfig(repoId: number, pipelineNumber: number): Promise { return this._get(`/api/repos/${repoId}/pipelines/${pipelineNumber}/config`) as Promise; } getPipelineFeed(opts?: Record): Promise { const query = encodeQueryString(opts); return this._get(`/api/user/feed?${query}`) as Promise; } cancelPipeline(repoId: number, pipelineNumber: number): Promise { return this._post(`/api/repos/${repoId}/pipelines/${pipelineNumber}/cancel`); } approvePipeline(repoId: number, pipelineNumber: string): Promise { return this._post(`/api/repos/${repoId}/pipelines/${pipelineNumber}/approve`); } declinePipeline(repoId: number, pipelineNumber: string): Promise { return this._post(`/api/repos/${repoId}/pipelines/${pipelineNumber}/decline`); } restartPipeline( repoId: number, pipeline: string, opts?: Record, ): Promise { const query = encodeQueryString(opts); return this._post(`/api/repos/${repoId}/pipelines/${pipeline}?${query}`) as Promise; } getLogs(repoId: number, pipeline: number, step: number): Promise { return this._get(`/api/repos/${repoId}/logs/${pipeline}/${step}`) as Promise; } getSecretList(repoId: number, page: number): Promise { return this._get(`/api/repos/${repoId}/secrets?page=${page}`) as Promise; } createSecret(repoId: number, secret: Partial): Promise { return this._post(`/api/repos/${repoId}/secrets`, secret); } updateSecret(repoId: number, secret: Partial): Promise { const secretName = encodeURIComponent(secret.name ?? ''); return this._patch(`/api/repos/${repoId}/secrets/${secretName}`, secret); } deleteSecret(repoId: number, secretName: string): Promise { const name = encodeURIComponent(secretName); return this._delete(`/api/repos/${repoId}/secrets/${name}`); } getRegistryList(repoId: number, page: number): Promise { return this._get(`/api/repos/${repoId}/registry?page=${page}`) as Promise; } createRegistry(repoId: number, registry: Partial): Promise { return this._post(`/api/repos/${repoId}/registry`, registry); } updateRegistry(repoId: number, registry: Partial): Promise { return this._patch(`/api/repos/${repoId}/registry/${registry.address}`, registry); } deleteRegistry(repoId: number, registryAddress: string): Promise { return this._delete(`/api/repos/${repoId}/registry/${registryAddress}`); } getCronList(repoId: number, page: number): Promise { return this._get(`/api/repos/${repoId}/cron?page=${page}`) as Promise; } createCron(repoId: number, cron: Partial): Promise { return this._post(`/api/repos/${repoId}/cron`, cron); } updateCron(repoId: number, cron: Partial): Promise { return this._patch(`/api/repos/${repoId}/cron/${cron.id}`, cron); } deleteCron(repoId: number, cronId: number): Promise { return this._delete(`/api/repos/${repoId}/cron/${cronId}`); } runCron(repoId: number, cronId: number): Promise { return this._post(`/api/repos/${repoId}/cron/${cronId}`) as Promise; } getOrg(orgId: number): Promise { return this._get(`/api/orgs/${orgId}`) as Promise; } lookupOrg(name: string): Promise { return this._get(`/api/orgs/lookup/${name}`) as Promise; } getOrgPermissions(orgId: number): Promise { return this._get(`/api/orgs/${orgId}/permissions`) as Promise; } getOrgSecretList(orgId: number, page: number): Promise { return this._get(`/api/orgs/${orgId}/secrets?page=${page}`) as Promise; } createOrgSecret(orgId: number, secret: Partial): Promise { return this._post(`/api/orgs/${orgId}/secrets`, secret); } updateOrgSecret(orgId: number, secret: Partial): Promise { const secretName = encodeURIComponent(secret.name ?? ''); return this._patch(`/api/orgs/${orgId}/secrets/${secretName}`, secret); } deleteOrgSecret(orgId: number, secretName: string): Promise { const name = encodeURIComponent(secretName); return this._delete(`/api/orgs/${orgId}/secrets/${name}`); } getGlobalSecretList(page: number): Promise { return this._get(`/api/secrets?page=${page}`) as Promise; } createGlobalSecret(secret: Partial): Promise { return this._post(`/api/secrets`, secret); } updateGlobalSecret(secret: Partial): Promise { const secretName = encodeURIComponent(secret.name ?? ''); return this._patch(`/api/secrets/${secretName}`, secret); } deleteGlobalSecret(secretName: string): Promise { const name = encodeURIComponent(secretName); return this._delete(`/api/secrets/${name}`); } getSelf(): Promise { return this._get('/api/user'); } getToken(): Promise { return this._post('/api/user/token') as Promise; } getAgents(page: number): Promise { return this._get(`/api/agents?page=${page}`) as Promise; } getAgent(agentId: Agent['id']): Promise { return this._get(`/api/agents/${agentId}`) as Promise; } createAgent(agent: Partial): Promise { return this._post('/api/agents', agent) as Promise; } updateAgent(agent: Partial): Promise { return this._patch(`/api/agents/${agent.id}`, agent); } deleteAgent(agent: Agent): Promise { return this._delete(`/api/agents/${agent.id}`); } getQueueInfo(): Promise { return this._get('/api/queue/info') as Promise; } pauseQueue(): Promise { return this._post('/api/queue/pause'); } resumeQueue(): Promise { return this._post('/api/queue/resume'); } getUsers(page: number): Promise { return this._get(`/api/users?page=${page}`) as Promise; } getUser(username: string): Promise { return this._get(`/api/users/${username}`) as Promise; } createUser(user: Partial): Promise { return this._post('/api/users', user) as Promise; } updateUser(user: Partial): Promise { return this._patch(`/api/users/${user.login}`, user); } deleteUser(user: User): Promise { return this._delete(`/api/users/${user.login}`); } resetToken(): Promise { return this._delete('/api/user/token') as Promise; } getOrgs(page: number): Promise { return this._get(`/api/orgs?page=${page}`) as Promise; } deleteOrg(org: Org): Promise { return this._delete(`/api/orgs/${org.id}`); } getAllRepos(page: number): Promise { return this._get(`/api/repos?page=${page}`) as Promise; } repairAllRepos(): Promise { return this._post(`/api/repos/repair`) as Promise; } // eslint-disable-next-line promise/prefer-await-to-callbacks on(callback: (data: { pipeline?: Pipeline; repo?: Repo }) => void): EventSource { return this._subscribe('/api/stream/events', callback, { reconnect: true, }); } streamLogs( repoId: number, pipeline: number, step: number, // eslint-disable-next-line promise/prefer-await-to-callbacks callback: (data: PipelineLog) => void, ): EventSource { return this._subscribe(`/api/stream/logs/${repoId}/${pipeline}/${step}`, callback, { reconnect: true, }); } }