Skip to content

Commit 76210db

Browse files
authored
feat(plugin-testops): attach git information on launch create (#658)
1 parent 850e4f3 commit 76210db

58 files changed

Lines changed: 4054 additions & 198 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.pnp.cjs

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ci/src/detectors/amazon.ts

Lines changed: 144 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { type CiDescriptor, CiType } from "@allurereport/core-api";
22

3+
import { resolveRepositoryFromGitUrl, stripRefsHeads } from "../helpers/gitProvider.js";
34
import { getEnv, getReponameFromRepoUrl } from "../utils.js";
45

56
const AMAZON_REGEXP = /^arn:aws:codebuild:([^:]+):([\d]+):(?:build|build-batch)\/([^:]+):([\da-f-]+)$/;
67
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;
712

813
export const parseArnValues = (source: string): string[] => {
914
if (!source) {
@@ -35,6 +40,103 @@ export const getPipelineName = (): string => {
3540
return initiator.match(PIPELINE_REGEXP)?.[1] ?? "";
3641
};
3742

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+
59+
const parseBranchFromSourceVersion = (sourceVersion?: string): string | undefined => {
60+
const normalizedSourceVersion = stripCommitSuffix((sourceVersion ?? "").trim());
61+
62+
if (!normalizedSourceVersion) {
63+
return undefined;
64+
}
65+
66+
if (normalizedSourceVersion.startsWith(BRANCH_REF_PREFIX)) {
67+
const branch = normalizedSourceVersion.slice(BRANCH_REF_PREFIX.length);
68+
69+
return branch || undefined;
70+
}
71+
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+
}
79+
80+
if (normalizedSourceVersion.startsWith(REF_PREFIX)) {
81+
return undefined;
82+
}
83+
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;
115+
};
116+
117+
const parsePullRequestFromSourceVersion = (sourceVersion?: string): string | undefined => {
118+
const normalizedSourceVersion = sourceVersion ?? "";
119+
const prPrefixMatch = normalizedSourceVersion.match(/^pr\/(?<id>\d+)$/i)?.groups?.id;
120+
121+
if (prPrefixMatch) {
122+
return prPrefixMatch;
123+
}
124+
125+
return normalizedSourceVersion.match(/refs\/pull\/(?<id>\d+)\//)?.groups?.id;
126+
};
127+
128+
const parsePullRequestFromWebhookTrigger = (): string | undefined => {
129+
const trigger = getEnv("CODEBUILD_WEBHOOK_TRIGGER") || "";
130+
131+
return trigger.match(/^pr\/(?<id>\d+)$/i)?.groups?.id;
132+
};
133+
134+
const getRepository = () => {
135+
const repoUrl = getEnv("CODEBUILD_SOURCE_REPO_URL");
136+
137+
return repoUrl ? resolveRepositoryFromGitUrl(repoUrl) : undefined;
138+
};
139+
38140
export const amazon: CiDescriptor = {
39141
type: CiType.Amazon,
40142

@@ -151,9 +253,8 @@ export const amazon: CiDescriptor = {
151253

152254
get jobRunBranch(): string {
153255
const sourceVersion = getEnv("CODEBUILD_SOURCE_VERSION");
154-
const { branch } = sourceVersion?.match?.(/refs\/heads\/(?<branch>\S+)\^\{(?<commithash>\S+)\}/)?.groups ?? {};
155256

156-
return branch ?? "";
257+
return parseBranchFromSourceVersion(sourceVersion) ?? "";
157258
},
158259

159260
get pullRequestUrl(): string {
@@ -163,4 +264,45 @@ export const amazon: CiDescriptor = {
163264
get pullRequestName(): string {
164265
return "";
165266
},
267+
268+
get provider() {
269+
return getRepository()?.provider;
270+
},
271+
272+
get repository() {
273+
const repository = getRepository();
274+
275+
return repository
276+
? {
277+
slug: repository.slug,
278+
url: repository.url,
279+
}
280+
: undefined;
281+
},
282+
283+
get sourceBranch() {
284+
return (
285+
parseBranchFromWebhookHeadRef(getEnv("CODEBUILD_WEBHOOK_HEAD_REF")) ||
286+
parseBranchFromSourceVersion(getEnv("CODEBUILD_SOURCE_VERSION")) ||
287+
this.jobRunBranch ||
288+
undefined
289+
);
290+
},
291+
292+
get targetBranch() {
293+
return stripRefsHeads(getEnv("CODEBUILD_WEBHOOK_BASE_REF")) || undefined;
294+
},
295+
296+
get pullRequest() {
297+
const pullRequestId =
298+
parsePullRequestFromWebhookTrigger() || parsePullRequestFromSourceVersion(getEnv("CODEBUILD_SOURCE_VERSION"));
299+
300+
return pullRequestId
301+
? {
302+
id: pullRequestId,
303+
url: this.pullRequestUrl || undefined,
304+
title: this.pullRequestName || undefined,
305+
}
306+
: undefined;
307+
},
166308
};

packages/ci/src/detectors/azure.ts

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

3+
import { resolveRepositoryFromGitUrl } from "../helpers/gitProvider.js";
34
import { getEnv } from "../utils.js";
45

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

712
export const getBuildID = (): string => getEnv("BUILD_BUILDID");
@@ -10,6 +15,59 @@ export const getDefinitionID = (): string => getEnv("SYSTEM_DEFINITIONID");
1015

1116
export const getProjectID = (): string => getEnv("SYSTEM_TEAMPROJECTID");
1217

18+
const mapAzureRepositoryProvider = (provider: string): GitProvider | undefined => {
19+
switch (provider) {
20+
case "GitHub":
21+
return GitProvider.Github;
22+
case "Bitbucket":
23+
return GitProvider.Bitbucket;
24+
default:
25+
return undefined;
26+
}
27+
};
28+
29+
const normalizeBranchRef = (branch?: string): string | undefined => {
30+
const trimmed = branch?.trim() ?? "";
31+
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;
61+
};
62+
63+
const getRepositoryUrl = () => getEnv("BUILD_REPOSITORY_URI") || getEnv("SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI");
64+
65+
const getRepositoryFromUrl = () => {
66+
const repositoryUrl = getRepositoryUrl();
67+
68+
return repositoryUrl ? resolveRepositoryFromGitUrl(repositoryUrl) : undefined;
69+
};
70+
1371
export const azure: CiDescriptor = {
1472
type: CiType.Azure,
1573

@@ -48,20 +106,24 @@ export const azure: CiDescriptor = {
48106
},
49107

50108
get jobRunBranch(): string {
51-
return getEnv("BUILD_SOURCEBRANCHNAME");
109+
return getSourceBranch() ?? "";
52110
},
53111

54112
get pullRequestUrl(): string {
55113
const repositoryProvider = getEnv("BUILD_REPOSITORY_PROVIDER");
56-
const repositoryUrl = getEnv("SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI");
57-
const pullRequestNumber = getEnv("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER");
114+
const repositoryUrl = getEnv("SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI") || getEnv("BUILD_REPOSITORY_URI");
115+
const pullRequestId = getPullRequestId();
116+
117+
if (!repositoryUrl || !pullRequestId) {
118+
return "";
119+
}
58120

59121
if (repositoryProvider === "GitHub") {
60-
return `${repositoryUrl}/pull/${pullRequestNumber}`;
122+
return `${repositoryUrl}/pull/${pullRequestId}`;
61123
}
62124

63125
if (repositoryProvider === "TfsGit" || repositoryProvider === "TfsVersionControl") {
64-
return `${repositoryUrl}/pullrequest/${pullRequestNumber}`;
126+
return `${repositoryUrl}/pullrequest/${pullRequestId}`;
65127
}
66128

67129
return "";
@@ -70,4 +132,47 @@ export const azure: CiDescriptor = {
70132
get pullRequestName(): string {
71133
return "";
72134
},
135+
136+
get provider() {
137+
return getRepositoryFromUrl()?.provider ?? mapAzureRepositoryProvider(getEnv("BUILD_REPOSITORY_PROVIDER"));
138+
},
139+
140+
get repository() {
141+
const repositoryUrl = getRepositoryUrl();
142+
const fromUrl = repositoryUrl ? resolveRepositoryFromGitUrl(repositoryUrl) : undefined;
143+
const slug = fromUrl?.slug ?? (getEnv("BUILD_REPOSITORY_NAME") || undefined);
144+
145+
if (!this.provider || !slug) {
146+
return undefined;
147+
}
148+
149+
return {
150+
slug,
151+
url: fromUrl?.url ?? repositoryUrl,
152+
};
153+
},
154+
155+
get sourceBranch() {
156+
return normalizeBranchRef(getEnv("SYSTEM_PULLREQUEST_SOURCEBRANCH")) || getSourceBranch() || undefined;
157+
},
158+
159+
get targetBranch() {
160+
return (
161+
normalizeBranchRef(getEnv("SYSTEM_PULLREQUEST_TARGETBRANCH")) ||
162+
normalizeBranchRef(getEnv("SYSTEM_PULLREQUEST_TARGETBRANCHNAME")) ||
163+
undefined
164+
);
165+
},
166+
167+
get pullRequest() {
168+
const pullRequestNumber = getPullRequestId();
169+
170+
return pullRequestNumber
171+
? {
172+
id: pullRequestNumber,
173+
url: this.pullRequestUrl || undefined,
174+
title: this.pullRequestName || undefined,
175+
}
176+
: undefined;
177+
},
73178
};

0 commit comments

Comments
 (0)