Open History to see the last commit
1import type { OrchestrationProjectShell, SourceControlProviderKind } from "@t3tools/contracts";
2import { canonicalizeGitCafePullRequestUrl } from "@t3tools/source-control-definitions/registry";
3import * as Effect from "effect/Effect";
4import * as Schema from "effect/Schema";
5
6import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
7import { normalizeThreadPullRequestKey } from "../sourceControl/threadPullRequests.ts";
8import { pullRequestSyncKey } from "./pullRequestSyncKey.ts";
9
10export interface PullRequestTarget {
11 readonly url?: string | undefined;
12 readonly repository?: string | undefined;
13 readonly number?: number | undefined;
14 readonly host?: string | undefined;
15}
16
17export class PullRequestUrlInvalidError extends Schema.TaggedError<PullRequestUrlInvalidError>()(
18 "PullRequestUrlInvalidError",
19 {},
20) {
21 override get message(): string {
22 return "This is not a recognised pull request URL. Pass repository and number instead.";
23 }
24}
25
26export class PullRequestTargetIncompleteError extends Schema.TaggedError<PullRequestTargetIncompleteError>()(
27 "PullRequestTargetIncompleteError",
28 {},
29) {
30 override get message(): string {
31 return "Pass either url, or both repository and number.";
32 }
33}
34
35export class PullRequestHostRequiredError extends Schema.TaggedError<PullRequestHostRequiredError>()(
36 "PullRequestHostRequiredError",
37 {},
38) {
39 override get message(): string {
40 return "This thread's project has no recognised remote. Pass host or url.";
41 }
42}
43
44export class PullRequestUrlUnavailableError extends Schema.TaggedError<PullRequestUrlUnavailableError>()(
45 "PullRequestUrlUnavailableError",
46 {},
47) {
48 override get message(): string {
49 return "This thread's project is on a host with no known pull request URL shape. Pass url.";
50 }
51}
52
53export class PullRequestRepositoryInvalidError extends Schema.TaggedError<PullRequestRepositoryInvalidError>()(
54 "PullRequestRepositoryInvalidError",
55 {},
56) {
57 override get message(): string {
58 return "Pass repository as owner/repo, without the host.";
59 }
60}
61
62export type PullRequestTargetError =
63 | PullRequestUrlInvalidError
64 | PullRequestTargetIncompleteError
65 | PullRequestHostRequiredError
66 | PullRequestUrlUnavailableError
67 | PullRequestRepositoryInvalidError;
68
69export interface ResolvedTarget {
70 readonly host: string;
71 readonly repository: string;
72 readonly number: number;
73 readonly url: string;
74}
75
76function projectHostAndProvider(project: OrchestrationProjectShell | undefined): {
77 readonly host: string | null;
78 readonly kind: SourceControlProviderKind | null;
79} {
80 const identity = project?.repositoryIdentity;
81 const kind = identity?.provider ?? null;
82 if (!identity || kind === null) return { host: null, kind: null };
83 return {
84 host: sourceControlDefinitions.changeRequestHost(identity, kind),
85 kind,
86 };
87}
88
89export const resolveTarget = Effect.fn("PullRequestsToolkit.resolveTarget")(function* (
90 input: PullRequestTarget,
91 project: OrchestrationProjectShell | undefined,
92) {
93 if (input.url !== undefined) {
94 const parsed = sourceControlDefinitions.parseChangeRequestUrl(input.url)?.link;
95 if (parsed === undefined) {
96 return yield* new PullRequestUrlInvalidError({});
97 }
98 return {
99 ...normalizeThreadPullRequestKey(parsed),
100 url: canonicalizeGitCafePullRequestUrl(input.url),
101 } satisfies ResolvedTarget;
102 }
103 if (input.repository === undefined || input.number === undefined) {
104 return yield* new PullRequestTargetIncompleteError({});
105 }
106 // Azure DevOps tools name the project's repository by its bare name, which no URL can be built
107 // from, so it is read as the project's full repository path.
108 const own =
109 project?.repositoryIdentity == null || input.repository.includes("/")
110 ? null
111 : pullRequestSyncKey(
112 {
113 projectId: project.id,
114 host: input.host,
115 repository: input.repository,
116 number: input.number,
117 },
118 project.repositoryIdentity,
119 );
120 const projectHost = projectHostAndProvider(project);
121 const host = (own?.host ?? input.host ?? projectHost.host)?.toLowerCase();
122 if (host === undefined) {
123 return yield* new PullRequestHostRequiredError({});
124 }
125 const repository = (own?.repository ?? input.repository).toLowerCase();
126 // Without a URL, a public forge host says what shape its change request URLs take;
127 // any other host is taken to run the project's own forge.
128 const recognizedKind = sourceControlDefinitions.recognizeRemote(`https://${host}`)?.kind;
129 const kind =
130 sourceControlDefinitions.findByPublicHost(host)?.kind ??
131 (recognizedKind === "gitcafe" ? recognizedKind : projectHost.kind);
132 const definition = kind === null ? null : sourceControlDefinitions.get(kind);
133 const url =
134 definition?.changeRequestUrl({
135 host,
136 repository,
137 number: input.number,
138 remoteUrl: project?.repositoryIdentity?.locator.remoteUrl,
139 }) ?? null;
140 if (url === null) {
141 // A known forge returns no URL when the repository is not in its shape.
142 return yield* definition === null || definition.kind === "unknown"
143 ? new PullRequestUrlUnavailableError({})
144 : new PullRequestRepositoryInvalidError({});
145 }
146 return {
147 ...normalizeThreadPullRequestKey({ host, repository, number: input.number, url }),
148 url: canonicalizeGitCafePullRequestUrl(url),
149 } satisfies ResolvedTarget;
150});