Skip to content

Commit 93bd4a5

Browse files
committed
feat(plugin-testops): attach git information on launch create
1 parent f6b51c7 commit 93bd4a5

68 files changed

Lines changed: 3055 additions & 129 deletions

Some content is hidden

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

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ allure-results.zip
6060
docs/kb/.obsidian/
6161

6262
.tmp
63+
/.tmp
64+
/.agent-workspace

.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/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@
88
- 📢 [Official announcements](https://github.com/orgs/allure-framework/discussions/categories/announcements) – be in touch with the latest updates
99
- 💬 [General Discussion ](https://github.com/orgs/allure-framework/discussions/categories/general-discussion) – engage in casual conversations, share insights and ideas with the community
1010

11-
---
12-
13-
TODO:
11+
---

packages/ci/src/detect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { local } from "./detectors/local.js";
1414
* Tries to detect current CI
1515
* Returns CI descriptor if some detected, otherwise undefined
1616
*/
17-
export const detect = (): CiDescriptor | undefined => {
17+
export const detect = (): CiDescriptor => {
1818
return (
1919
[amazon, azure, bitbucket, circle, drone, github, gitlab, jenkins].find((descriptor) => descriptor.detected) ??
2020
local

packages/ci/src/detectors/github.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import { join } from "node:path/posix";
22

33
import { type CiDescriptor, CiType } from "@allurereport/core-api";
44

5+
import { resolveGithubPullRequestNumber } from "../helpers/github.js";
56
import { getEnv } from "../utils.js";
67

7-
const pullRequestSuffixRe = /\/merge$/;
8-
98
const getBaseURL = () => getEnv("GITHUB_SERVER_URL");
109

1110
const getRunID = () => getEnv("GITHUB_RUN_ID");
@@ -62,13 +61,12 @@ export const github: CiDescriptor = {
6261
},
6362

6463
get pullRequestUrl(): string {
65-
const refName = getEnv("GITHUB_REF_NAME");
64+
const pullRequestNumber = resolveGithubPullRequestNumber();
6665

67-
if (!pullRequestSuffixRe.test(refName)) {
66+
if (!pullRequestNumber) {
6867
return "";
6968
}
7069

71-
const pullRequestNumber = refName.replace(pullRequestSuffixRe, "");
7270
const serverUrl = getEnv("GITHUB_SERVER_URL");
7371
const repo = getRepo();
7472
const pathname = join(repo, "pull", pullRequestNumber);
@@ -77,14 +75,12 @@ export const github: CiDescriptor = {
7775
},
7876

7977
get pullRequestName(): string {
80-
const refName = getEnv("GITHUB_REF_NAME");
78+
const pullRequestNumber = resolveGithubPullRequestNumber();
8179

82-
if (!pullRequestSuffixRe.test(refName)) {
80+
if (!pullRequestNumber) {
8381
return "";
8482
}
8583

86-
const pullRequestNumber = refName.replace(pullRequestSuffixRe, "");
87-
8884
return `Pull request #${pullRequestNumber}`;
8985
},
9086
};

packages/ci/src/detectors/local.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ export const local: CiDescriptor = {
6262
return "";
6363
},
6464
};
65+
66+
export const isLocalCiDescriptor = (ci: CiDescriptor): ci is Omit<CiDescriptor, "type"> & { type: CiType.Local } =>
67+
ci.type === CiType.Local;

packages/ci/src/gitHints/amazon.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { type CiDescriptor, type CiGitHints } from "@allurereport/core-api";
2+
3+
import { resolveRepositoryFromGitUrl, stripRefsHeads } from "../helpers/gitProvider.js";
4+
import { getEnv } from "../utils.js";
5+
6+
const parseBranchFromSourceVersion = (sourceVersion: string): string | undefined => {
7+
const branchMatch = sourceVersion.match(/refs\/heads\/(?<branch>[^/^]+)/)?.groups?.branch;
8+
9+
if (branchMatch) {
10+
return branchMatch;
11+
}
12+
13+
const branchTrigger = getEnv("CODEBUILD_WEBHOOK_TRIGGER");
14+
15+
if (branchTrigger.startsWith("branch/")) {
16+
return branchTrigger.slice("branch/".length);
17+
}
18+
19+
return undefined;
20+
};
21+
22+
const parsePullRequestFromSourceVersion = (sourceVersion: string): string | undefined => {
23+
const prPrefixMatch = sourceVersion.match(/^pr\/(?<id>\d+)$/i)?.groups?.id;
24+
25+
if (prPrefixMatch) {
26+
return prPrefixMatch;
27+
}
28+
29+
return sourceVersion.match(/refs\/pull\/(?<id>\d+)\//)?.groups?.id;
30+
};
31+
32+
const parsePullRequestFromWebhookTrigger = (): string | undefined => {
33+
const trigger = getEnv("CODEBUILD_WEBHOOK_TRIGGER");
34+
35+
return trigger.match(/^pr\/(?<id>\d+)$/i)?.groups?.id;
36+
};
37+
38+
export const resolveAmazonGitHints = (ci: CiDescriptor): CiGitHints => {
39+
const repoUrl = getEnv("CODEBUILD_SOURCE_REPO_URL");
40+
const repository = repoUrl ? resolveRepositoryFromGitUrl(repoUrl) : undefined;
41+
42+
if (!repository) {
43+
return {};
44+
}
45+
46+
const sourceVersion = getEnv("CODEBUILD_SOURCE_VERSION");
47+
const pullRequestId = parsePullRequestFromWebhookTrigger() || parsePullRequestFromSourceVersion(sourceVersion);
48+
const pullRequest = pullRequestId
49+
? {
50+
id: pullRequestId,
51+
url: ci.pullRequestUrl || undefined,
52+
title: ci.pullRequestName || undefined,
53+
}
54+
: undefined;
55+
56+
const headRef = getEnv("CODEBUILD_WEBHOOK_HEAD_REF");
57+
58+
return {
59+
provider: repository.provider,
60+
repository: {
61+
slug: repository.slug,
62+
url: repository.url,
63+
},
64+
sourceBranch:
65+
(headRef ? stripRefsHeads(headRef) : undefined) ||
66+
parseBranchFromSourceVersion(sourceVersion) ||
67+
ci.jobRunBranch ||
68+
undefined,
69+
targetBranch: stripRefsHeads(getEnv("CODEBUILD_WEBHOOK_BASE_REF")) || undefined,
70+
pullRequest,
71+
};
72+
};

packages/ci/src/gitHints/azure.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { type CiDescriptor, type CiGitHints, GitProvider } from "@allurereport/core-api";
2+
3+
import { resolveRepositoryFromGitUrl, stripRefsHeads } from "../helpers/gitProvider.js";
4+
import { getEnv } from "../utils.js";
5+
6+
const mapAzureRepositoryProvider = (provider: string): GitProvider | undefined => {
7+
switch (provider) {
8+
case "GitHub":
9+
return GitProvider.Github;
10+
case "Bitbucket":
11+
return GitProvider.Bitbucket;
12+
default:
13+
return undefined;
14+
}
15+
};
16+
17+
const normalizeBranchRef = (branch: string): string | undefined => {
18+
const trimmed = branch.trim();
19+
20+
return trimmed ? stripRefsHeads(trimmed) : undefined;
21+
};
22+
23+
export const resolveAzureGitHints = (ci: CiDescriptor): CiGitHints => {
24+
const repositoryUrl = getEnv("BUILD_REPOSITORY_URI") || getEnv("SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI") || undefined;
25+
const fromUrl = repositoryUrl ? resolveRepositoryFromGitUrl(repositoryUrl) : undefined;
26+
const fromProvider = mapAzureRepositoryProvider(getEnv("BUILD_REPOSITORY_PROVIDER"));
27+
28+
const provider = fromUrl?.provider ?? fromProvider;
29+
30+
if (!provider) {
31+
return {};
32+
}
33+
34+
const slug = fromUrl?.slug ?? (getEnv("BUILD_REPOSITORY_NAME") || undefined);
35+
const url = fromUrl?.url ?? repositoryUrl;
36+
37+
if (!slug) {
38+
return {};
39+
}
40+
41+
const pullRequestNumber =
42+
getEnv("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER") || getEnv("SYSTEM_PULLREQUEST_PULLREQUESTID");
43+
const pullRequest = pullRequestNumber
44+
? {
45+
id: pullRequestNumber,
46+
url: ci.pullRequestUrl || undefined,
47+
title: ci.pullRequestName || undefined,
48+
}
49+
: undefined;
50+
51+
const sourceBranch =
52+
normalizeBranchRef(getEnv("SYSTEM_PULLREQUEST_SOURCEBRANCH")) ||
53+
getEnv("BUILD_SOURCEBRANCHNAME") ||
54+
ci.jobRunBranch ||
55+
undefined;
56+
const targetBranch =
57+
normalizeBranchRef(getEnv("SYSTEM_PULLREQUEST_TARGETBRANCH")) ||
58+
getEnv("SYSTEM_PULLREQUEST_TARGETBRANCHNAME") ||
59+
undefined;
60+
61+
return {
62+
provider,
63+
repository: {
64+
slug,
65+
url,
66+
},
67+
sourceBranch,
68+
targetBranch,
69+
pullRequest,
70+
};
71+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { type CiDescriptor, type CiGitHints, GitProvider } from "@allurereport/core-api";
2+
3+
import { getEnv } from "../utils.js";
4+
5+
export const resolveBitbucketGitHints = (ci: CiDescriptor): CiGitHints => {
6+
const repositorySlug = getEnv("BITBUCKET_REPO_FULL_NAME");
7+
const origin = getEnv("BITBUCKET_GIT_HTTP_ORIGIN");
8+
const repository = repositorySlug
9+
? {
10+
slug: repositorySlug,
11+
url: origin || undefined,
12+
}
13+
: undefined;
14+
15+
const sourceBranch = getEnv("BITBUCKET_BRANCH") || ci.jobRunBranch || undefined;
16+
const targetBranch = getEnv("BITBUCKET_PR_DESTINATION_BRANCH") || undefined;
17+
18+
const prId = getEnv("BITBUCKET_PR_ID");
19+
const pullRequest = prId
20+
? {
21+
id: prId,
22+
url: ci.pullRequestUrl || undefined,
23+
title: ci.pullRequestName || undefined,
24+
}
25+
: undefined;
26+
27+
return {
28+
provider: GitProvider.Bitbucket,
29+
repository,
30+
sourceBranch,
31+
targetBranch,
32+
pullRequest,
33+
};
34+
};

packages/ci/src/gitHints/circle.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { type CiDescriptor, type CiGitHints } from "@allurereport/core-api";
2+
3+
import { parsePullRequestNumberFromUrl, resolveRepositoryFromGitUrl } from "../helpers/gitProvider.js";
4+
import { getEnv } from "../utils.js";
5+
6+
export const resolveCircleGitHints = (ci: CiDescriptor): CiGitHints => {
7+
const repositoryUrl = getEnv("CIRCLE_REPOSITORY_URL");
8+
const repository = repositoryUrl ? resolveRepositoryFromGitUrl(repositoryUrl) : undefined;
9+
10+
if (!repository) {
11+
return {};
12+
}
13+
14+
const pullRequestUrl = getEnv("CIRCLE_PULL_REQUEST");
15+
const pullRequestNumber = pullRequestUrl ? parsePullRequestNumberFromUrl(pullRequestUrl) : undefined;
16+
17+
if (!pullRequestNumber) {
18+
return {
19+
provider: repository.provider,
20+
repository: {
21+
slug: repository.slug,
22+
url: repository.url,
23+
},
24+
sourceBranch: getEnv("CIRCLE_BRANCH") || ci.jobRunBranch || undefined,
25+
};
26+
}
27+
28+
return {
29+
provider: repository.provider,
30+
repository: {
31+
slug: repository.slug,
32+
url: repository.url,
33+
},
34+
sourceBranch: getEnv("CIRCLE_BRANCH") || ci.jobRunBranch || undefined,
35+
pullRequest: {
36+
id: pullRequestNumber,
37+
url: ci.pullRequestUrl || pullRequestUrl || undefined,
38+
title: ci.pullRequestName || undefined,
39+
},
40+
};
41+
};

0 commit comments

Comments
 (0)