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