Skip to content
499 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 /** For a managed workflow (`managed/<name>.yml`): what installed it. */
65 managedBy?: ManagedBy | null;
66};
67
68/** What installed a managed workflow in a repository. */
69export type ManagedBy = {
70 /** `g1t` for one g1t's services installed, or the workspace's name. */
71 origin: string;
72 /** A workspace's: `required` or `default`; empty for g1t's. */
73 mode: "required" | "default" | "";
74 /** A workspace's: the file it was copied from, `owner/repo/path@ref`. */
75 source: string | null;
76};
77
78/**
79 * A workflow a workspace's owners added to its repositories: it runs in
80 * each repository it targets as that repository's own, under
81 * `managed/<name>.yml`, from a copy of its source taken when it was saved.
82 */
83export type WorkspaceManagedWorkflow = {
84 name: string;
85 path: string;
86 /** Its `name:`, as repositories list it. */
87 workflowName: string;
88 /** `owner/repo/.g1t/workflows/file.yml@ref`. */
89 source: string;
90 sourceSha: string | null;
91 /** A private source runs in private repositories only. */
92 sourcePrivate: boolean;
93 mode: "required" | "default";
94 /** Names and patterns; empty is every repository. */
95 repositories: string[];
96 visibility: "all" | "public" | "private";
97 content: string;
98 createdAt: string;
99 updatedAt: string;
100 updatedBy: string;
101};
102
103export type WorkspaceManagedWorkflowChange = {
104 source?: string;
105 mode?: "required" | "default";
106 repositories?: string[];
107 visibility?: "all" | "public" | "private";
108 /** Copy the source again. */
109 refresh?: boolean;
110};
111
112export type StepState = {
113 number: number;
114 name: string;
115 status: "queued" | "in_progress" | "completed";
116 conclusion: Conclusion | null;
117 startedAt: string | null;
118 finishedAt: string | null;
119};
120
121export type Annotation = {
122 level: "error" | "warning" | "notice";
123 message: string;
124 title: string | null;
125 file: string | null;
126 line: number | null;
127};
128
129export type Job = {
130 id: string;
131 runId: string;
132 key: string;
133 name: string;
134 needs: string[];
135 /**
136 * `calling`: running the reusable workflow it calls, whose jobs follow it.
137 * `pending`: held by its environment's protection rules; `reason` says for what.
138 */
139 status: "waiting" | "pending" | "queued" | "in_progress" | "calling" | "completed";
140 conclusion: Conclusion | null;
141 steps: StepState[];
142 annotations: Annotation[];
143 reason: string | null;
144 startedAt: string | null;
145 finishedAt: string | null;
146 /** The environment it names, once its needs are done. */
147 environment?: string | null;
148 /** Its `runs-on` names self-hosted runners. */
149 selfHosted?: boolean;
150 /** The self-hosted runner that took it, by name. */
151 runner?: string | null;
152 /** Cancelled, and running its `if: always()` and `cancelled()` steps and post steps before it ends. */
153 cancelling?: boolean;
154 /** Where its deployment is (`environment.url`), for a job that deploys and says; the current attempt's only. */
155 environmentUrl?: string | null;
156 /** For a job that calls a reusable workflow: that workflow's file. Its jobs' keys are this job's key, `/`, their own. */
157 uses?: string | null;
158};
159
160/** One attempt of a run: the first, or a re-run. */
161export type RunAttempt = {
162 attempt: number;
163 status: RunStatus;
164 conclusion: Conclusion | null;
165 /** Who started it: whoever caused the run, then whoever re-ran it. */
166 actor: string | null;
167 /** It ran with debug logging. */
168 debug: boolean;
169 startedAt: string | null;
170 finishedAt: string | null;
171};
172
173/** What a job's steps wrote to `$GITHUB_STEP_SUMMARY`, in Markdown, masked. */
174export type JobSummary = { jobId: string; name: string; steps: { step: number; markdown: string }[] };
175
176/** A job's whole log, to download. `omitted`: left out of a run's logs that grew too large. */
177export type JobLogText = {
178 jobId: string;
179 name: string;
180 steps: StepState[];
181 chunks: LogChunk[];
182 done: boolean;
183 omitted?: boolean;
184};
185
186export type RunDetail = {
187 run: WorkflowRun;
188 jobs: Job[];
189 notes: WorkflowNote[];
190 /** For a pull request's run from outside: whether it waits for, or had, approval. */
191 approval?: RunApproval | null;
192 /** The environments whose protection rules hold its jobs, this attempt. */
193 pendingDeployments?: PendingDeployment[];
194 /** Every attempt, oldest first, the one shown (`run.attempt`) included. */
195 attempts?: RunAttempt[];
196};
197
198export type RunApproval = {
199 state: "required" | "approved";
200 /** Why it waits, in words. */
201 reason: string;
202 approvedBy: string | null;
203};
204
205/** One person or team who may approve an environment's jobs. */
206export type EnvironmentReviewer = { type: "user" | "team"; name: string };
207
208/** A branch or tag pattern an environment takes deployments from. */
209export type BranchPattern = { name: string; type: "branch" | "tag" };
210
211/** The most reviewers an environment may have. */
212export const MAX_ENVIRONMENT_REVIEWERS = 6;
213/** The longest wait timer, in minutes (30 days). */
214export const MAX_WAIT_MINUTES = 43_200;
215
216/**
217 * An environment and its protection rules. Jobs naming it with
218 * `environment:` wait until the rules let them through, and only then get
219 * its secrets.
220 */
221export type Environment = {
222 /** Lowercase. */
223 name: string;
224 reviewers: EnvironmentReviewer[];
225 preventSelfReview: boolean;
226 waitMinutes: number;
227 /** `protected`: branches the rules protect; `selected`: `branchPatterns`. */
228 branchPolicy: "all" | "protected" | "selected";
229 branchPatterns: BranchPattern[];
230 adminsBypass: boolean;
231 /** Whether it has rules saved; false for one only named by a workflow or a secret. */
232 protected: boolean;
233 updatedAt: string | null;
234 updatedBy: string | null;
235};
236
237/** What changes an environment's rules; left out is unchanged. */
238export type EnvironmentChange = Partial<
239 Pick<Environment, "reviewers" | "preventSelfReview" | "waitMinutes" | "branchPolicy" | "branchPatterns" | "adminsBypass">
240>;
241
242/** An environment holding a run's jobs, and where its rules stand. */
243export type PendingDeployment = {
244 environment: string;
245 state: "waiting" | "approved" | "rejected";
246 needsReview: boolean;
247 /** When its wait timer lets its jobs start. */
248 waitUntil: string | null;
249 reviewers: EnvironmentReviewer[];
250 /** The jobs it holds, by name. */
251 jobs: string[];
252 /** Whether the viewer may approve or reject it now. */
253 canReview: boolean;
254 reviewedBy: string | null;
255 comment: string | null;
256 reviewedAt: string | null;
257};
258
259/** Which pull requests' runs wait for approval, least strict first. */
260export const APPROVAL_POLICIES = ["first_time_contributors", "outside_contributors", "all_external_contributors"] as const;
261export type ApprovalPolicy = (typeof APPROVAL_POLICIES)[number];
262
263/** A repository's choices for its workflows. */
264export type ActionsSettings = {
265 /**
266 * What a workflow without `permissions:` gets. Unchosen, a repository made
267 * before restricted tokens keeps `write`; a newer one takes its
268 * workspace's default. Never more than `maxPermissions`.
269 */
270 defaultPermissions: "read" | "write";
271 /** Whether the repository chose it. */
272 defaultChosen: boolean;
273 /** The most the workspace lets a repository's default be. */
274 maxPermissions: "read" | "write";
275 approvalPolicy: ApprovalPolicy;
276 /** "Allow g1t Actions to create and approve pull requests". */
277 canApprovePullRequests: boolean;
278 /** Whether the workspace lets its repositories turn that on. */
279 workspaceAllowsPullRequests: boolean;
280 /**
281 * Who may use the repository's actions and reusable workflows while it is
282 * private: only itself (`none`), or private repositories of its workspace
283 * (`organization`). A public repository's are anyone's.
284 */
285 accessLevel: ActionsAccessLevel;
286};
287
288/** Settings, Actions, Access. */
289export const ACTIONS_ACCESS_LEVELS = ["none", "organization"] as const;
290export type ActionsAccessLevel = (typeof ACTIONS_ACCESS_LEVELS)[number];
291
292/** What changes a repository's choices; `inherit` unchooses its default. */
293export type ActionsSettingsChange = {
294 defaultPermissions?: "read" | "write" | "inherit";
295 approvalPolicy?: ApprovalPolicy;
296 canApprovePullRequests?: boolean;
297 accessLevel?: ActionsAccessLevel;
298};
299
300/** A workspace's policy for its repositories' job tokens. */
301export type WorkspaceActionsSettings = {
302 /** What a repository made from now on gets by default. */
303 defaultPermissions: "read" | "write";
304 /** The most any repository's default may be. */
305 maxPermissions: "read" | "write";
306 canApprovePullRequests: boolean;
307};
308
309export type LogChunk = { seq: number; step: number; text: string };
310export type JobLog = { chunks: LogChunk[]; done: boolean };
311
312/**
313 * Who may read a secret or variable: `workflows` (`secrets.*`, `vars.*` in
314 * GitHub Actions) and `deployments` (a deploy build's environment and the
315 * running app's bindings). Agents, checks and the merge queue never read
316 * any.
317 */
318export type SettingReader = "workflows" | "deployments";
319
320/**
321 * One row of secrets and variables, as Vercel lists environment variables:
322 * a key, its type, the environments it applies to and who reads it. A key
323 * may have one row per environment. Secrets' values are never returned.
324 */
325export type Setting = {
326 id: string;
327 name: string;
328 /** `variable` is shown as Config. Config may become a secret, never back. */
329 kind: SettingKind;
330 /** A variable's value. */
331 value: string | null;
332 /** A project's (a repository's belong to its project) or the workspace's. */
333 scope: "project" | "workspace";
334 updatedAt: string;
335 availableTo: SettingReader[];
336 /** The environments it applies to; empty is every environment. */
337 environments: string[];
338 /** A workspace's row: the projects it reaches, by slug; empty is every one. */
339 projects: string[];
340 /** Where to rotate it, or who to ask. */
341 note: string | null;
342 updatedBy: string | null;
343};
344
345/** What saving a row sets beyond its value; left out is unchanged. */
346export type SettingOptions = {
347 /** The row to change; left out, the key's row for every environment. */
348 id?: string;
349 availableTo?: SettingReader[];
350 environments?: string[];
351 /** A workspace's row: project slugs; empty for every one. */
352 projects?: string[];
353 note?: string;
354};
355
356export type SettingsOwner = { repo: RepoPath } | { workspace: string };
357export type SettingKind = "secret" | "variable";
358/** `all` lists both. */
359export type SettingKindFilter = SettingKind | "all";
360
361export type RunsFilter = {
362 workflow?: string;
363 branch?: string;
364 event?: string;
365 pull?: number;
366 sha?: string;
367 limit?: number;
368};
369
370export interface ActionsApi {
371 workflows(repo: RepoPath, viewer: Viewer): Promise<Result<Workflow[]>>;
372 runs(repo: RepoPath, viewer: Viewer, filter?: RunsFilter): Promise<Result<WorkflowRun[]>>;
373 /** The latest attempt, or an earlier one. */
374 run(repo: RepoPath, viewer: Viewer, id: string, attempt?: number): Promise<Result<RunDetail>>;
375 logs(repo: RepoPath, viewer: Viewer, job: string, after?: number): Promise<Result<JobLog>>;
376 /** The job summaries of an attempt (the latest by default), jobs without one left out. */
377 summaries(repo: RepoPath, viewer: Viewer, id: string, attempt?: number): Promise<Result<JobSummary[]>>;
378 /** A job's whole log, any attempt's, by the id the run gave the job. */
379 jobLogText(repo: RepoPath, viewer: Viewer, job: string): Promise<Result<JobLogText>>;
380 /** Every job's whole log for an attempt, to download as one archive. */
381 runLogs(repo: RepoPath, viewer: Viewer, id: string, attempt?: number): Promise<Result<JobLogText[]>>;
382 dispatch(
383 actor: User,
384 repo: RepoPath,
385 workflow: string,
386 ref: string | undefined,
387 inputs: Record<string, unknown>,
388 ): Promise<Result<WorkflowRun>>;
389 /** Running jobs clean up first; `force` (or cancelling a run that is cancelling) stops them outright. */
390 cancel(actor: User, repo: RepoPath, id: string, force?: boolean): Promise<Result<WorkflowRun>>;
391 /** A new attempt: every job, the failed ones, or `job` and what needs it; `debug` for debug logging. */
392 rerun(
393 actor: User,
394 repo: RepoPath,
395 id: string,
396 failedOnly?: boolean,
397 options?: { job?: string; debug?: boolean },
398 ): Promise<Result<WorkflowRun>>;
399 setWorkflowEnabled(actor: User, repo: RepoPath, workflow: string, enabled: boolean): Promise<Result<Workflow>>;
400 settings(actor: User, owner: SettingsOwner, kind: SettingKindFilter): Promise<Result<Setting[]>>;
401 /** `value` null keeps an existing entry's default value. */
402 setSetting(
403 actor: User,
404 owner: SettingsOwner,
405 kind: SettingKind,
406 name: string,
407 value: string | null,
408 options?: SettingOptions,
409 ): Promise<Result<Setting>>;
410 /** One row by `id`, or every row of the key. */
411 deleteSetting(actor: User, owner: SettingsOwner, kind: SettingKindFilter, name: string, id?: string): Promise<Result<boolean>>;
412 /** Lets a pull request's run from outside start. Write role. */
413 approveRun(actor: User, repo: RepoPath, id: string): Promise<Result<WorkflowRun>>;
414 pendingDeployments(repo: RepoPath, viewer: Viewer, id: string): Promise<Result<PendingDeployment[]>>;
415 /** Approves or rejects the jobs `environments` hold (every waiting one when empty). */
416 reviewDeployments(
417 actor: User,
418 repo: RepoPath,
419 id: string,
420 state: "approved" | "rejected",
421 environments?: string[],
422 comment?: string,
423 ): Promise<Result<PendingDeployment[]>>;
424 actionsSettings(repo: RepoPath, viewer: Viewer): Promise<Result<ActionsSettings>>;
425 /** Admin role. Left out is unchanged. */
426 setActionsSettings(actor: User, repo: RepoPath, change: ActionsSettingsChange): Promise<Result<ActionsSettings>>;
427 /** Members. */
428 workspaceActionsSettings(workspace: string, viewer: Viewer): Promise<Result<WorkspaceActionsSettings>>;
429 /** Owners. Left out is unchanged. */
430 setWorkspaceActionsSettings(
431 actor: User,
432 workspace: string,
433 change: Partial<WorkspaceActionsSettings>,
434 ): Promise<Result<WorkspaceActionsSettings>>;
435 /** Owners. */
436 workspaceManagedWorkflows(workspace: string, viewer: Viewer): Promise<Result<WorkspaceManagedWorkflow[]>>;
437 /** Owners. `create` adds one, which needs `source` and `mode`; otherwise left out is unchanged. */
438 setWorkspaceManagedWorkflow(
439 actor: User,
440 workspace: string,
441 name: string,
442 change: WorkspaceManagedWorkflowChange,
443 create?: boolean,
444 ): Promise<Result<WorkspaceManagedWorkflow>>;
445 /** Owners. */
446 removeWorkspaceManagedWorkflow(actor: User, workspace: string, name: string): Promise<Result<boolean>>;
447 /** Every environment the repository's rules, secrets, workflows or jobs name. */
448 environments(repo: RepoPath, viewer: Viewer): Promise<Result<Environment[]>>;
449 /** Admin role. */
450 setEnvironment(actor: User, repo: RepoPath, name: string, change: EnvironmentChange): Promise<Result<Environment>>;
451 deleteEnvironment(actor: User, repo: RepoPath, name: string): Promise<Result<boolean>>;
452 /** A repository's artifacts, newest first, or one run's. */
453 artifacts(repo: RepoPath, viewer: Viewer, filter?: { run?: string; name?: string; page?: number; per_page?: number }): Promise<Result<ArtifactList>>;
454 /** One artifact by id, or by run and name, with a token to download it for a few minutes. */
455 artifactDownload(repo: RepoPath, viewer: Viewer, by: { id?: number; run?: string; name?: string }): Promise<Result<ArtifactBlob>>;
456 /** Needs the Write role. */
457 deleteArtifact(actor: User, repo: RepoPath, id: number): Promise<Result<Artifact>>;
458 /** How long the repository keeps artifacts; with `days`, sets it (Maintain). */
459 artifactRetention(repo: RepoPath, viewer: Viewer, days?: number): Promise<Result<ArtifactRetention>>;
460}
461
462/**
463 * A workflow run's artifact, kept in R2 for its retention days. Mirrors
464 * `g1t_contracts::actions::Artifact` (which travels in `snake_case`).
465 */
466export type Artifact = {
467 id: number;
468 name: string;
469 size: number;
470 /** `sha256:<hex>`, when the uploader said. */
471 digest: string | null;
472 /** `zip`, or `tgz` for one an older runner sent. */
473 format: string;
474 run_id: string;
475 job_id: string;
476 repo_id: string;
477 expired: boolean;
478 created_at: string;
479 updated_at: string;
480 expires_at: string;
481 head_branch?: string | null;
482 head_sha?: string | null;
483};
484
485export type ArtifactList = { total_count: number; artifacts: Artifact[] };
486
487/** An artifact, where it is, and a signed token for the API's `/actions/toolkit/blobs/{blob}`. */
488export type ArtifactBlob = { artifact: Artifact; object: string; blob: string };
489
490export type ArtifactRetention = { days: number; maximum_allowed_days: number };
491
492/**
493 * Whether a workflow's path is a managed workflow's: one g1t installed in
494 * the repository without committing it (crates/actions/src/managed.rs), so
495 * no file in the repository holds it.
496 */
497export function isManagedWorkflow(path: string): boolean {
498 return path.startsWith("managed/");
499}