Skip to content

Commit 89dc1e2

Browse files
authored
Merge pull request #262 from test-results-reporter/fix/gitlab-branch-hyperlink-issue
fix: Incorrect branch and PR hyperlink for gitlab ci extension
2 parents de28edb + 826fc41 commit 89dc1e2

11 files changed

+406
-150
lines changed

src/extensions/ci-info.extension.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,18 @@ class CIInfoExtension extends BaseExtension {
5656
this.#setRepositoryElement();
5757
}
5858
if (this.extension.inputs.show_repository_branch) {
59-
if (this.ci.repository_ref.includes('refs/pull')) {
59+
if (this.ci.pull_request_name) {
6060
this.#setPullRequestElement();
6161
} else {
6262
this.#setRepositoryBranchElement();
6363
}
6464
}
6565
if (!this.extension.inputs.show_repository && !this.extension.inputs.show_repository_branch && this.extension.inputs.show_repository_non_common) {
66-
if (this.ci.repository_ref.includes('refs/pull')) {
66+
if (this.ci.pull_request_name) {
6767
this.#setRepositoryElement();
6868
this.#setPullRequestElement();
6969
} else {
70-
const branch_name = this.ci.repository_ref.replace('refs/heads/', '');
71-
if (!COMMON_BRANCH_NAMES.includes(branch_name.toLowerCase())) {
70+
if (!COMMON_BRANCH_NAMES.includes(this.ci.branch_name.toLowerCase())) {
7271
this.#setRepositoryElement();
7372
this.#setRepositoryBranchElement();
7473
}
@@ -81,15 +80,11 @@ class CIInfoExtension extends BaseExtension {
8180
}
8281

8382
#setPullRequestElement() {
84-
const pr_url = this.ci.repository_url + this.ci.repository_ref.replace('refs/pull/', '/pull/');
85-
const pr_name = this.ci.repository_ref.replace('refs/pull/', '').replace('/merge', '');
86-
this.repository_elements.push({ label: 'Pull Request', key: pr_name, value: pr_url, type: 'hyperlink' });
83+
this.repository_elements.push({ label: 'Pull Request', key: this.ci.pull_request_name, value: this.ci.pull_request_url, type: 'hyperlink' });
8784
}
8885

8986
#setRepositoryBranchElement() {
90-
const branch_url = this.ci.repository_url + this.ci.repository_ref.replace('refs/heads/', '/tree/');
91-
const branch_name = this.ci.repository_ref.replace('refs/heads/', '');
92-
this.repository_elements.push({ label: 'Branch', key: branch_name, value: branch_url, type: 'hyperlink' });
87+
this.repository_elements.push({ label: 'Branch', key: this.ci.branch_name, value: this.ci.branch_url, type: 'hyperlink' });
9388
}
9489

9590
#setBuildElements() {

src/extensions/extensions.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ export type ICIInfo = {
55
repository_name: string
66
repository_ref: string
77
repository_commit_sha: string
8+
branch_url: string
9+
branch_name: string
10+
pull_request_url: string
11+
pull_request_name: string
812
build_url: string
913
build_number: string
1014
build_name: string

src/helpers/ci.js

-140
This file was deleted.

src/helpers/ci/azure-devops.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const ENV = process.env;
2+
3+
/**
4+
* @returns {import('../../extensions/extensions').ICIInfo}
5+
*/
6+
function info() {
7+
const azure_devops = {
8+
ci: 'AZURE_DEVOPS_PIPELINES',
9+
git: 'AZURE_DEVOPS_REPOS',
10+
repository_url: ENV.BUILD_REPOSITORY_URI,
11+
repository_name: ENV.BUILD_REPOSITORY_NAME,
12+
repository_ref: ENV.BUILD_SOURCEBRANCH,
13+
repository_commit_sha: ENV.BUILD_SOURCEVERSION,
14+
branch_url: '',
15+
branch_name: '',
16+
pull_request_url:'',
17+
pull_request_name: '',
18+
build_url: ENV.SYSTEM_TEAMFOUNDATIONCOLLECTIONURI + ENV.SYSTEM_TEAMPROJECT + '/_build/results?buildId=' + ENV.BUILD_BUILDID,
19+
build_number: ENV.BUILD_BUILDNUMBER,
20+
build_name: ENV.BUILD_DEFINITIONNAME,
21+
build_reason: ENV.BUILD_REASON,
22+
user: ENV.BUILD_REQUESTEDFOR
23+
}
24+
25+
azure_devops.branch_url = azure_devops.repository_url + azure_devops.repository_ref.replace('refs/heads/', '/tree/');
26+
azure_devops.branch_name = azure_devops.repository_ref.replace('refs/heads/', '');
27+
28+
if (azure_devops.repository_ref.includes('refs/pull')) {
29+
azure_devops.pull_request_url = azure_devops.repository_url + azure_devops.repository_ref.replace('refs/pull/', '/pull/');
30+
azure_devops.pull_request_name = azure_devops.repository_ref.replace('refs/pull/', '').replace('/merge', '');
31+
}
32+
return azure_devops;
33+
}
34+
35+
module.exports = {
36+
info
37+
}

src/helpers/ci/github.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const ENV = process.env;
2+
3+
/**
4+
* @returns {import('../../extensions/extensions').ICIInfo}
5+
*/
6+
function info() {
7+
const github = {
8+
ci: 'GITHUB_ACTIONS',
9+
git: 'GITHUB',
10+
repository_url: ENV.GITHUB_SERVER_URL + '/' + ENV.GITHUB_REPOSITORY,
11+
repository_name: ENV.GITHUB_REPOSITORY,
12+
repository_ref: ENV.GITHUB_REF,
13+
repository_commit_sha: ENV.GITHUB_SHA,
14+
branch_url: '',
15+
branch_name: '',
16+
pull_request_url:'',
17+
pull_request_name: '',
18+
build_url: ENV.GITHUB_SERVER_URL + '/' + ENV.GITHUB_REPOSITORY + '/actions/runs/' + ENV.GITHUB_RUN_ID,
19+
build_number: ENV.GITHUB_RUN_NUMBER,
20+
build_name: ENV.GITHUB_WORKFLOW,
21+
build_reason: ENV.GITHUB_EVENT_NAME,
22+
user: ENV.GITHUB_ACTOR,
23+
}
24+
25+
github.branch_url = github.repository_url + github.repository_ref.replace('refs/heads/', '/tree/');
26+
github.branch_name = github.repository_ref.replace('refs/heads/', '');
27+
28+
if (github.repository_ref.includes('refs/pull')) {
29+
github.pull_request_url = github.repository_url + github.repository_ref.replace('refs/pull/', '/pull/');
30+
github.pull_request_name = github.repository_ref.replace('refs/pull/', '').replace('/merge', '');
31+
}
32+
33+
return github
34+
}
35+
36+
module.exports = {
37+
info
38+
}

src/helpers/ci/gitlab.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const ENV = process.env;
2+
3+
/**
4+
* @returns {import('../../extensions/extensions').ICIInfo}
5+
*/
6+
function info() {
7+
const gitlab = {
8+
ci: 'GITLAB',
9+
git: 'GITLAB',
10+
repository_url: ENV.CI_PROJECT_URL,
11+
repository_name: ENV.CI_PROJECT_NAME,
12+
repository_ref: '/-/tree/' + (ENV.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME || ENV.CI_COMMIT_REF_NAME),
13+
repository_commit_sha: ENV.CI_MERGE_REQUEST_SOURCE_BRANCH_SHA || ENV.CI_COMMIT_SHA,
14+
branch_url: ENV.CI_PROJECT_URL + '/-/tree/' + (ENV.CI_COMMIT_REF_NAME || ENV.CI_COMMIT_BRANCH),
15+
branch_name: ENV.CI_COMMIT_REF_NAME || ENV.CI_COMMIT_BRANCH,
16+
pull_request_url:'',
17+
pull_request_name: '',
18+
build_url: ENV.CI_JOB_URL,
19+
build_number: ENV.CI_JOB_ID,
20+
build_name: ENV.CI_JOB_NAME,
21+
build_reason: ENV.CI_PIPELINE_SOURCE,
22+
user: ENV.GITLAB_USER_LOGIN || ENV.CI_COMMIT_AUTHOR
23+
}
24+
25+
if (ENV.CI_OPEN_MERGE_REQUESTS) {
26+
const pr_number = ENV.CI_OPEN_MERGE_REQUESTS.split("!")[1];
27+
gitlab.pull_request_name = "#" + pr_number;
28+
gitlab.pull_request_url = ENV.CI_PROJECT_URL + "/-/merge_requests/" + pr_number;
29+
}
30+
31+
return gitlab
32+
}
33+
34+
module.exports = {
35+
info
36+
}

src/helpers/ci/index.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const os = require('os');
2+
const github = require('./github');
3+
const gitlab = require('./gitlab');
4+
const jenkins = require('./jenkins');
5+
const azure_devops = require('./azure-devops');
6+
const system = require('./system');
7+
8+
const ENV = process.env;
9+
10+
/**
11+
* @returns {import('../../extensions/extensions').ICIInfo}
12+
*/
13+
function getCIInformation() {
14+
const ci_info = getBaseCIInfo();
15+
const system_info = system.info();
16+
return {
17+
...ci_info,
18+
...system_info
19+
}
20+
}
21+
22+
function getBaseCIInfo() {
23+
if (ENV.GITHUB_ACTIONS) {
24+
return github.info();
25+
}
26+
if (ENV.GITLAB_CI) {
27+
return gitlab.info();
28+
}
29+
if (ENV.JENKINS_URL) {
30+
return jenkins.info();
31+
}
32+
if (ENV.SYSTEM_TEAMFOUNDATIONCOLLECTIONURI) {
33+
return azure_devops.info();
34+
}
35+
return getDefaultInformation();
36+
}
37+
38+
function getDefaultInformation() {
39+
return {
40+
ci: ENV.TEST_BEATS_CI_NAME,
41+
git: ENV.TEST_BEATS_CI_GIT,
42+
repository_url: ENV.TEST_BEATS_CI_REPOSITORY_URL,
43+
repository_name: ENV.TEST_BEATS_CI_REPOSITORY_NAME,
44+
repository_ref: ENV.TEST_BEATS_CI_REPOSITORY_REF,
45+
repository_commit_sha: ENV.TEST_BEATS_CI_REPOSITORY_COMMIT_SHA,
46+
branch_url: ENV.TEST_BEATS_BRANCH_URL,
47+
branch_name: ENV.TEST_BEATS_BRANCH_NAME,
48+
pull_request_url: ENV.TEST_BEATS_PULL_REQUEST_URL,
49+
pull_request_name: ENV.TEST_BEATS_PULL_REQUEST_NAME,
50+
build_url: ENV.TEST_BEATS_CI_BUILD_URL,
51+
build_number: ENV.TEST_BEATS_CI_BUILD_NUMBER,
52+
build_name: ENV.TEST_BEATS_CI_BUILD_NAME,
53+
build_reason: ENV.TEST_BEATS_CI_BUILD_REASON,
54+
user: ENV.TEST_BEATS_CI_USER || os.userInfo().username
55+
}
56+
}
57+
58+
module.exports = {
59+
getCIInformation
60+
}

0 commit comments

Comments
 (0)