Open History to see the last commit
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 * as OrchestrationEngine from "../orchestration/Services/OrchestrationEngine.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;
23}
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 };
52}
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<
65 void,
66 never,
67 OrchestrationEngine.OrchestrationEngineService | ProjectionSnapshotQuery.ProjectionSnapshotQuery
68> =>
69 Effect.gen(function* () {
70 const engine = yield* OrchestrationEngine.OrchestrationEngineService;
71 const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
72 const thread = yield* snapshots.getThreadShellById(input.threadId);
73 if (Option.isNone(thread)) return;
74 const project = Option.getOrUndefined(
75 yield* snapshots.getProjectShellById(thread.value.projectId),
76 );
77 const key = createdPullRequestKey(input.result, project);
78 if (key === null) return;
79 const commandId = yield* input.commandId;
80 yield* engine
81 .dispatch({
82 type: "thread.pull-request.link",
83 commandId,
84 threadId: input.threadId,
85 ...key,
86 source: "created",
87 })
88 .pipe(Effect.catchTags({ OrchestrationCommandInvariantError: () => Effect.void }));
89 }).pipe(
90 Effect.withSpan("linkCreatedPullRequest"),
91 Effect.catchCause((cause) =>
92 Cause.hasInterruptsOnly(cause)
93 ? Effect.failCause(cause as Cause.Cause<never>)
94 : Effect.logWarning("failed to link created pull request to thread", {
95 threadId: input.threadId,
96 }),
97 ),
98 );