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 Orchestrator from "../orchestration-v2/Orchestrator.ts";
16import * as ProjectService from "../project/ProjectService.ts";
17
18export interface CreatedPullRequestKey {
19 readonly host: string;
20 readonly repository: string;
21 readonly number: number;
22 readonly url: string;
23}
24
25export function createdPullRequestKey(
26 result: Pick<GitRunStackedActionResult, "pr">,
27 project: OrchestrationProjectShell | undefined,
28): CreatedPullRequestKey | null {
29 const { status, number, url } = result.pr;
30 if ((status !== "created" && status !== "opened_existing") || number === undefined || !url) {
31 return null;
32 }
33 const parsed = parseChangeRequestUrl(url);
34 if (parsed !== null) return { ...parsed, url };
35 const identity = project?.repositoryIdentity;
36 const kind = identity?.provider as SourceControlProviderKind | undefined;
37 const repository = sourceControlRepositorySelector(identity);
38 if (!identity || kind === undefined || repository === null) return null;
39 return {
40 host: pullRequestHostOf(identity, kind),
41 repository: repository.toLowerCase(),
42 number,
43 url,
44 };
45}
46
47/**
48 * Never fails: the git action already succeeded and its result is on its way to the client, so
49 * a link that cannot be made is logged and dropped.
50 */
51export const linkCreatedPullRequest: <E>(input: {
52 readonly threadId: ThreadId;
53 readonly result: Pick<GitRunStackedActionResult, "pr">;
54 readonly commandId: Effect.Effect<CommandId, E>;
55}) => Effect.Effect<void, never, Orchestrator.OrchestratorV2 | ProjectService.ProjectService> =
56 Effect.fn("linkCreatedPullRequest")(
57 function* <E>(input: {
58 readonly threadId: ThreadId;
59 readonly result: Pick<GitRunStackedActionResult, "pr">;
60 readonly commandId: Effect.Effect<CommandId, E>;
61 }) {
62 const engine = yield* Orchestrator.OrchestratorV2;
63 const projects = yield* ProjectService.ProjectService;
64 const thread = yield* engine
65 .getThreadShell(input.threadId)
66 .pipe(Effect.map(Option.fromNullishOr));
67 if (Option.isNone(thread)) return;
68 const project = Option.getOrUndefined(yield* projects.getShell(thread.value.projectId));
69 const key = createdPullRequestKey(input.result, project);
70 if (key === null) return;
71 const commandId = yield* input.commandId;
72 yield* engine
73 .dispatch({
74 type: "thread.pull-request.link",
75 commandId,
76 threadId: input.threadId,
77 ...key,
78 source: "created",
79 })
80 .pipe(Effect.asVoid);
81 },
82 (effect, input) =>
83 effect.pipe(
84 Effect.catchCause((cause) =>
85 Cause.hasInterruptsOnly(cause)
86 ? Effect.failCause(cause as Cause.Cause<never>)
87 : Effect.logWarning("failed to link created pull request to thread", {
88 threadId: input.threadId,
89 }),
90 ),
91 ),
92 );