Repository navigation
Expand file tree
/
Copy pathpullRequestSyncKey.ts
More file actions
40 lines (39 loc) · 1.64 KB
/
Copy pathpullRequestSyncKey.ts
File metadata and controls
40 lines (39 loc) · 1.64 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
import type { PullRequestRef, RepositoryIdentity, ThreadPullRequestKey } from "@t3tools/contracts";
import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
import { normalizeThreadPullRequestKey } from "../sourceControl/threadPullRequests.ts";
export function pullRequestSyncKey(
reference: PullRequestRef,
identity?: RepositoryIdentity | null,
): ThreadPullRequestKey | null {
const host = identity
? sourceControlDefinitions.changeRequestHost(identity, identity.provider)
: undefined;
// A host whose tools select a repository by something other than its path (Azure DevOps takes
// the bare name) can only key such a reference through the project's own repository.
const definition = identity?.provider
? sourceControlDefinitions.find(identity.provider)
: undefined;
if (identity && definition?.repositorySelector && !reference.repository.includes("/")) {
if (
reference.repository.toLowerCase() !==
definition.repositorySelector(identity)?.toLowerCase() ||
(reference.host !== undefined && reference.host.toLowerCase() !== host)
)
return null;
const [projectHost, ...repository] = identity.canonicalKey.split("/");
if (!projectHost || repository.length === 0) return null;
return normalizeThreadPullRequestKey({
host: projectHost,
repository: repository.join("/"),
number: reference.number,
});
}
const resolvedHost = reference.host ?? host;
return resolvedHost === undefined
? null
: normalizeThreadPullRequestKey({
host: resolvedHost,
repository: reference.repository,
number: reference.number,
});
}