History

feat(pull-requests): link multiple pull requests to threads (#10839)

Julius Marminge committed 2 months ago

Commit afb84891 parent 383cc40172 files · +12057 −908Browse files

Showing the changes to apps/server/src/pullRequest/pullRequestSyncKey.ts (1 of 172 files). Show all

Unified · Split

apps/server/src/pullRequest/pullRequestSyncKey.ts+44−0
@@ -0,0 +1,44 @@
1import {
2 pullRequestHostOf,
3 type PullRequestRef,
4 type RepositoryIdentity,
5 type SourceControlProviderKind,
6 type ThreadPullRequestKey,
7} from "@t3tools/contracts";
8import { sourceControlRepositorySelector } from "@t3tools/shared/sourceControl";
9import { normalizeThreadPullRequestKey } from "@t3tools/shared/threadPullRequests";
10
11/** Convert checkout-scoped references to the host-level identity used by linked threads. */
12export function pullRequestSyncKey(
13 reference: PullRequestRef,
14 identity?: RepositoryIdentity | null,
15): ThreadPullRequestKey | null {
16 if (identity?.provider === "azure-devops" && !reference.repository.includes("/")) {
17 if (
18 reference.repository.toLowerCase() !==
19 sourceControlRepositorySelector(identity)?.toLowerCase() ||
20 (reference.host !== undefined &&
21 reference.host.toLowerCase() !== pullRequestHostOf(identity, "azure-devops"))
22 )
23 return null;
24 const [host, ...repository] = identity.canonicalKey.split("/");
25 if (!host || repository.length === 0) return null;
26 return normalizeThreadPullRequestKey({
27 host,
28 repository: repository.join("/"),
29 number: reference.number,
30 });
31 }
32 const host =
33 reference.host ??
34 (identity
35 ? pullRequestHostOf(identity, identity.provider as SourceControlProviderKind)
36 : undefined);
37 return host === undefined
38 ? null
39 : normalizeThreadPullRequestKey({
40 host,
41 repository: reference.repository,
42 number: reference.number,
43 });
44}