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

linkCreatedPullRequest.ts

97 lines3.6 KB

1import { sourceControlRepositorySelector } from "@t3tools/shared/sourceControl";
2import {
3 type CommandId,
4 pullRequestHostOf,
5 type GitRunStackedActionResult,
6 type OrchestrationProjectShell,
7 type SourceControlProviderKind,
8 type ThreadId,
9} from "@t3tools/contracts";
10import { parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";
11import * as Cause from "effect/Cause";
12import * as Effect from "effect/Effect";
13import * as Option from "effect/Option";
14​
15import { OrchestratorV2 } from "../orchestration-v2/Orchestrator.ts";
16import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";
17​
18export interface CreatedPullRequestKey {
19 readonly host: string;
20 readonly repository: string;
21 readonly number: number;
22 readonly url: string;
24​
25/**
26 * The identity a stacked action's pull request should be linked under, or
27 * null when the action did not leave one behind. Reading the host and
28 * repository from the URL keeps the link host-level even when the checkout's
29 * remote differs from where the PR was opened (a fork, say); the project is
30 * only consulted when the URL is one this cannot read.
31 */
32export function createdPullRequestKey(
33 result: Pick<GitRunStackedActionResult, "pr">,
34 project: OrchestrationProjectShell | undefined,
35): CreatedPullRequestKey | null {
36 const { status, number, url } = result.pr;
37 if ((status !== "created" && status !== "opened_existing") || number === undefined || !url) {
38 return null;
39 }
40 const parsed = parseChangeRequestUrl(url);
41 if (parsed !== null) return { ...parsed, url };
42 const identity = project?.repositoryIdentity;
43 const kind = identity?.provider as SourceControlProviderKind | undefined;
44 const repository = sourceControlRepositorySelector(identity);
45 if (!identity || kind === undefined || repository === null) return null;
46 return {
47 host: pullRequestHostOf(identity, kind),
48 repository: repository.toLowerCase(),
49 number,
50 url,
51 };
53​
54/**
55 * Links the pull request a `create_pr`-shaped action produced to the thread it
56 * ran beside. Never fails: the git action already succeeded and its result is
57 * on its way to the client, so a link that cannot be made is logged and
58 * dropped. A duplicate link is the decider saying the thread already knew.
59 */
60export const linkCreatedPullRequest = <E>(input: {
61 readonly threadId: ThreadId;
62 readonly result: Pick<GitRunStackedActionResult, "pr">;
63 readonly commandId: Effect.Effect<CommandId, E>;
64}): Effect.Effect<void, never, OrchestratorV2 | ProjectionSnapshotQuery.ProjectionSnapshotQuery> =>
65 Effect.gen(function* () {
66 const engine = yield* OrchestratorV2;
67​
68 const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
69 const thread = yield* engine
70 .getThreadShell(input.threadId)
71 .pipe(Effect.map(Option.fromNullishOr));
72 if (Option.isNone(thread)) return;
73 const project = Option.getOrUndefined(
74 yield* snapshots.getProjectShellById(thread.value.projectId),
75 );
76 const key = createdPullRequestKey(input.result, project);
77 if (key === null) return;
78 const commandId = yield* input.commandId;
79 yield* engine
80 .dispatch({
81 type: "thread.pull-request.link",
82 commandId,
83 threadId: input.threadId,
84 ...key,
85 source: "created",
86 })
87 .pipe(Effect.asVoid);
88 }).pipe(
89 Effect.withSpan("linkCreatedPullRequest"),
90 Effect.catchCause((cause) =>
91 Cause.hasInterruptsOnly(cause)
92 ? Effect.failCause(cause as Cause.Cause<never>)
93 : Effect.logWarning("failed to link created pull request to thread", {
94 threadId: input.threadId,
95 }),
96 ),
97 );