-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpullRequestProjectMatch.ts
More file actions
48 lines (40 loc) · 1.29 KB
/
pullRequestProjectMatch.ts
File metadata and controls
48 lines (40 loc) · 1.29 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
import type { Project } from "./types";
import { parsePullRequestReferenceParts } from "./pullRequestReference";
function lastPathSegment(input: string): string {
const segments = input.split(/[\\/]/).filter((segment) => segment.length > 0);
return segments.at(-1) ?? "";
}
function normalizeRepositorySlug(input: string): string {
return input
.trim()
.replace(/\.git$/i, "")
.replace(/[_\s]+/g, "-")
.replace(/-+/g, "-")
.toLowerCase();
}
function projectRepositoryCandidates(project: Project): string[] {
const candidates = [lastPathSegment(project.cwd), project.name]
.map(normalizeRepositorySlug)
.filter((candidate) => candidate.length > 0);
return [...new Set(candidates)];
}
export function findProjectMatchingPullRequestReference(
projects: readonly Project[],
reference: string,
): Project | null {
const parsed = parsePullRequestReferenceParts(reference);
if (parsed?.kind !== "url" || !parsed.repo) {
return null;
}
const targetRepository = normalizeRepositorySlug(parsed.repo);
if (targetRepository.length === 0) {
return null;
}
const matches = projects.filter((project) =>
projectRepositoryCandidates(project).includes(targetRepository),
);
if (matches.length !== 1) {
return null;
}
return matches[0] ?? null;
}