Skip to content

Commit 24712ac

Browse files
committed
feat(plugin-testops): attach git information on launch create
1 parent d34a53f commit 24712ac

51 files changed

Lines changed: 2794 additions & 125 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: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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-]+)$/;
@@ -35,6 +36,48 @@ export const getPipelineName = (): string => {
3536
return initiator.match(PIPELINE_REGEXP)?.[1] ?? "";
3637
};
3738

39+
const parseBranchFromSourceVersion = (sourceVersion?: string): string | undefined => {
40+
const prefix = "refs/heads/";
41+
const normalizedSourceVersion = sourceVersion ?? "";
42+
43+
if (normalizedSourceVersion.startsWith(prefix)) {
44+
const branch = normalizedSourceVersion.slice(prefix.length).replace(/\^\{[^}]+\}$/, "");
45+
46+
return branch || undefined;
47+
}
48+
49+
const branchTrigger = getEnv("CODEBUILD_WEBHOOK_TRIGGER") || "";
50+
51+
if (branchTrigger.startsWith("branch/")) {
52+
return branchTrigger.slice("branch/".length);
53+
}
54+
55+
return undefined;
56+
};
57+
58+
const parsePullRequestFromSourceVersion = (sourceVersion?: string): string | undefined => {
59+
const normalizedSourceVersion = sourceVersion ?? "";
60+
const prPrefixMatch = normalizedSourceVersion.match(/^pr\/(?<id>\d+)$/i)?.groups?.id;
61+
62+
if (prPrefixMatch) {
63+
return prPrefixMatch;
64+
}
65+
66+
return normalizedSourceVersion.match(/refs\/pull\/(?<id>\d+)\//)?.groups?.id;
67+
};
68+
69+
const parsePullRequestFromWebhookTrigger = (): string | undefined => {
70+
const trigger = getEnv("CODEBUILD_WEBHOOK_TRIGGER") || "";
71+
72+
return trigger.match(/^pr\/(?<id>\d+)$/i)?.groups?.id;
73+
};
74+
75+
const getRepository = () => {
76+
const repoUrl = getEnv("CODEBUILD_SOURCE_REPO_URL");
77+
78+
return repoUrl ? resolveRepositoryFromGitUrl(repoUrl) : undefined;
79+
};
80+
3881
export const amazon: CiDescriptor = {
3982
type: CiType.Amazon,
4083

@@ -151,9 +194,8 @@ export const amazon: CiDescriptor = {
151194

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

156-
return branch ?? "";
198+
return parseBranchFromSourceVersion(sourceVersion) ?? "";
157199
},
158200

159201
get pullRequestUrl(): string {
@@ -163,4 +205,47 @@ export const amazon: CiDescriptor = {
163205
get pullRequestName(): string {
164206
return "";
165207
},
208+
209+
get provider() {
210+
return getRepository()?.provider;
211+
},
212+
213+
get repository() {
214+
const repository = getRepository();
215+
216+
return repository
217+
? {
218+
slug: repository.slug,
219+
url: repository.url,
220+
}
221+
: undefined;
222+
},
223+
224+
get sourceBranch() {
225+
const headRef = getEnv("CODEBUILD_WEBHOOK_HEAD_REF");
226+
227+
return (
228+
(headRef ? stripRefsHeads(headRef) : undefined) ||
229+
parseBranchFromSourceVersion(getEnv("CODEBUILD_SOURCE_VERSION")) ||
230+
this.jobRunBranch ||
231+
undefined
232+
);
233+
},
234+
235+
get targetBranch() {
236+
return stripRefsHeads(getEnv("CODEBUILD_WEBHOOK_BASE_REF")) || undefined;
237+
},
238+
239+
get pullRequest() {
240+
const pullRequestId =
241+
parsePullRequestFromWebhookTrigger() || parsePullRequestFromSourceVersion(getEnv("CODEBUILD_SOURCE_VERSION"));
242+
243+
return pullRequestId
244+
? {
245+
id: pullRequestId,
246+
url: this.pullRequestUrl || undefined,
247+
title: this.pullRequestName || undefined,
248+
}
249+
: undefined;
250+
},
166251
};

packages/ci/src/detectors/azure.ts

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

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

56
export const getRootURL = (): string => getEnv("SYSTEM_COLLECTIONURI");
@@ -10,6 +11,31 @@ export const getDefinitionID = (): string => getEnv("SYSTEM_DEFINITIONID");
1011

1112
export const getProjectID = (): string => getEnv("SYSTEM_TEAMPROJECTID");
1213

14+
const mapAzureRepositoryProvider = (provider: string): GitProvider | undefined => {
15+
switch (provider) {
16+
case "GitHub":
17+
return GitProvider.Github;
18+
case "Bitbucket":
19+
return GitProvider.Bitbucket;
20+
default:
21+
return undefined;
22+
}
23+
};
24+
25+
const normalizeBranchRef = (branch?: string): string | undefined => {
26+
const trimmed = branch?.trim() ?? "";
27+
28+
return trimmed ? stripRefsHeads(trimmed) : undefined;
29+
};
30+
31+
const getRepositoryUrl = () => getEnv("BUILD_REPOSITORY_URI") || getEnv("SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI");
32+
33+
const getRepositoryFromUrl = () => {
34+
const repositoryUrl = getRepositoryUrl();
35+
36+
return repositoryUrl ? resolveRepositoryFromGitUrl(repositoryUrl) : undefined;
37+
};
38+
1339
export const azure: CiDescriptor = {
1440
type: CiType.Azure,
1541

@@ -53,9 +79,13 @@ export const azure: CiDescriptor = {
5379

5480
get pullRequestUrl(): string {
5581
const repositoryProvider = getEnv("BUILD_REPOSITORY_PROVIDER");
56-
const repositoryUrl = getEnv("SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI");
82+
const repositoryUrl = getEnv("SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI") || getEnv("BUILD_REPOSITORY_URI");
5783
const pullRequestNumber = getEnv("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER");
5884

85+
if (!repositoryUrl || !pullRequestNumber) {
86+
return "";
87+
}
88+
5989
if (repositoryProvider === "GitHub") {
6090
return `${repositoryUrl}/pull/${pullRequestNumber}`;
6191
}
@@ -70,4 +100,53 @@ export const azure: CiDescriptor = {
70100
get pullRequestName(): string {
71101
return "";
72102
},
103+
104+
get provider() {
105+
return getRepositoryFromUrl()?.provider ?? mapAzureRepositoryProvider(getEnv("BUILD_REPOSITORY_PROVIDER"));
106+
},
107+
108+
get repository() {
109+
const repositoryUrl = getRepositoryUrl();
110+
const fromUrl = repositoryUrl ? resolveRepositoryFromGitUrl(repositoryUrl) : undefined;
111+
const slug = fromUrl?.slug ?? (getEnv("BUILD_REPOSITORY_NAME") || undefined);
112+
113+
if (!this.provider || !slug) {
114+
return undefined;
115+
}
116+
117+
return {
118+
slug,
119+
url: fromUrl?.url ?? repositoryUrl,
120+
};
121+
},
122+
123+
get sourceBranch() {
124+
return (
125+
normalizeBranchRef(getEnv("SYSTEM_PULLREQUEST_SOURCEBRANCH")) ||
126+
getEnv("BUILD_SOURCEBRANCHNAME") ||
127+
this.jobRunBranch ||
128+
undefined
129+
);
130+
},
131+
132+
get targetBranch() {
133+
return (
134+
normalizeBranchRef(getEnv("SYSTEM_PULLREQUEST_TARGETBRANCH")) ||
135+
getEnv("SYSTEM_PULLREQUEST_TARGETBRANCHNAME") ||
136+
undefined
137+
);
138+
},
139+
140+
get pullRequest() {
141+
const pullRequestNumber =
142+
getEnv("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER") || getEnv("SYSTEM_PULLREQUEST_PULLREQUESTID");
143+
144+
return pullRequestNumber
145+
? {
146+
id: pullRequestNumber,
147+
url: this.pullRequestUrl || undefined,
148+
title: this.pullRequestName || undefined,
149+
}
150+
: undefined;
151+
},
73152
};

packages/ci/src/detectors/bitbucket.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type CiDescriptor, CiType } from "@allurereport/core-api";
1+
import { type CiDescriptor, CiType, GitProvider } from "@allurereport/core-api";
22

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

@@ -62,4 +62,39 @@ export const bitbucket: CiDescriptor = {
6262
get pullRequestName(): string {
6363
return "";
6464
},
65+
66+
get provider() {
67+
return GitProvider.Bitbucket;
68+
},
69+
70+
get repository() {
71+
const repositorySlug = getEnv("BITBUCKET_REPO_FULL_NAME");
72+
73+
return repositorySlug
74+
? {
75+
slug: repositorySlug,
76+
url: getEnv("BITBUCKET_GIT_HTTP_ORIGIN") || undefined,
77+
}
78+
: undefined;
79+
},
80+
81+
get sourceBranch() {
82+
return getEnv("BITBUCKET_BRANCH") || this.jobRunBranch || undefined;
83+
},
84+
85+
get targetBranch() {
86+
return getEnv("BITBUCKET_PR_DESTINATION_BRANCH") || undefined;
87+
},
88+
89+
get pullRequest() {
90+
const prId = getEnv("BITBUCKET_PR_ID");
91+
92+
return prId
93+
? {
94+
id: prId,
95+
url: this.pullRequestUrl || undefined,
96+
title: this.pullRequestName || undefined,
97+
}
98+
: undefined;
99+
},
65100
};

packages/ci/src/detectors/circle.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { type CiDescriptor, CiType } from "@allurereport/core-api";
22

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

56
export const getBuildNumber = (): string => getEnv("CIRCLE_BUILD_NUM");
@@ -13,6 +14,12 @@ const getJobURL = (): string => {
1314
return jobRunURL.replace(`/${buildNumber}`, "");
1415
};
1516

17+
const getRepository = () => {
18+
const repositoryUrl = getEnv("CIRCLE_REPOSITORY_URL");
19+
20+
return repositoryUrl ? resolveRepositoryFromGitUrl(repositoryUrl) : undefined;
21+
};
22+
1623
export const circle: CiDescriptor = {
1724
type: CiType.Circle,
1825

@@ -68,4 +75,36 @@ export const circle: CiDescriptor = {
6875
get pullRequestName(): string {
6976
return "";
7077
},
78+
79+
get provider() {
80+
return getRepository()?.provider;
81+
},
82+
83+
get repository() {
84+
const repository = getRepository();
85+
86+
return repository
87+
? {
88+
slug: repository.slug,
89+
url: repository.url,
90+
}
91+
: undefined;
92+
},
93+
94+
get sourceBranch() {
95+
return getEnv("CIRCLE_BRANCH") || this.jobRunBranch || undefined;
96+
},
97+
98+
get pullRequest() {
99+
const pullRequestUrl = getEnv("CIRCLE_PULL_REQUEST");
100+
const pullRequestNumber = pullRequestUrl ? parsePullRequestNumberFromUrl(pullRequestUrl) : undefined;
101+
102+
return pullRequestNumber
103+
? {
104+
id: pullRequestNumber,
105+
url: this.pullRequestUrl || pullRequestUrl || undefined,
106+
title: this.pullRequestName || undefined,
107+
}
108+
: undefined;
109+
},
71110
};

0 commit comments

Comments
 (0)