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

runReferences.ts

90 lines3.2 KB

1// What a run touched, for a person reading the run: the issue that started it, the pull request a
2// step merged. Anything that touches something calls `noteReference`; a new kind of thing is a
3// new caller, and nothing else changes.
4import {
5 type IssueRef,
6 type IssueState,
7 type PullRequestRef,
8 WorkflowReferenceKind,
9} from "@t3tools/contracts";
10import * as Cause from "effect/Cause";
11import * as Context from "effect/Context";
12import * as Effect from "effect/Effect";
13import type * as Schema from "effect/Schema";
14​
15import type { ReferenceInput, WorkflowStore } from "./WorkflowStore.ts";
16​
17/** Keeps what the step whose code is running touches, against that step. */
18export class StepReferences extends Context.Service<
19 StepReferences,
20 { readonly note: (reference: ReferenceInput) => Effect.Effect<void> }
21>()("t3/workflows/runReferences/StepReferences") {}
22​
23/** Keep that `runId` touched `reference`, from `step` or, with none, from what started it. */
24export const noteFor = (
25 store: WorkflowStore["Service"],
26 runId: string,
27 step: string | null,
28 reference: ReferenceInput,
29): Effect.Effect<void> =>
30 store.referenceNoted(runId, step, reference).pipe(
31 // Only for a person to read: a step must never fail or wait over it.
32 Effect.catchCause((cause) =>
33 Effect.logWarning("Could not keep what a workflow run touched", {
34 runId,
35 step,
36 kind: reference.kind,
37 cause: Cause.pretty(cause),
38 }),
39 ),
40 );
41​
42/** Keep that the running step touched `reference`. Outside a step it does nothing. */
43export const noteReference = (reference: ReferenceInput): Effect.Effect<void> =>
44 Effect.flatMap(Effect.serviceOption(StepReferences), (current) =>
45 current._tag === "None" ? Effect.void : current.value.note(reference),
46 );
47​
48export const stepReferences = (
49 store: WorkflowStore["Service"],
50 runId: string,
51 step: string,
52): Context.Context<StepReferences> =>
53 Context.make(StepReferences, { note: (reference) => noteFor(store, runId, step, reference) });
54​
55/** An issue, with its title and where it stands when the toucher read them. */
56export const issueReference = (
57 ref: IssueRef,
58 seen?: { readonly title: string; readonly url: string; readonly state: IssueState },
59): ReferenceInput => ({
60 kind: WorkflowReferenceKind.issue,
61 key: JSON.stringify([ref.tracker, ref.host ?? "", ref.scope.toLowerCase(), ref.key]),
62 address: {
63 projectId: ref.projectId,
64 tracker: ref.tracker,
65 ...(ref.host === undefined ? {} : { host: ref.host }),
66 scope: ref.scope,
67 key: ref.key,
68 } satisfies Schema.Json,
69 title: seen?.title ?? "",
70 url: seen?.url,
71 state: seen?.state.category,
72});
73​
74/** A pull request by its normalized host and repository, so every way of naming it is one. */
75export const pullRequestReference = (
76 ref: Pick<PullRequestRef, "projectId" | "repository" | "number"> & { readonly host: string },
77 seen?: { readonly title?: string; readonly url?: string; readonly state?: string },
78): ReferenceInput => ({
79 kind: WorkflowReferenceKind.pullRequest,
80 key: JSON.stringify([ref.host, ref.repository, ref.number]),
81 address: {
82 projectId: ref.projectId,
83 host: ref.host,
84 repository: ref.repository,
85 number: ref.number,
86 },
87 title: seen?.title ?? "",
88 url: seen?.url,
89 state: seen?.state,
90});