Open History to see the last commit
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
48/** Keep what `runId` is about, among what it touched too, so it opens as they do. */
49export const subjectSet = (
50 store: WorkflowStore["Service"],
51 runId: string,
52 subject: ReferenceInput,
53): Effect.Effect<void> =>
54 Effect.andThen(store.runSubject(runId, subject), noteFor(store, runId, null, subject));
55
56export const stepReferences = (
57 store: WorkflowStore["Service"],
58 runId: string,
59 step: string,
60): Context.Context<StepReferences> =>
61 Context.make(StepReferences, { note: (reference) => noteFor(store, runId, step, reference) });
62
63/** An issue, with its title and where it stands when the toucher read them. */
64export const issueReference = (
65 ref: IssueRef,
66 seen?: { readonly title: string; readonly url: string; readonly state: IssueState },
67): ReferenceInput => ({
68 kind: WorkflowReferenceKind.issue,
69 key: JSON.stringify([ref.tracker, ref.host ?? "", ref.scope.toLowerCase(), ref.key]),
70 address: {
71 projectId: ref.projectId,
72 tracker: ref.tracker,
73 ...(ref.host === undefined ? {} : { host: ref.host }),
74 scope: ref.scope,
75 key: ref.key,
76 } satisfies Schema.Json,
77 title: seen?.title ?? "",
78 url: seen?.url,
79 state: seen?.state.category,
80});
81
82/** A pull request by its normalized host and repository, so every way of naming it is one. */
83export const pullRequestReference = (
84 ref: Pick<PullRequestRef, "projectId" | "repository" | "number"> & { readonly host: string },
85 seen?: { readonly title?: string; readonly url?: string; readonly state?: string },
86): ReferenceInput => ({
87 kind: WorkflowReferenceKind.pullRequest,
88 key: JSON.stringify([ref.host, ref.repository, ref.number]),
89 address: {
90 projectId: ref.projectId,
91 host: ref.host,
92 repository: ref.repository,
93 number: ref.number,
94 },
95 title: seen?.title ?? "",
96 url: seen?.url,
97 state: seen?.state,
98});