Skip to content
383 linesCodeBlameRaw
1import type { User, Viewer } from "./identity";
2import type { RepoPath } from "./repos";
3import type { Result } from "./result";
4
5/**
6 * GitHub Actions workflows, run on g1t as they are. Mirrors
7 * `crates/contracts/src/actions.rs`.
8 */
9
10export type WorkflowNote = {
11 severity: "info" | "warning" | "unsupported";
12 job: string | null;
13 message: string;
14};
15
16/** One `workflow_dispatch` input, as written in the workflow. */
17export type DispatchInput = {
18 description?: string;
19 required?: boolean;
20 default?: string | number | boolean;
21 type?: "string" | "boolean" | "number" | "choice" | "environment";
22 options?: string[];
23};
24
25/**
26 * `action_required`: a pull request's run from outside, waiting for someone
27 * with the Write role to approve it. `waiting`: its jobs are held by an
28 * environment's protection rules (a run's detail says so; lists do not).
29 */
30export type RunStatus = "pending" | "action_required" | "queued" | "in_progress" | "waiting" | "completed";
31export type Conclusion = "success" | "failure" | "cancelled" | "skipped";
32
33export type WorkflowRun = {
34 id: string;
35 workflowId: string;
36 path: string;
37 name: string;
38 title: string;
39 number: number;
40 attempt: number;
41 event: string;
42 ref: string;
43 sha: string;
44 pull: number | null;
45 status: RunStatus;
46 conclusion: Conclusion | null;
47 error: string | null;
48 actor: string | null;
49 createdAt: string;
50 startedAt: string | null;
51 finishedAt: string | null;
52};
53
54export type Workflow = {
55 id: string;
56 path: string;
57 name: string;
58 events: string[];
59 state: "active" | "disabled";
60 error: string | null;
61 notes: WorkflowNote[];
62 dispatch: Record<string, DispatchInput> | null;
63 lastRun: WorkflowRun | null;
64};
65
66export type StepState = {
67 number: number;
68 name: string;
69 status: "queued" | "in_progress" | "completed";
70 conclusion: Conclusion | null;
71 startedAt: string | null;
72 finishedAt: string | null;
73};
74
75export type Annotation = {
76 level: "error" | "warning" | "notice";
77 message: string;
78 title: string | null;
79 file: string | null;
80 line: number | null;
81};
82
83export type Job = {
84 id: string;
85 runId: string;
86 key: string;
87 name: string;
88 needs: string[];
89 /**
90 * `calling`: running the reusable workflow it calls, whose jobs follow it.
91 * `pending`: held by its environment's protection rules; `reason` says for what.
92 */
93 status: "waiting" | "pending" | "queued" | "in_progress" | "calling" | "completed";
94 conclusion: Conclusion | null;
95 steps: StepState[];
96 annotations: Annotation[];
97 reason: string | null;
98 startedAt: string | null;
99 finishedAt: string | null;
100 /** The environment it names, once its needs are done. */
101 environment?: string | null;
102 /** Its `runs-on` names self-hosted runners. */
103 selfHosted?: boolean;
104 /** The self-hosted runner that took it, by name. */
105 runner?: string | null;
106};
107
108export type RunDetail = {
109 run: WorkflowRun;
110 jobs: Job[];
111 notes: WorkflowNote[];
112 /** For a pull request's run from outside: whether it waits for, or had, approval. */
113 approval?: RunApproval | null;
114 /** The environments whose protection rules hold its jobs, this attempt. */
115 pendingDeployments?: PendingDeployment[];
116};
117
118export type RunApproval = {
119 state: "required" | "approved";
120 /** Why it waits, in words. */
121 reason: string;
122 approvedBy: string | null;
123};
124
125/** One person or team who may approve an environment's jobs. */
126export type EnvironmentReviewer = { type: "user" | "team"; name: string };
127
128/** A branch or tag pattern an environment takes deployments from. */
129export type BranchPattern = { name: string; type: "branch" | "tag" };
130
131/** The most reviewers an environment may have. */
132export const MAX_ENVIRONMENT_REVIEWERS = 6;
133/** The longest wait timer, in minutes (30 days). */
134export const MAX_WAIT_MINUTES = 43_200;
135
136/**
137 * An environment and its protection rules. Jobs naming it with
138 * `environment:` wait until the rules let them through, and only then get
139 * its secrets.
140 */
141export type Environment = {
142 /** Lowercase. */
143 name: string;
144 reviewers: EnvironmentReviewer[];
145 preventSelfReview: boolean;
146 waitMinutes: number;
147 /** `protected`: branches the rules protect; `selected`: `branchPatterns`. */
148 branchPolicy: "all" | "protected" | "selected";
149 branchPatterns: BranchPattern[];
150 adminsBypass: boolean;
151 /** Whether it has rules saved; false for one only named by a workflow or a secret. */
152 protected: boolean;
153 updatedAt: string | null;
154 updatedBy: string | null;
155};
156
157/** What changes an environment's rules; left out is unchanged. */
158export type EnvironmentChange = Partial<
159 Pick<Environment, "reviewers" | "preventSelfReview" | "waitMinutes" | "branchPolicy" | "branchPatterns" | "adminsBypass">
160>;
161
162/** An environment holding a run's jobs, and where its rules stand. */
163export type PendingDeployment = {
164 environment: string;
165 state: "waiting" | "approved" | "rejected";
166 needsReview: boolean;
167 /** When its wait timer lets its jobs start. */
168 waitUntil: string | null;
169 reviewers: EnvironmentReviewer[];
170 /** The jobs it holds, by name. */
171 jobs: string[];
172 /** Whether the viewer may approve or reject it now. */
173 canReview: boolean;
174 reviewedBy: string | null;
175 comment: string | null;
176 reviewedAt: string | null;
177};
178
179/** Which pull requests' runs wait for approval, least strict first. */
180export const APPROVAL_POLICIES = ["first_time_contributors", "outside_contributors", "all_external_contributors"] as const;
181export type ApprovalPolicy = (typeof APPROVAL_POLICIES)[number];
182
183/** A repository's choices for its workflows. */
184export type ActionsSettings = {
185 /**
186 * What a workflow without `permissions:` gets. Unchosen, a repository made
187 * before restricted tokens keeps `write`; a newer one takes its
188 * workspace's default. Never more than `maxPermissions`.
189 */
190 defaultPermissions: "read" | "write";
191 /** Whether the repository chose it. */
192 defaultChosen: boolean;
193 /** The most the workspace lets a repository's default be. */
194 maxPermissions: "read" | "write";
195 approvalPolicy: ApprovalPolicy;
196 /** "Allow g1t Actions to create and approve pull requests". */
197 canApprovePullRequests: boolean;
198 /** Whether the workspace lets its repositories turn that on. */
199 workspaceAllowsPullRequests: boolean;
200 /**
201 * Who may use the repository's actions and reusable workflows while it is
202 * private: only itself (`none`), or private repositories of its workspace
203 * (`organization`). A public repository's are anyone's.
204 */
205 accessLevel: ActionsAccessLevel;
206};
207
208/** Settings, Actions, Access. */
209export const ACTIONS_ACCESS_LEVELS = ["none", "organization"] as const;
210export type ActionsAccessLevel = (typeof ACTIONS_ACCESS_LEVELS)[number];
211
212/** What changes a repository's choices; `inherit` unchooses its default. */
213export type ActionsSettingsChange = {
214 defaultPermissions?: "read" | "write" | "inherit";
215 approvalPolicy?: ApprovalPolicy;
216 canApprovePullRequests?: boolean;
217 accessLevel?: ActionsAccessLevel;
218};
219
220/** A workspace's policy for its repositories' job tokens. */
221export type WorkspaceActionsSettings = {
222 /** What a repository made from now on gets by default. */
223 defaultPermissions: "read" | "write";
224 /** The most any repository's default may be. */
225 maxPermissions: "read" | "write";
226 canApprovePullRequests: boolean;
227};
228
229export type LogChunk = { seq: number; step: number; text: string };
230export type JobLog = { chunks: LogChunk[]; done: boolean };
231
232/**
233 * Who may read a secret or variable: `workflows` (`secrets.*`, `vars.*` in
234 * GitHub Actions) and `deployments` (a deploy build's environment and the
235 * running app's bindings). Agents, checks and the merge queue never read
236 * any.
237 */
238export type SettingReader = "workflows" | "deployments";
239
240/**
241 * One row of secrets and variables, as Vercel lists environment variables:
242 * a key, its type, the environments it applies to and who reads it. A key
243 * may have one row per environment. Secrets' values are never returned.
244 */
245export type Setting = {
246 id: string;
247 name: string;
248 /** `variable` is shown as Config. Config may become a secret, never back. */
249 kind: SettingKind;
250 /** A variable's value. */
251 value: string | null;
252 /** A project's (a repository's belong to its project) or the workspace's. */
253 scope: "project" | "workspace";
254 updatedAt: string;
255 availableTo: SettingReader[];
256 /** The environments it applies to; empty is every environment. */
257 environments: string[];
258 /** A workspace's row: the projects it reaches, by slug; empty is every one. */
259 projects: string[];
260 /** Where to rotate it, or who to ask. */
261 note: string | null;
262 updatedBy: string | null;
263};
264
265/** What saving a row sets beyond its value; left out is unchanged. */
266export type SettingOptions = {
267 /** The row to change; left out, the key's row for every environment. */
268 id?: string;
269 availableTo?: SettingReader[];
270 environments?: string[];
271 /** A workspace's row: project slugs; empty for every one. */
272 projects?: string[];
273 note?: string;
274};
275
276export type SettingsOwner = { repo: RepoPath } | { workspace: string };
277export type SettingKind = "secret" | "variable";
278/** `all` lists both. */
279export type SettingKindFilter = SettingKind | "all";
280
281export type RunsFilter = {
282 workflow?: string;
283 branch?: string;
284 event?: string;
285 pull?: number;
286 sha?: string;
287 limit?: number;
288};
289
290export interface ActionsApi {
291 workflows(repo: RepoPath, viewer: Viewer): Promise<Result<Workflow[]>>;
292 runs(repo: RepoPath, viewer: Viewer, filter?: RunsFilter): Promise<Result<WorkflowRun[]>>;
293 run(repo: RepoPath, viewer: Viewer, id: string): Promise<Result<RunDetail>>;
294 logs(repo: RepoPath, viewer: Viewer, job: string, after?: number): Promise<Result<JobLog>>;
295 dispatch(
296 actor: User,
297 repo: RepoPath,
298 workflow: string,
299 ref: string | undefined,
300 inputs: Record<string, unknown>,
301 ): Promise<Result<WorkflowRun>>;
302 cancel(actor: User, repo: RepoPath, id: string): Promise<Result<WorkflowRun>>;
303 rerun(actor: User, repo: RepoPath, id: string, failedOnly?: boolean): Promise<Result<WorkflowRun>>;
304 setWorkflowEnabled(actor: User, repo: RepoPath, workflow: string, enabled: boolean): Promise<Result<Workflow>>;
305 settings(actor: User, owner: SettingsOwner, kind: SettingKindFilter): Promise<Result<Setting[]>>;
306 /** `value` null keeps an existing entry's default value. */
307 setSetting(
308 actor: User,
309 owner: SettingsOwner,
310 kind: SettingKind,
311 name: string,
312 value: string | null,
313 options?: SettingOptions,
314 ): Promise<Result<Setting>>;
315 /** One row by `id`, or every row of the key. */
316 deleteSetting(actor: User, owner: SettingsOwner, kind: SettingKindFilter, name: string, id?: string): Promise<Result<boolean>>;
317 /** Lets a pull request's run from outside start. Write role. */
318 approveRun(actor: User, repo: RepoPath, id: string): Promise<Result<WorkflowRun>>;
319 pendingDeployments(repo: RepoPath, viewer: Viewer, id: string): Promise<Result<PendingDeployment[]>>;
320 /** Approves or rejects the jobs `environments` hold (every waiting one when empty). */
321 reviewDeployments(
322 actor: User,
323 repo: RepoPath,
324 id: string,
325 state: "approved" | "rejected",
326 environments?: string[],
327 comment?: string,
328 ): Promise<Result<PendingDeployment[]>>;
329 actionsSettings(repo: RepoPath, viewer: Viewer): Promise<Result<ActionsSettings>>;
330 /** Admin role. Left out is unchanged. */
331 setActionsSettings(actor: User, repo: RepoPath, change: ActionsSettingsChange): Promise<Result<ActionsSettings>>;
332 /** Members. */
333 workspaceActionsSettings(workspace: string, viewer: Viewer): Promise<Result<WorkspaceActionsSettings>>;
334 /** Owners. Left out is unchanged. */
335 setWorkspaceActionsSettings(
336 actor: User,
337 workspace: string,
338 change: Partial<WorkspaceActionsSettings>,
339 ): Promise<Result<WorkspaceActionsSettings>>;
340 /** Every environment the repository's rules, secrets, workflows or jobs name. */
341 environments(repo: RepoPath, viewer: Viewer): Promise<Result<Environment[]>>;
342 /** Admin role. */
343 setEnvironment(actor: User, repo: RepoPath, name: string, change: EnvironmentChange): Promise<Result<Environment>>;
344 deleteEnvironment(actor: User, repo: RepoPath, name: string): Promise<Result<boolean>>;
345 /** A repository's artifacts, newest first, or one run's. */
346 artifacts(repo: RepoPath, viewer: Viewer, filter?: { run?: string; name?: string; page?: number; per_page?: number }): Promise<Result<ArtifactList>>;
347 /** One artifact by id, or by run and name, with a token to download it for a few minutes. */
348 artifactDownload(repo: RepoPath, viewer: Viewer, by: { id?: number; run?: string; name?: string }): Promise<Result<ArtifactBlob>>;
349 /** Needs the Write role. */
350 deleteArtifact(actor: User, repo: RepoPath, id: number): Promise<Result<Artifact>>;
351 /** How long the repository keeps artifacts; with `days`, sets it (Maintain). */
352 artifactRetention(repo: RepoPath, viewer: Viewer, days?: number): Promise<Result<ArtifactRetention>>;
353}
354
355/**
356 * A workflow run's artifact, kept in R2 for its retention days. Mirrors
357 * `g1t_contracts::actions::Artifact` (which travels in `snake_case`).
358 */
359export type Artifact = {
360 id: number;
361 name: string;
362 size: number;
363 /** `sha256:<hex>`, when the uploader said. */
364 digest: string | null;
365 /** `zip`, or `tgz` for one an older runner sent. */
366 format: string;
367 run_id: string;
368 job_id: string;
369 repo_id: string;
370 expired: boolean;
371 created_at: string;
372 updated_at: string;
373 expires_at: string;
374 head_branch?: string | null;
375 head_sha?: string | null;
376};
377
378export type ArtifactList = { total_count: number; artifacts: Artifact[] };
379
380/** An artifact, where it is, and a signed token for the API's `/actions/toolkit/blobs/{blob}`. */
381export type ArtifactBlob = { artifact: Artifact; object: string; blob: string };
382
383export type ArtifactRetention = { days: number; maximum_allowed_days: number };