Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

forgeApi.ts

109 lines4.4 KB

1// Issues and pull requests as a workflow file sees them: triggers, calls for inside a step, and a
2// wait on a pull request for the body.
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import type * as Option from "effect/Option";
6​
7import { checkOptions } from "./options.ts";
8import { issuesOpened, pullRequestsOpened } from "./triggers.ts";
9import type { InRun } from "./workflow.ts";
10import { waitAs } from "./workflow.ts";
11import {
12 type IssueAddress,
13 type PullRequestAddress,
14 PullRequestStanding,
15 type PullRequestUntil,
16 type PullRequestWatch,
17 WorkflowForge,
18} from "./WorkflowForge.ts";
19​
20const forge = <A, E>(use: (forge: WorkflowForge["Service"]) => Effect.Effect<A, E>) =>
21 Effect.flatMap(WorkflowForge, use);
22​
23/**
24 * The project's issues, in whichever tracker it uses. `opened` is a trigger; the rest are for
25 * inside `step` or `onEnd`. An issue is named by what a trigger or `list` gave, or by
26 * `{ project, url }`.
27 */
28export const issues = {
29 opened: issuesOpened,
30 list: (query: Parameters<WorkflowForge["Service"]["issues"]["list"]>[0]) => {
31 checkOptions("issues.list", query, [
32 "project",
33 "state",
34 "labels",
35 "query",
36 "assignee",
37 "sort",
38 "limit",
39 ]);
40 return forge((all) => all.issues.list(query));
41 },
42 read: (issue: IssueAddress) => forge((all) => all.issues.read(issue)),
43 comment: (issue: IssueAddress, body: string) => forge((all) => all.issues.comment(issue, body)),
44 /** Move the issue to one of its `states`, as `read` lists them. */
45 setState: (issue: IssueAddress, state: string) =>
46 forge((all) => all.issues.setState(issue, state)),
47 create: (input: Parameters<WorkflowForge["Service"]["issues"]["create"]>[0]) => {
48 checkOptions("issues.create", input, ["project", "title", "body", "labels", "assignees"]);
49 return forge((all) => all.issues.create(input));
50 },
51};
52​
53/**
54 * The project's pull requests on its git host. `opened` is a trigger; the rest are for inside
55 * `step` or `onEnd`. A pull request is named by what a trigger or `list` gave, or by
56 * `{ project, url }`.
57 */
58export const pullRequests = {
59 opened: pullRequestsOpened,
60 list: (query: Parameters<WorkflowForge["Service"]["pullRequests"]["list"]>[0]) => {
61 checkOptions("pullRequests.list", query, ["project", "state", "query", "limit"]);
62 return forge((all) => all.pullRequests.list(query));
63 },
64 read: (pullRequest: PullRequestAddress) => forge((all) => all.pullRequests.read(pullRequest)),
65 comment: (pullRequest: PullRequestAddress, body: string) =>
66 forge((all) => all.pullRequests.comment(pullRequest, body)),
67 /** `merge`, `close`, `ready`, `update-branch` and the host's other actions on a pull request. */
68 act: (
69 pullRequest: PullRequestAddress,
70 action: Parameters<WorkflowForge["Service"]["pullRequests"]["act"]>[1],
71 options?: Parameters<WorkflowForge["Service"]["pullRequests"]["act"]>[2],
72 ) => {
73 checkOptions("pullRequests.act", options, ["mergeMethod"]);
74 return forge((all) => all.pullRequests.act(pullRequest, action, options));
75 },
76 /**
77 * Wait in the body until the pull request gets somewhere, without holding anything: `checks`
78 * until none of its checks is still running, `merged` or `closed` until it is no longer open.
79 * A pull request that is merged or closed ends any of them. Gives where it stands, or nothing
80 * after `timeout`. The host is asked about once a minute.
81 */
82 wait: (
83 name: string,
84 pullRequest: PullRequestAddress,
85 options: { readonly until: PullRequestUntil; readonly timeout?: Duration.Input },
86 ): Effect.Effect<Option.Option<PullRequestStanding>, never, InRun> => {
87 checkOptions(`pullRequests.wait("${name}")`, options, ["until", "timeout"]);
88 return Effect.orDie(
89 waitAs("wait", name, {
90 returns: PullRequestStanding,
91 timeout: options.timeout,
92 watch: {
93 kind: "pullRequest",
94 until: options.until,
95 // Only the address: a listing's row carries far more than a wait needs to keep.
96 pullRequest:
97 "url" in pullRequest && "project" in pullRequest
98 ? { project: pullRequest.project, url: pullRequest.url }
99 : {
100 projectId: pullRequest.projectId,
101 host: pullRequest.host,
102 repository: pullRequest.repository,
103 number: pullRequest.number,
104 },
105 } satisfies PullRequestWatch,
106 }),
107 );
108 },
109};