Skip to content

Commit e69f668

Browse files
committed
chore: review
1 parent 24712ac commit e69f668

22 files changed

Lines changed: 1213 additions & 134 deletions

packages/ci/src/detectors/amazon.ts

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { getEnv, getReponameFromRepoUrl } from "../utils.js";
55

66
const AMAZON_REGEXP = /^arn:aws:codebuild:([^:]+):([\d]+):(?:build|build-batch)\/([^:]+):([\da-f-]+)$/;
77
const PIPELINE_REGEXP = /^(?:codepipeline\/)(.+)$/;
8+
const REF_PREFIX = "refs/";
9+
const BRANCH_REF_PREFIX = "refs/heads/";
10+
const TAG_REF_PREFIX = "refs/tags/";
11+
const GIT_SHA_REGEXP = /^[\da-f]{40}$/i;
812

913
export const parseArnValues = (source: string): string[] => {
1014
if (!source) {
@@ -36,23 +40,78 @@ export const getPipelineName = (): string => {
3640
return initiator.match(PIPELINE_REGEXP)?.[1] ?? "";
3741
};
3842

43+
const stripCommitSuffix = (sourceVersion: string): string => sourceVersion.replace(/\^\{[^}]+\}$/, "");
44+
45+
const parseBranchFromWebhookTrigger = (): string | undefined => {
46+
const branchTrigger = getEnv("CODEBUILD_WEBHOOK_TRIGGER") || "";
47+
48+
if (branchTrigger.startsWith("branch/")) {
49+
const branch = branchTrigger.slice("branch/".length);
50+
51+
return branch || undefined;
52+
}
53+
54+
return undefined;
55+
};
56+
57+
const isTagWebhookTrigger = (): boolean => (getEnv("CODEBUILD_WEBHOOK_TRIGGER") || "").startsWith("tag/");
58+
3959
const parseBranchFromSourceVersion = (sourceVersion?: string): string | undefined => {
40-
const prefix = "refs/heads/";
41-
const normalizedSourceVersion = sourceVersion ?? "";
60+
const normalizedSourceVersion = stripCommitSuffix((sourceVersion ?? "").trim());
4261

43-
if (normalizedSourceVersion.startsWith(prefix)) {
44-
const branch = normalizedSourceVersion.slice(prefix.length).replace(/\^\{[^}]+\}$/, "");
62+
if (!normalizedSourceVersion) {
63+
return undefined;
64+
}
65+
66+
if (normalizedSourceVersion.startsWith(BRANCH_REF_PREFIX)) {
67+
const branch = normalizedSourceVersion.slice(BRANCH_REF_PREFIX.length);
4568

4669
return branch || undefined;
4770
}
4871

49-
const branchTrigger = getEnv("CODEBUILD_WEBHOOK_TRIGGER") || "";
72+
if (
73+
normalizedSourceVersion.startsWith(TAG_REF_PREFIX) ||
74+
normalizedSourceVersion.match(/^pr\/\d+$/i) ||
75+
normalizedSourceVersion.match(/^refs\/pull\/\d+\//i)
76+
) {
77+
return undefined;
78+
}
5079

51-
if (branchTrigger.startsWith("branch/")) {
52-
return branchTrigger.slice("branch/".length);
80+
if (normalizedSourceVersion.startsWith(REF_PREFIX)) {
81+
return undefined;
5382
}
5483

55-
return undefined;
84+
const triggerBranch = parseBranchFromWebhookTrigger();
85+
86+
if (triggerBranch) {
87+
return triggerBranch;
88+
}
89+
90+
if (isTagWebhookTrigger() || GIT_SHA_REGEXP.test(normalizedSourceVersion)) {
91+
return undefined;
92+
}
93+
94+
return normalizedSourceVersion;
95+
};
96+
97+
const parseBranchFromWebhookHeadRef = (headRef?: string): string | undefined => {
98+
const normalizedHeadRef = (headRef ?? "").trim();
99+
100+
if (!normalizedHeadRef || normalizedHeadRef.startsWith(TAG_REF_PREFIX) || isTagWebhookTrigger()) {
101+
return undefined;
102+
}
103+
104+
if (normalizedHeadRef.startsWith(BRANCH_REF_PREFIX)) {
105+
const branch = normalizedHeadRef.slice(BRANCH_REF_PREFIX.length);
106+
107+
return branch || undefined;
108+
}
109+
110+
if (normalizedHeadRef.startsWith(REF_PREFIX)) {
111+
return undefined;
112+
}
113+
114+
return normalizedHeadRef;
56115
};
57116

58117
const parsePullRequestFromSourceVersion = (sourceVersion?: string): string | undefined => {
@@ -222,10 +281,8 @@ export const amazon: CiDescriptor = {
222281
},
223282

224283
get sourceBranch() {
225-
const headRef = getEnv("CODEBUILD_WEBHOOK_HEAD_REF");
226-
227284
return (
228-
(headRef ? stripRefsHeads(headRef) : undefined) ||
285+
parseBranchFromWebhookHeadRef(getEnv("CODEBUILD_WEBHOOK_HEAD_REF")) ||
229286
parseBranchFromSourceVersion(getEnv("CODEBUILD_SOURCE_VERSION")) ||
230287
this.jobRunBranch ||
231288
undefined

packages/ci/src/detectors/azure.ts

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { type CiDescriptor, CiType, GitProvider } from "@allurereport/core-api";
22

3-
import { resolveRepositoryFromGitUrl, stripRefsHeads } from "../helpers/gitProvider.js";
3+
import { resolveRepositoryFromGitUrl } from "../helpers/gitProvider.js";
44
import { getEnv } from "../utils.js";
55

6+
const REF_PREFIX = "refs/";
7+
const BRANCH_REF_PREFIX = "refs/heads/";
8+
const TAG_REF_PREFIX = "refs/tags/";
9+
610
export const getRootURL = (): string => getEnv("SYSTEM_COLLECTIONURI");
711

812
export const getBuildID = (): string => getEnv("BUILD_BUILDID");
@@ -25,7 +29,35 @@ const mapAzureRepositoryProvider = (provider: string): GitProvider | undefined =
2529
const normalizeBranchRef = (branch?: string): string | undefined => {
2630
const trimmed = branch?.trim() ?? "";
2731

28-
return trimmed ? stripRefsHeads(trimmed) : undefined;
32+
if (!trimmed || trimmed.startsWith(TAG_REF_PREFIX)) {
33+
return undefined;
34+
}
35+
36+
if (trimmed.startsWith(BRANCH_REF_PREFIX)) {
37+
const branchName = trimmed.slice(BRANCH_REF_PREFIX.length);
38+
39+
return branchName || undefined;
40+
}
41+
42+
if (trimmed.startsWith(REF_PREFIX)) {
43+
return undefined;
44+
}
45+
46+
return trimmed;
47+
};
48+
49+
const getSourceBranch = (): string | undefined => {
50+
const sourceBranch = getEnv("BUILD_SOURCEBRANCH");
51+
52+
if (sourceBranch) {
53+
return normalizeBranchRef(sourceBranch);
54+
}
55+
56+
return normalizeBranchRef(getEnv("BUILD_SOURCEBRANCHNAME"));
57+
};
58+
59+
const getPullRequestId = (): string | undefined => {
60+
return getEnv("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER") || getEnv("SYSTEM_PULLREQUEST_PULLREQUESTID") || undefined;
2961
};
3062

3163
const getRepositoryUrl = () => getEnv("BUILD_REPOSITORY_URI") || getEnv("SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI");
@@ -74,24 +106,24 @@ export const azure: CiDescriptor = {
74106
},
75107

76108
get jobRunBranch(): string {
77-
return getEnv("BUILD_SOURCEBRANCHNAME");
109+
return getSourceBranch() ?? "";
78110
},
79111

80112
get pullRequestUrl(): string {
81113
const repositoryProvider = getEnv("BUILD_REPOSITORY_PROVIDER");
82114
const repositoryUrl = getEnv("SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI") || getEnv("BUILD_REPOSITORY_URI");
83-
const pullRequestNumber = getEnv("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER");
115+
const pullRequestId = getPullRequestId();
84116

85-
if (!repositoryUrl || !pullRequestNumber) {
117+
if (!repositoryUrl || !pullRequestId) {
86118
return "";
87119
}
88120

89121
if (repositoryProvider === "GitHub") {
90-
return `${repositoryUrl}/pull/${pullRequestNumber}`;
122+
return `${repositoryUrl}/pull/${pullRequestId}`;
91123
}
92124

93125
if (repositoryProvider === "TfsGit" || repositoryProvider === "TfsVersionControl") {
94-
return `${repositoryUrl}/pullrequest/${pullRequestNumber}`;
126+
return `${repositoryUrl}/pullrequest/${pullRequestId}`;
95127
}
96128

97129
return "";
@@ -121,25 +153,19 @@ export const azure: CiDescriptor = {
121153
},
122154

123155
get sourceBranch() {
124-
return (
125-
normalizeBranchRef(getEnv("SYSTEM_PULLREQUEST_SOURCEBRANCH")) ||
126-
getEnv("BUILD_SOURCEBRANCHNAME") ||
127-
this.jobRunBranch ||
128-
undefined
129-
);
156+
return normalizeBranchRef(getEnv("SYSTEM_PULLREQUEST_SOURCEBRANCH")) || getSourceBranch() || undefined;
130157
},
131158

132159
get targetBranch() {
133160
return (
134161
normalizeBranchRef(getEnv("SYSTEM_PULLREQUEST_TARGETBRANCH")) ||
135-
getEnv("SYSTEM_PULLREQUEST_TARGETBRANCHNAME") ||
162+
normalizeBranchRef(getEnv("SYSTEM_PULLREQUEST_TARGETBRANCHNAME")) ||
136163
undefined
137164
);
138165
},
139166

140167
get pullRequest() {
141-
const pullRequestNumber =
142-
getEnv("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER") || getEnv("SYSTEM_PULLREQUEST_PULLREQUESTID");
168+
const pullRequestNumber = getPullRequestId();
143169

144170
return pullRequestNumber
145171
? {

packages/ci/src/detectors/bitbucket.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,32 @@ import { type CiDescriptor, CiType, GitProvider } from "@allurereport/core-api";
22

33
import { getEnv } from "../utils.js";
44

5+
const stripGitSuffix = (url: string): string => url.replace(/\.git\/?$/i, "");
6+
7+
const getRepositoryUrl = (): string => {
8+
const origin = (getEnv("BITBUCKET_GIT_HTTP_ORIGIN") || "").trim();
9+
10+
if (!origin) {
11+
return "";
12+
}
13+
14+
try {
15+
const url = new URL(origin);
16+
17+
url.username = "";
18+
url.password = "";
19+
url.pathname = stripGitSuffix(url.pathname).replace(/\/+$/, "");
20+
url.search = "";
21+
url.hash = "";
22+
23+
return url.toString().replace(/\/$/, "");
24+
} catch {
25+
return stripGitSuffix(origin).replace(/\/+$/, "");
26+
}
27+
};
28+
529
export const getJobURL = (): string => {
6-
const origin = getEnv("BITBUCKET_GIT_HTTP_ORIGIN");
30+
const origin = getRepositoryUrl();
731

832
return `${origin}/pipelines`;
933
};
@@ -54,7 +78,11 @@ export const bitbucket: CiDescriptor = {
5478
return "";
5579
}
5680

57-
const origin = getEnv("BITBUCKET_GIT_HTTP_ORIGIN");
81+
const origin = getRepositoryUrl();
82+
83+
if (!origin) {
84+
return "";
85+
}
5886

5987
return `${origin}/pull-requests/${prId}`;
6088
},
@@ -73,7 +101,7 @@ export const bitbucket: CiDescriptor = {
73101
return repositorySlug
74102
? {
75103
slug: repositorySlug,
76-
url: getEnv("BITBUCKET_GIT_HTTP_ORIGIN") || undefined,
104+
url: getRepositoryUrl() || undefined,
77105
}
78106
: undefined;
79107
},

packages/ci/src/detectors/circle.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@ const getRepository = () => {
2020
return repositoryUrl ? resolveRepositoryFromGitUrl(repositoryUrl) : undefined;
2121
};
2222

23+
const getPullRequestUrl = (): string => {
24+
const pullRequestUrl = getEnv("CIRCLE_PULL_REQUEST").trim();
25+
26+
if (pullRequestUrl) {
27+
return pullRequestUrl;
28+
}
29+
30+
return (
31+
getEnv("CIRCLE_PULL_REQUESTS")
32+
.split(",")
33+
.map((url) => url.trim())
34+
.find(Boolean) ?? ""
35+
);
36+
};
37+
38+
const getPullRequestNumber = (): string | undefined => {
39+
const pullRequestUrl = getPullRequestUrl();
40+
const pullRequestNumber = pullRequestUrl ? parsePullRequestNumberFromUrl(pullRequestUrl) : undefined;
41+
42+
return pullRequestNumber || getEnv("CIRCLE_PR_NUMBER") || undefined;
43+
};
44+
2345
export const circle: CiDescriptor = {
2446
type: CiType.Circle,
2547

@@ -46,7 +68,7 @@ export const circle: CiDescriptor = {
4668
},
4769

4870
get jobName(): string {
49-
const username = getEnv("CIRCLE_USERNAME");
71+
const username = getEnv("CIRCLE_PROJECT_USERNAME") || getEnv("CIRCLE_USERNAME");
5072
const reponame = getEnv("CIRCLE_PROJECT_REPONAME");
5173

5274
return `${username}/${reponame}`;
@@ -69,7 +91,7 @@ export const circle: CiDescriptor = {
6991
},
7092

7193
get pullRequestUrl(): string {
72-
return "";
94+
return getPullRequestUrl();
7395
},
7496

7597
get pullRequestName(): string {
@@ -96,8 +118,8 @@ export const circle: CiDescriptor = {
96118
},
97119

98120
get pullRequest() {
99-
const pullRequestUrl = getEnv("CIRCLE_PULL_REQUEST");
100-
const pullRequestNumber = pullRequestUrl ? parsePullRequestNumberFromUrl(pullRequestUrl) : undefined;
121+
const pullRequestUrl = getPullRequestUrl();
122+
const pullRequestNumber = getPullRequestNumber();
101123

102124
return pullRequestNumber
103125
? {

0 commit comments

Comments
 (0)