Skip to content

Commit 925d56d

Browse files
committed
feat: git flow for testops plugin
1 parent 523bcd6 commit 925d56d

38 files changed

Lines changed: 1631 additions & 7 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@ allure-results.zip
5656

5757
*storybook.log
5858

59-
.tmp
59+
/.tmp
60+
/.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.
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/github.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { type CiDescriptor, type CiGitHints, GitProvider } from "@allurereport/core-api";
2+
3+
import { getEnv } from "../utils.js";
4+
5+
const pullRequestSuffixRe = /\/merge$/;
6+
7+
const stripRefsHeads = (ref: string): string => ref.replace(/^refs\/heads\//, "");
8+
9+
export const resolveGithubGitHints = (ci: CiDescriptor): CiGitHints => {
10+
const repositorySlug = getEnv("GITHUB_REPOSITORY");
11+
const serverUrl = getEnv("GITHUB_SERVER_URL");
12+
const repository = repositorySlug
13+
? {
14+
slug: repositorySlug,
15+
url: serverUrl ? `${serverUrl}/${repositorySlug}` : undefined,
16+
}
17+
: undefined;
18+
19+
const headRef = getEnv("GITHUB_HEAD_REF");
20+
const baseRef = getEnv("GITHUB_BASE_REF");
21+
const sourceBranch = headRef || stripRefsHeads(getEnv("GITHUB_REF")) || ci.jobRunBranch || undefined;
22+
const targetBranch = baseRef || undefined;
23+
24+
const refName = getEnv("GITHUB_REF_NAME");
25+
const pullRequestId = pullRequestSuffixRe.test(refName) ? refName.replace(pullRequestSuffixRe, "") : "";
26+
27+
const pullRequest = pullRequestId
28+
? {
29+
id: pullRequestId,
30+
url: ci.pullRequestUrl || undefined,
31+
title: ci.pullRequestName || undefined,
32+
}
33+
: undefined;
34+
35+
return {
36+
provider: GitProvider.Github,
37+
repository,
38+
sourceBranch,
39+
targetBranch,
40+
pullRequest,
41+
};
42+
};

packages/ci/src/gitHints/gitlab.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { type CiDescriptor, type CiGitHints, GitProvider } from "@allurereport/core-api";
2+
3+
import { getEnv } from "../utils.js";
4+
5+
export const resolveGitlabGitHints = (ci: CiDescriptor): CiGitHints => {
6+
const projectPath = getEnv("CI_PROJECT_PATH");
7+
const projectUrl = getEnv("CI_PROJECT_URL");
8+
const repository = projectPath
9+
? {
10+
slug: projectPath,
11+
url: projectUrl || undefined,
12+
}
13+
: undefined;
14+
15+
const mergeRequestSource = getEnv("CI_MERGE_REQUEST_SOURCE_BRANCH_NAME");
16+
const sourceBranch = mergeRequestSource || getEnv("CI_COMMIT_REF_NAME") || ci.jobRunBranch || undefined;
17+
const targetBranch = getEnv("CI_MERGE_REQUEST_TARGET_BRANCH_NAME") || undefined;
18+
19+
const mergeRequestIid = getEnv("CI_MERGE_REQUEST_IID");
20+
const pullRequest = mergeRequestIid
21+
? {
22+
id: mergeRequestIid,
23+
url: ci.pullRequestUrl || undefined,
24+
title: getEnv("CI_MERGE_REQUEST_TITLE") || ci.pullRequestName || undefined,
25+
}
26+
: undefined;
27+
28+
return {
29+
provider: GitProvider.Gitlab,
30+
repository,
31+
sourceBranch,
32+
targetBranch,
33+
pullRequest,
34+
};
35+
};

packages/ci/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { detect } from "./detect.js";
2+
export { resolveGitHints } from "./resolveGitHints.js";

packages/ci/src/resolveGitHints.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { type CiDescriptor, type CiGitHints, CiType } from "@allurereport/core-api";
2+
3+
import { resolveBitbucketGitHints } from "./gitHints/bitbucket.js";
4+
import { resolveGithubGitHints } from "./gitHints/github.js";
5+
import { resolveGitlabGitHints } from "./gitHints/gitlab.js";
6+
7+
export const resolveGitHints = (ci: CiDescriptor): CiGitHints => {
8+
switch (ci.type) {
9+
case CiType.Github:
10+
return resolveGithubGitHints(ci);
11+
case CiType.Gitlab:
12+
return resolveGitlabGitHints(ci);
13+
case CiType.Bitbucket:
14+
return resolveBitbucketGitHints(ci);
15+
default:
16+
return {};
17+
}
18+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { CiType, type CiDescriptor, GitProvider } from "@allurereport/core-api";
2+
import { type Mock, beforeEach, describe, expect, it, vi } from "vitest";
3+
4+
import { resolveBitbucketGitHints } from "../src/gitHints/bitbucket.js";
5+
import { getEnv } from "../src/utils.js";
6+
7+
vi.mock("../src/utils.js", () => ({
8+
getEnv: vi.fn(),
9+
}));
10+
11+
const bitbucketCi = {
12+
type: CiType.Bitbucket,
13+
jobRunBranch: "feature/foo",
14+
pullRequestUrl: "https://bitbucket.org/myorg/myrepo/pull-requests/15",
15+
pullRequestName: "",
16+
} as CiDescriptor;
17+
18+
beforeEach(() => {
19+
vi.clearAllMocks();
20+
});
21+
22+
describe("resolveBitbucketGitHints", () => {
23+
it("maps repository, branches, and pull request", () => {
24+
(getEnv as Mock).mockImplementation((key: string) => {
25+
const env: Record<string, string> = {
26+
BITBUCKET_REPO_FULL_NAME: "myorg/myrepo",
27+
BITBUCKET_GIT_HTTP_ORIGIN: "https://bitbucket.org/myorg/myrepo",
28+
BITBUCKET_BRANCH: "feature/foo",
29+
BITBUCKET_PR_DESTINATION_BRANCH: "main",
30+
BITBUCKET_PR_ID: "15",
31+
};
32+
33+
return env[key] ?? "";
34+
});
35+
36+
expect(resolveBitbucketGitHints(bitbucketCi)).toEqual({
37+
provider: GitProvider.Bitbucket,
38+
repository: {
39+
slug: "myorg/myrepo",
40+
url: "https://bitbucket.org/myorg/myrepo",
41+
},
42+
sourceBranch: "feature/foo",
43+
targetBranch: "main",
44+
pullRequest: {
45+
id: "15",
46+
url: "https://bitbucket.org/myorg/myrepo/pull-requests/15",
47+
title: undefined,
48+
},
49+
});
50+
});
51+
});
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { CiType, type CiDescriptor, GitProvider } from "@allurereport/core-api";
2+
import { type Mock, beforeEach, describe, expect, it, vi } from "vitest";
3+
4+
import { resolveGithubGitHints } from "../src/gitHints/github.js";
5+
import { getEnv } from "../src/utils.js";
6+
7+
vi.mock("../src/utils.js", () => ({
8+
getEnv: vi.fn(),
9+
}));
10+
11+
const githubCi = {
12+
type: CiType.Github,
13+
jobRunBranch: "feature/foo",
14+
pullRequestUrl: "https://github.com/myorg/myrepo/pull/42",
15+
pullRequestName: "Pull request #42",
16+
} as CiDescriptor;
17+
18+
beforeEach(() => {
19+
vi.clearAllMocks();
20+
});
21+
22+
describe("resolveGithubGitHints", () => {
23+
it("maps repository, branches, and pull request for PR workflow", () => {
24+
(getEnv as Mock).mockImplementation((key: string) => {
25+
const env: Record<string, string> = {
26+
GITHUB_REPOSITORY: "myorg/myrepo",
27+
GITHUB_SERVER_URL: "https://github.com",
28+
GITHUB_HEAD_REF: "feature/foo",
29+
GITHUB_BASE_REF: "main",
30+
GITHUB_REF_NAME: "42/merge",
31+
GITHUB_REF: "refs/heads/feature/foo",
32+
};
33+
34+
return env[key] ?? "";
35+
});
36+
37+
expect(resolveGithubGitHints(githubCi)).toEqual({
38+
provider: GitProvider.Github,
39+
repository: {
40+
slug: "myorg/myrepo",
41+
url: "https://github.com/myorg/myrepo",
42+
},
43+
sourceBranch: "feature/foo",
44+
targetBranch: "main",
45+
pullRequest: {
46+
id: "42",
47+
url: "https://github.com/myorg/myrepo/pull/42",
48+
title: "Pull request #42",
49+
},
50+
});
51+
});
52+
53+
it("uses GITHUB_REF branch when HEAD_REF is empty", () => {
54+
(getEnv as Mock).mockImplementation((key: string) => {
55+
if (key === "GITHUB_REPOSITORY") {
56+
return "myorg/myrepo";
57+
}
58+
59+
if (key === "GITHUB_REF") {
60+
return "refs/heads/main";
61+
}
62+
63+
return "";
64+
});
65+
66+
expect(resolveGithubGitHints(githubCi).sourceBranch).toBe("main");
67+
expect(resolveGithubGitHints(githubCi).pullRequest).toBeUndefined();
68+
});
69+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { CiType, type CiDescriptor, GitProvider } from "@allurereport/core-api";
2+
import { type Mock, beforeEach, describe, expect, it, vi } from "vitest";
3+
4+
import { resolveGitlabGitHints } from "../src/gitHints/gitlab.js";
5+
import { getEnv } from "../src/utils.js";
6+
7+
vi.mock("../src/utils.js", () => ({
8+
getEnv: vi.fn(),
9+
}));
10+
11+
const gitlabCi = {
12+
type: CiType.Gitlab,
13+
jobRunBranch: "feature/foo",
14+
pullRequestUrl: "https://gitlab.com/myorg/myrepo/-/merge_requests/7",
15+
pullRequestName: "Fix things",
16+
} as CiDescriptor;
17+
18+
beforeEach(() => {
19+
vi.clearAllMocks();
20+
});
21+
22+
describe("resolveGitlabGitHints", () => {
23+
it("maps merge request metadata and branches", () => {
24+
(getEnv as Mock).mockImplementation((key: string) => {
25+
const env: Record<string, string> = {
26+
CI_PROJECT_PATH: "myorg/myrepo",
27+
CI_PROJECT_URL: "https://gitlab.com/myorg/myrepo",
28+
CI_MERGE_REQUEST_SOURCE_BRANCH_NAME: "feature/foo",
29+
CI_MERGE_REQUEST_TARGET_BRANCH_NAME: "main",
30+
CI_MERGE_REQUEST_IID: "7",
31+
CI_MERGE_REQUEST_TITLE: "Fix things",
32+
CI_COMMIT_REF_NAME: "feature/foo",
33+
};
34+
35+
return env[key] ?? "";
36+
});
37+
38+
expect(resolveGitlabGitHints(gitlabCi)).toEqual({
39+
provider: GitProvider.Gitlab,
40+
repository: {
41+
slug: "myorg/myrepo",
42+
url: "https://gitlab.com/myorg/myrepo",
43+
},
44+
sourceBranch: "feature/foo",
45+
targetBranch: "main",
46+
pullRequest: {
47+
id: "7",
48+
url: "https://gitlab.com/myorg/myrepo/-/merge_requests/7",
49+
title: "Fix things",
50+
},
51+
});
52+
});
53+
});

0 commit comments

Comments
 (0)