Skip to content

Commit

Permalink
Merge pull request #262 from test-results-reporter/fix/gitlab-branch-…
Browse files Browse the repository at this point in the history
…hyperlink-issue

fix: Incorrect branch and PR hyperlink for gitlab ci extension
  • Loading branch information
leelaprasadv authored Dec 13, 2024
2 parents de28edb + 826fc41 commit 89dc1e2
Show file tree
Hide file tree
Showing 11 changed files with 406 additions and 150 deletions.
15 changes: 5 additions & 10 deletions src/extensions/ci-info.extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,18 @@ class CIInfoExtension extends BaseExtension {
this.#setRepositoryElement();
}
if (this.extension.inputs.show_repository_branch) {
if (this.ci.repository_ref.includes('refs/pull')) {
if (this.ci.pull_request_name) {
this.#setPullRequestElement();
} else {
this.#setRepositoryBranchElement();
}
}
if (!this.extension.inputs.show_repository && !this.extension.inputs.show_repository_branch && this.extension.inputs.show_repository_non_common) {
if (this.ci.repository_ref.includes('refs/pull')) {
if (this.ci.pull_request_name) {
this.#setRepositoryElement();
this.#setPullRequestElement();
} else {
const branch_name = this.ci.repository_ref.replace('refs/heads/', '');
if (!COMMON_BRANCH_NAMES.includes(branch_name.toLowerCase())) {
if (!COMMON_BRANCH_NAMES.includes(this.ci.branch_name.toLowerCase())) {
this.#setRepositoryElement();
this.#setRepositoryBranchElement();
}
Expand All @@ -81,15 +80,11 @@ class CIInfoExtension extends BaseExtension {
}

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

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

#setBuildElements() {
Expand Down
4 changes: 4 additions & 0 deletions src/extensions/extensions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export type ICIInfo = {
repository_name: string
repository_ref: string
repository_commit_sha: string
branch_url: string
branch_name: string
pull_request_url: string
pull_request_name: string
build_url: string
build_number: string
build_name: string
Expand Down
140 changes: 0 additions & 140 deletions src/helpers/ci.js

This file was deleted.

37 changes: 37 additions & 0 deletions src/helpers/ci/azure-devops.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const ENV = process.env;

/**
* @returns {import('../../extensions/extensions').ICIInfo}
*/
function info() {
const azure_devops = {
ci: 'AZURE_DEVOPS_PIPELINES',
git: 'AZURE_DEVOPS_REPOS',
repository_url: ENV.BUILD_REPOSITORY_URI,
repository_name: ENV.BUILD_REPOSITORY_NAME,
repository_ref: ENV.BUILD_SOURCEBRANCH,
repository_commit_sha: ENV.BUILD_SOURCEVERSION,
branch_url: '',
branch_name: '',
pull_request_url:'',
pull_request_name: '',
build_url: ENV.SYSTEM_TEAMFOUNDATIONCOLLECTIONURI + ENV.SYSTEM_TEAMPROJECT + '/_build/results?buildId=' + ENV.BUILD_BUILDID,
build_number: ENV.BUILD_BUILDNUMBER,
build_name: ENV.BUILD_DEFINITIONNAME,
build_reason: ENV.BUILD_REASON,
user: ENV.BUILD_REQUESTEDFOR
}

azure_devops.branch_url = azure_devops.repository_url + azure_devops.repository_ref.replace('refs/heads/', '/tree/');
azure_devops.branch_name = azure_devops.repository_ref.replace('refs/heads/', '');

if (azure_devops.repository_ref.includes('refs/pull')) {
azure_devops.pull_request_url = azure_devops.repository_url + azure_devops.repository_ref.replace('refs/pull/', '/pull/');
azure_devops.pull_request_name = azure_devops.repository_ref.replace('refs/pull/', '').replace('/merge', '');
}
return azure_devops;
}

module.exports = {
info
}
38 changes: 38 additions & 0 deletions src/helpers/ci/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const ENV = process.env;

/**
* @returns {import('../../extensions/extensions').ICIInfo}
*/
function info() {
const github = {
ci: 'GITHUB_ACTIONS',
git: 'GITHUB',
repository_url: ENV.GITHUB_SERVER_URL + '/' + ENV.GITHUB_REPOSITORY,
repository_name: ENV.GITHUB_REPOSITORY,
repository_ref: ENV.GITHUB_REF,
repository_commit_sha: ENV.GITHUB_SHA,
branch_url: '',
branch_name: '',
pull_request_url:'',
pull_request_name: '',
build_url: ENV.GITHUB_SERVER_URL + '/' + ENV.GITHUB_REPOSITORY + '/actions/runs/' + ENV.GITHUB_RUN_ID,
build_number: ENV.GITHUB_RUN_NUMBER,
build_name: ENV.GITHUB_WORKFLOW,
build_reason: ENV.GITHUB_EVENT_NAME,
user: ENV.GITHUB_ACTOR,
}

github.branch_url = github.repository_url + github.repository_ref.replace('refs/heads/', '/tree/');
github.branch_name = github.repository_ref.replace('refs/heads/', '');

if (github.repository_ref.includes('refs/pull')) {
github.pull_request_url = github.repository_url + github.repository_ref.replace('refs/pull/', '/pull/');
github.pull_request_name = github.repository_ref.replace('refs/pull/', '').replace('/merge', '');
}

return github
}

module.exports = {
info
}
36 changes: 36 additions & 0 deletions src/helpers/ci/gitlab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const ENV = process.env;

/**
* @returns {import('../../extensions/extensions').ICIInfo}
*/
function info() {
const gitlab = {
ci: 'GITLAB',
git: 'GITLAB',
repository_url: ENV.CI_PROJECT_URL,
repository_name: ENV.CI_PROJECT_NAME,
repository_ref: '/-/tree/' + (ENV.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME || ENV.CI_COMMIT_REF_NAME),
repository_commit_sha: ENV.CI_MERGE_REQUEST_SOURCE_BRANCH_SHA || ENV.CI_COMMIT_SHA,
branch_url: ENV.CI_PROJECT_URL + '/-/tree/' + (ENV.CI_COMMIT_REF_NAME || ENV.CI_COMMIT_BRANCH),
branch_name: ENV.CI_COMMIT_REF_NAME || ENV.CI_COMMIT_BRANCH,
pull_request_url:'',
pull_request_name: '',
build_url: ENV.CI_JOB_URL,
build_number: ENV.CI_JOB_ID,
build_name: ENV.CI_JOB_NAME,
build_reason: ENV.CI_PIPELINE_SOURCE,
user: ENV.GITLAB_USER_LOGIN || ENV.CI_COMMIT_AUTHOR
}

if (ENV.CI_OPEN_MERGE_REQUESTS) {
const pr_number = ENV.CI_OPEN_MERGE_REQUESTS.split("!")[1];
gitlab.pull_request_name = "#" + pr_number;
gitlab.pull_request_url = ENV.CI_PROJECT_URL + "/-/merge_requests/" + pr_number;
}

return gitlab
}

module.exports = {
info
}
60 changes: 60 additions & 0 deletions src/helpers/ci/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const os = require('os');
const github = require('./github');
const gitlab = require('./gitlab');
const jenkins = require('./jenkins');
const azure_devops = require('./azure-devops');
const system = require('./system');

const ENV = process.env;

/**
* @returns {import('../../extensions/extensions').ICIInfo}
*/
function getCIInformation() {
const ci_info = getBaseCIInfo();
const system_info = system.info();
return {
...ci_info,
...system_info
}
}

function getBaseCIInfo() {
if (ENV.GITHUB_ACTIONS) {
return github.info();
}
if (ENV.GITLAB_CI) {
return gitlab.info();
}
if (ENV.JENKINS_URL) {
return jenkins.info();
}
if (ENV.SYSTEM_TEAMFOUNDATIONCOLLECTIONURI) {
return azure_devops.info();
}
return getDefaultInformation();
}

function getDefaultInformation() {
return {
ci: ENV.TEST_BEATS_CI_NAME,
git: ENV.TEST_BEATS_CI_GIT,
repository_url: ENV.TEST_BEATS_CI_REPOSITORY_URL,
repository_name: ENV.TEST_BEATS_CI_REPOSITORY_NAME,
repository_ref: ENV.TEST_BEATS_CI_REPOSITORY_REF,
repository_commit_sha: ENV.TEST_BEATS_CI_REPOSITORY_COMMIT_SHA,
branch_url: ENV.TEST_BEATS_BRANCH_URL,
branch_name: ENV.TEST_BEATS_BRANCH_NAME,
pull_request_url: ENV.TEST_BEATS_PULL_REQUEST_URL,
pull_request_name: ENV.TEST_BEATS_PULL_REQUEST_NAME,
build_url: ENV.TEST_BEATS_CI_BUILD_URL,
build_number: ENV.TEST_BEATS_CI_BUILD_NUMBER,
build_name: ENV.TEST_BEATS_CI_BUILD_NAME,
build_reason: ENV.TEST_BEATS_CI_BUILD_REASON,
user: ENV.TEST_BEATS_CI_USER || os.userInfo().username
}
}

module.exports = {
getCIInformation
}
Loading

0 comments on commit 89dc1e2

Please sign in to comment.