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

pullRequestTarget.ts

177 lines6.3 KB

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