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

linkCreatedPullRequest.ts

104 lines3.6 KB

1import {
2 type CommandId,
3 type GitRunStackedActionResult,
4 type RepositoryIdentity,
5 type ThreadId,
6} from "@t3tools/contracts";
7import * as Cause from "effect/Cause";
8import * as Effect from "effect/Effect";
9import * as Option from "effect/Option";
10​
11import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
12import * as ProjectService from "../project/ProjectService.ts";
13import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
14import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
15​
16export interface CreatedPullRequestKey {
17 readonly host: string;
18 readonly repository: string;
19 readonly number: number;
20 readonly url: string;
22​
23export function createdPullRequestKey(
24 result: Pick<GitRunStackedActionResult, "pr">,
25 /** Of the repository the action ran in, which a tracked repository does not share with its project. */
26 identity: RepositoryIdentity | null | undefined,
27): CreatedPullRequestKey | null {
28 const { status, number, url } = result.pr;
29 if ((status !== "created" && status !== "opened_existing") || number === undefined || !url) {
30 return null;
31 }
32 const parsed = sourceControlDefinitions.parseChangeRequestUrl(url)?.link;
33 if (parsed !== undefined) {
34 return { host: parsed.host, repository: parsed.repository, number: parsed.number, url };
35 }
36 const kind = identity?.provider;
37 const repository = sourceControlDefinitions.repositorySelector(identity);
38 if (!identity || kind === undefined || repository === null) return null;
39 return {
40 host: sourceControlDefinitions.changeRequestHost(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 /** Where the action ran. */
54 readonly cwd: string;
55 readonly result: Pick<GitRunStackedActionResult, "pr">;
56 readonly commandId: Effect.Effect<CommandId, E>;
57}) => Effect.Effect<
58 void,
59 never,
60 | Orchestrator.OrchestratorV2
61 | ProjectService.ProjectService
62 | RepositoryIdentityResolver.RepositoryIdentityResolver
63> = Effect.fn("linkCreatedPullRequest")(
64 function* <E>(input: {
65 readonly threadId: ThreadId;
66 readonly cwd: string;
67 readonly result: Pick<GitRunStackedActionResult, "pr">;
68 readonly commandId: Effect.Effect<CommandId, E>;
69 }) {
70 const identities = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
71 const engine = yield* Orchestrator.OrchestratorV2;
72 const projects = yield* ProjectService.ProjectService;
73 const thread = yield* engine
74 .getThreadShell(input.threadId)
75 .pipe(Effect.map(Option.fromNullishOr));
76 if (Option.isNone(thread)) return;
77 const project = Option.getOrUndefined(yield* projects.getShell(thread.value.projectId));
78 const key = createdPullRequestKey(
79 input.result,
80 (yield* identities.resolve(input.cwd)) ?? project?.repositoryIdentity,
81 );
82 if (key === null) return;
83 const commandId = yield* input.commandId;
84 yield* engine
85 .dispatch({
86 type: "thread.pull-request.link",
87 commandId,
88 threadId: input.threadId,
89 ...key,
90 source: "created",
91 })
92 .pipe(Effect.asVoid);
93 },
94 (effect, input) =>
95 effect.pipe(
96 Effect.catchCause((cause) =>
97 Cause.hasInterruptsOnly(cause)
98 ? Effect.failCause(cause as Cause.Cause<never>)
99 : Effect.logWarning("failed to link created pull request to thread", {
100 threadId: input.threadId,
101 }),
102 ),
103 ),
104);