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

linkCreatedPullRequest.ts

85 lines3.0 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 * 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;
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 };
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.gen(function* () {
57 const engine = yield* Orchestrator.OrchestratorV2;
58 const projects = yield* ProjectService.ProjectService;
59 const thread = yield* engine
60 .getThreadShell(input.threadId)
61 .pipe(Effect.map(Option.fromNullishOr));
62 if (Option.isNone(thread)) return;
63 const project = Option.getOrUndefined(yield* projects.getShell(thread.value.projectId));
64 const key = createdPullRequestKey(input.result, project);
65 if (key === null) return;
66 const commandId = yield* input.commandId;
67 yield* engine
68 .dispatch({
69 type: "thread.pull-request.link",
70 commandId,
71 threadId: input.threadId,
72 ...key,
73 source: "created",
74 })
75 .pipe(Effect.asVoid);
76 }).pipe(
77 Effect.withSpan("linkCreatedPullRequest"),
78 Effect.catchCause((cause) =>
79 Cause.hasInterruptsOnly(cause)
80 ? Effect.failCause(cause as Cause.Cause<never>)
81 : Effect.logWarning("failed to link created pull request to thread", {
82 threadId: input.threadId,
83 }),
84 ),
85 );