Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Incorrect branch and PR hyperlink for gitlab ci extension #262

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
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) {
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
8 changes: 8 additions & 0 deletions src/extensions/extensions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ export type ICIInfo = {
repository_name: string
repository_ref: string
repository_commit_sha: string
branch_url: string
branch_name: string
pull_request: IPRInfo | boolean
leelaprasadv marked this conversation as resolved.
Show resolved Hide resolved
build_url: string
build_number: string
build_name: string
user: string
}

export type IPRInfo = {
name: string
url: string
}
140 changes: 0 additions & 140 deletions src/helpers/ci.js

This file was deleted.

38 changes: 38 additions & 0 deletions src/helpers/ci/azure-devops.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 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: false,
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/'),
name: azure_devops.repository_ref.replace('refs/pull/', '').replace('/merge', '')
}
}
return azure_devops;
}

module.exports = {
info
}
39 changes: 39 additions & 0 deletions src/helpers/ci/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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: false,
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/'),
name: github.repository_ref.replace('refs/pull/', '').replace('/merge', '')
}
}

return github
}

module.exports = {
info
}
37 changes: 37 additions & 0 deletions src/helpers/ci/gitlab.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 gitlab = {
ci: 'GITLAB',
git: 'GITLAB',
repository_url: ENV.CI_PROJECT_URL,
repository_name: ENV.CI_PROJECT_NAME,
repository_ref: ENV.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME || (`/-/tree/` + 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: false,
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: ENV.CI_OPEN_MERGE_REQUESTS ? "#" + pr_number : "",
url: ENV.CI_PROJECT_URL + "/-/merge_requests/" + pr_number
}
}

return gitlab
}

module.exports = {
info
}
59 changes: 59 additions & 0 deletions src/helpers/ci/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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: ENV.TEST_BEATS_PR || false,
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
Loading