2021-11-03 16:40:31 +00:00
|
|
|
import ApiClient, { encodeQueryString } from './client';
|
2022-01-09 02:59:45 +00:00
|
|
|
import {
|
2023-01-28 13:13:04 +00:00
|
|
|
Agent,
|
|
|
|
Cron,
|
2022-08-14 11:48:53 +00:00
|
|
|
OrgPermissions,
|
2022-10-18 01:24:12 +00:00
|
|
|
Pipeline,
|
|
|
|
PipelineConfig,
|
|
|
|
PipelineFeed,
|
|
|
|
PipelineLog,
|
2022-10-28 15:38:53 +00:00
|
|
|
PipelineStep,
|
2022-01-09 02:59:45 +00:00
|
|
|
Registry,
|
|
|
|
Repo,
|
|
|
|
RepoPermissions,
|
|
|
|
RepoSettings,
|
|
|
|
Secret,
|
|
|
|
} from './types';
|
2021-11-03 16:40:31 +00:00
|
|
|
|
|
|
|
type RepoListOptions = {
|
|
|
|
all?: boolean;
|
|
|
|
flush?: boolean;
|
|
|
|
};
|
2022-09-27 09:05:00 +00:00
|
|
|
|
2022-10-22 13:54:43 +00:00
|
|
|
type PipelineOptions = {
|
2022-09-27 09:05:00 +00:00
|
|
|
branch: string;
|
|
|
|
variables: Record<string, string>;
|
|
|
|
};
|
2022-12-01 20:41:36 +00:00
|
|
|
|
|
|
|
type DeploymentOptions = {
|
|
|
|
id: string;
|
|
|
|
environment: string;
|
|
|
|
variables: Record<string, string>;
|
|
|
|
};
|
|
|
|
|
2021-11-03 16:40:31 +00:00
|
|
|
export default class WoodpeckerClient extends ApiClient {
|
|
|
|
getRepoList(opts?: RepoListOptions): Promise<Repo[]> {
|
|
|
|
const query = encodeQueryString(opts);
|
|
|
|
return this._get(`/api/user/repos?${query}`) as Promise<Repo[]>;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRepo(owner: string, repo: string): Promise<Repo> {
|
|
|
|
return this._get(`/api/repos/${owner}/${repo}`) as Promise<Repo>;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRepoPermissions(owner: string, repo: string): Promise<RepoPermissions> {
|
|
|
|
return this._get(`/api/repos/${owner}/${repo}/permissions`) as Promise<RepoPermissions>;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRepoBranches(owner: string, repo: string): Promise<string[]> {
|
|
|
|
return this._get(`/api/repos/${owner}/${repo}/branches`) as Promise<string[]>;
|
|
|
|
}
|
|
|
|
|
|
|
|
activateRepo(owner: string, repo: string): Promise<unknown> {
|
|
|
|
return this._post(`/api/repos/${owner}/${repo}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRepo(owner: string, repo: string, repoSettings: RepoSettings): Promise<unknown> {
|
|
|
|
return this._patch(`/api/repos/${owner}/${repo}`, repoSettings);
|
|
|
|
}
|
|
|
|
|
2021-12-11 15:03:14 +00:00
|
|
|
deleteRepo(owner: string, repo: string, remove = true): Promise<unknown> {
|
|
|
|
const query = encodeQueryString({ remove });
|
|
|
|
return this._delete(`/api/repos/${owner}/${repo}?${query}`);
|
2021-11-03 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
repairRepo(owner: string, repo: string): Promise<unknown> {
|
|
|
|
return this._post(`/api/repos/${owner}/${repo}/repair`);
|
|
|
|
}
|
|
|
|
|
2022-10-22 13:54:43 +00:00
|
|
|
createPipeline(owner: string, repo: string, options: PipelineOptions): Promise<Pipeline> {
|
2022-10-21 15:39:19 +00:00
|
|
|
return this._post(`/api/repos/${owner}/${repo}/pipelines`, options) as Promise<Pipeline>;
|
2021-11-03 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 20:41:36 +00:00
|
|
|
// Deploy triggers a deployment for an existing pipeline using the
|
|
|
|
// specified target environment.
|
2023-01-15 15:33:12 +00:00
|
|
|
deployPipeline(owner: string, repo: string, pipelineNumber: number, options: DeploymentOptions): Promise<Pipeline> {
|
2022-12-01 20:41:36 +00:00
|
|
|
const vars = {
|
|
|
|
...options.variables,
|
|
|
|
event: 'deployment',
|
|
|
|
deploy_to: options.environment,
|
|
|
|
};
|
|
|
|
const query = encodeQueryString(vars);
|
2023-01-15 15:33:12 +00:00
|
|
|
return this._post(`/api/repos/${owner}/${repo}/pipelines/${pipelineNumber}?${query}`) as Promise<Pipeline>;
|
2022-12-01 20:41:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
getPipelineList(owner: string, repo: string, opts?: Record<string, string | number | boolean>): Promise<Pipeline[]> {
|
|
|
|
const query = encodeQueryString(opts);
|
|
|
|
return this._get(`/api/repos/${owner}/${repo}/pipelines?${query}`) as Promise<Pipeline[]>;
|
2022-09-27 09:05:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 15:33:12 +00:00
|
|
|
getPipeline(owner: string, repo: string, pipelineNumber: number | 'latest'): Promise<Pipeline> {
|
|
|
|
return this._get(`/api/repos/${owner}/${repo}/pipelines/${pipelineNumber}`) as Promise<Pipeline>;
|
2021-11-03 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 15:33:12 +00:00
|
|
|
getPipelineConfig(owner: string, repo: string, pipelineNumber: number): Promise<PipelineConfig[]> {
|
|
|
|
return this._get(`/api/repos/${owner}/${repo}/pipelines/${pipelineNumber}/config`) as Promise<PipelineConfig[]>;
|
2022-01-09 02:59:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
getPipelineFeed(opts?: Record<string, string | number | boolean>): Promise<PipelineFeed[]> {
|
2021-11-03 16:40:31 +00:00
|
|
|
const query = encodeQueryString(opts);
|
2022-10-18 01:24:12 +00:00
|
|
|
return this._get(`/api/user/feed?${query}`) as Promise<PipelineFeed[]>;
|
2021-11-03 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 15:33:12 +00:00
|
|
|
cancelPipeline(owner: string, repo: string, pipelineNumber: number): Promise<unknown> {
|
|
|
|
return this._post(`/api/repos/${owner}/${repo}/pipelines/${pipelineNumber}/cancel`);
|
2021-11-03 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 15:33:12 +00:00
|
|
|
approvePipeline(owner: string, repo: string, pipelineNumber: string): Promise<unknown> {
|
|
|
|
return this._post(`/api/repos/${owner}/${repo}/pipelines/${pipelineNumber}/approve`);
|
2021-11-03 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 15:33:12 +00:00
|
|
|
declinePipeline(owner: string, repo: string, pipelineNumber: string): Promise<unknown> {
|
|
|
|
return this._post(`/api/repos/${owner}/${repo}/pipelines/${pipelineNumber}/decline`);
|
2021-11-03 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
restartPipeline(
|
2021-11-03 16:40:31 +00:00
|
|
|
owner: string,
|
|
|
|
repo: string,
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline: string,
|
2021-11-03 16:40:31 +00:00
|
|
|
opts?: Record<string, string | number | boolean>,
|
|
|
|
): Promise<unknown> {
|
|
|
|
const query = encodeQueryString(opts);
|
2022-10-18 01:24:12 +00:00
|
|
|
return this._post(`/api/repos/${owner}/${repo}/pipelines/${pipeline}?${query}`);
|
2021-11-03 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
getLogs(owner: string, repo: string, pipeline: number, step: number): Promise<PipelineLog[]> {
|
|
|
|
return this._get(`/api/repos/${owner}/${repo}/logs/${pipeline}/${step}`) as Promise<PipelineLog[]>;
|
2021-11-03 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
getArtifact(owner: string, repo: string, pipeline: string, step: string, file: string): Promise<unknown> {
|
|
|
|
return this._get(`/api/repos/${owner}/${repo}/files/${pipeline}/${step}/${file}?raw=true`);
|
2021-11-03 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
getArtifactList(owner: string, repo: string, pipeline: string): Promise<unknown> {
|
|
|
|
return this._get(`/api/repos/${owner}/${repo}/files/${pipeline}`);
|
2021-11-03 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getSecretList(owner: string, repo: string): Promise<Secret[]> {
|
|
|
|
return this._get(`/api/repos/${owner}/${repo}/secrets`) as Promise<Secret[]>;
|
|
|
|
}
|
|
|
|
|
|
|
|
createSecret(owner: string, repo: string, secret: Partial<Secret>): Promise<unknown> {
|
|
|
|
return this._post(`/api/repos/${owner}/${repo}/secrets`, secret);
|
|
|
|
}
|
|
|
|
|
2022-03-01 23:19:33 +00:00
|
|
|
updateSecret(owner: string, repo: string, secret: Partial<Secret>): Promise<unknown> {
|
|
|
|
return this._patch(`/api/repos/${owner}/${repo}/secrets/${secret.name}`, secret);
|
|
|
|
}
|
|
|
|
|
2021-11-03 16:40:31 +00:00
|
|
|
deleteSecret(owner: string, repo: string, secretName: string): Promise<unknown> {
|
|
|
|
return this._delete(`/api/repos/${owner}/${repo}/secrets/${secretName}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
getRegistryList(owner: string, repo: string): Promise<Registry[]> {
|
|
|
|
return this._get(`/api/repos/${owner}/${repo}/registry`) as Promise<Registry[]>;
|
|
|
|
}
|
|
|
|
|
|
|
|
createRegistry(owner: string, repo: string, registry: Partial<Registry>): Promise<unknown> {
|
|
|
|
return this._post(`/api/repos/${owner}/${repo}/registry`, registry);
|
|
|
|
}
|
|
|
|
|
2022-03-01 23:19:33 +00:00
|
|
|
updateRegistry(owner: string, repo: string, registry: Partial<Registry>): Promise<unknown> {
|
|
|
|
return this._patch(`/api/repos/${owner}/${repo}/registry/${registry.address}`, registry);
|
|
|
|
}
|
|
|
|
|
2021-11-03 16:40:31 +00:00
|
|
|
deleteRegistry(owner: string, repo: string, registryAddress: string): Promise<unknown> {
|
|
|
|
return this._delete(`/api/repos/${owner}/${repo}/registry/${registryAddress}`);
|
|
|
|
}
|
|
|
|
|
2022-08-31 22:36:32 +00:00
|
|
|
getCronList(owner: string, repo: string): Promise<Cron[]> {
|
|
|
|
return this._get(`/api/repos/${owner}/${repo}/cron`) as Promise<Cron[]>;
|
|
|
|
}
|
|
|
|
|
|
|
|
createCron(owner: string, repo: string, cron: Partial<Cron>): Promise<unknown> {
|
|
|
|
return this._post(`/api/repos/${owner}/${repo}/cron`, cron);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCron(owner: string, repo: string, cron: Partial<Cron>): Promise<unknown> {
|
|
|
|
return this._patch(`/api/repos/${owner}/${repo}/cron/${cron.id}`, cron);
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteCron(owner: string, repo: string, cronId: number): Promise<unknown> {
|
|
|
|
return this._delete(`/api/repos/${owner}/${repo}/cron/${cronId}`);
|
|
|
|
}
|
|
|
|
|
2022-10-25 23:23:28 +00:00
|
|
|
runCron(owner: string, repo: string, cronId: number): Promise<Pipeline> {
|
|
|
|
return this._post(`/api/repos/${owner}/${repo}/cron/${cronId}`) as Promise<Pipeline>;
|
|
|
|
}
|
|
|
|
|
2022-08-14 11:48:53 +00:00
|
|
|
getOrgPermissions(owner: string): Promise<OrgPermissions> {
|
|
|
|
return this._get(`/api/orgs/${owner}/permissions`) as Promise<OrgPermissions>;
|
|
|
|
}
|
|
|
|
|
|
|
|
getOrgSecretList(owner: string): Promise<Secret[]> {
|
|
|
|
return this._get(`/api/orgs/${owner}/secrets`) as Promise<Secret[]>;
|
|
|
|
}
|
|
|
|
|
|
|
|
createOrgSecret(owner: string, secret: Partial<Secret>): Promise<unknown> {
|
|
|
|
return this._post(`/api/orgs/${owner}/secrets`, secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateOrgSecret(owner: string, secret: Partial<Secret>): Promise<unknown> {
|
|
|
|
return this._patch(`/api/orgs/${owner}/secrets/${secret.name}`, secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteOrgSecret(owner: string, secretName: string): Promise<unknown> {
|
|
|
|
return this._delete(`/api/orgs/${owner}/secrets/${secretName}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
getGlobalSecretList(): Promise<Secret[]> {
|
|
|
|
return this._get(`/api/secrets`) as Promise<Secret[]>;
|
|
|
|
}
|
|
|
|
|
|
|
|
createGlobalSecret(secret: Partial<Secret>): Promise<unknown> {
|
|
|
|
return this._post(`/api/secrets`, secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateGlobalSecret(secret: Partial<Secret>): Promise<unknown> {
|
|
|
|
return this._patch(`/api/secrets/${secret.name}`, secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteGlobalSecret(secretName: string): Promise<unknown> {
|
|
|
|
return this._delete(`/api/secrets/${secretName}`);
|
|
|
|
}
|
|
|
|
|
2021-11-03 16:40:31 +00:00
|
|
|
getSelf(): Promise<unknown> {
|
|
|
|
return this._get('/api/user');
|
|
|
|
}
|
|
|
|
|
|
|
|
getToken(): Promise<string> {
|
|
|
|
return this._post('/api/user/token') as Promise<string>;
|
|
|
|
}
|
|
|
|
|
2023-01-28 13:13:04 +00:00
|
|
|
getAgents(): Promise<Agent[]> {
|
|
|
|
return this._get('/api/agents') as Promise<Agent[]>;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAgent(agentId: Agent['id']): Promise<Agent> {
|
|
|
|
return this._get(`/api/agents/${agentId}`) as Promise<Agent>;
|
|
|
|
}
|
|
|
|
|
|
|
|
createAgent(agent: Partial<Agent>): Promise<Agent> {
|
|
|
|
return this._post('/api/agents', agent) as Promise<Agent>;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAgent(agent: Partial<Agent>): Promise<unknown> {
|
|
|
|
return this._patch(`/api/agents/${agent.id}`, agent);
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteAgent(agent: Agent): Promise<unknown> {
|
|
|
|
return this._delete(`/api/agents/${agent.id}`);
|
|
|
|
}
|
|
|
|
|
2021-11-03 16:40:31 +00:00
|
|
|
// eslint-disable-next-line promise/prefer-await-to-callbacks
|
2022-10-28 15:38:53 +00:00
|
|
|
on(callback: (data: { pipeline?: Pipeline; repo?: Repo; step?: PipelineStep }) => void): EventSource {
|
2021-11-03 16:40:31 +00:00
|
|
|
return this._subscribe('/stream/events', callback, {
|
|
|
|
reconnect: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
streamLogs(
|
|
|
|
owner: string,
|
|
|
|
repo: string,
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline: number,
|
2022-10-28 15:38:53 +00:00
|
|
|
step: number,
|
2021-11-03 16:40:31 +00:00
|
|
|
// eslint-disable-next-line promise/prefer-await-to-callbacks
|
2022-10-18 01:24:12 +00:00
|
|
|
callback: (data: PipelineLog) => void,
|
2021-11-03 16:40:31 +00:00
|
|
|
): EventSource {
|
2022-10-28 15:38:53 +00:00
|
|
|
return this._subscribe(`/stream/logs/${owner}/${repo}/${pipeline}/${step}`, callback, {
|
2021-11-03 16:40:31 +00:00
|
|
|
reconnect: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|