From 50f3093ff94282762417c9224df0f1f2ce8c4f3b Mon Sep 17 00:00:00 2001 From: Vio Date: Tue, 6 Dec 2022 20:07:56 +0100 Subject: [PATCH] fix: Extract repo slug from git origin URL --- src/__tests__/utils-extract-repo-slug.js | 17 +++++++++ src/agent.js | 4 ++- src/utils.js | 46 +++++++++++++++++++++++- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/__tests__/utils-extract-repo-slug.js diff --git a/src/__tests__/utils-extract-repo-slug.js b/src/__tests__/utils-extract-repo-slug.js new file mode 100644 index 00000000..cb86b88f --- /dev/null +++ b/src/__tests__/utils-extract-repo-slug.js @@ -0,0 +1,17 @@ +const { extractRepoSlug } = require('../utils'); + +describe('Utils - extractRepoSlug', () => { + test('should fallback when the data is missing or invalid', () => { + expect(extractRepoSlug(undefined)).toEqual(''); + expect(extractRepoSlug('')).toEqual(''); + expect(extractRepoSlug('invalid-url')).toEqual(''); + }); + + test('should extract slug from ssh URLs', () => { + expect(extractRepoSlug('git@github.com:relative-ci/agent.git')).toEqual('relative-ci/agent'); + }); + + test('should extract slug from http(s) URLs', () => { + expect(extractRepoSlug('https://github.com/relative-ci/agent.git')).toEqual('relative-ci/agent'); + }); +}); diff --git a/src/agent.js b/src/agent.js index 4dd45f49..00e85614 100644 --- a/src/agent.js +++ b/src/agent.js @@ -5,7 +5,9 @@ import filter from '@bundle-stats/plugin-webpack-filter'; import pck from '../package.json'; import * as LOCALES from '../locales/en'; import send from './send'; -import { debug, getCommitMessage, getEnvCI } from './utils'; +import { + debug, getCommitMessage, getEnvCI, +} from './utils'; const DEFAULT_ENDPOINT = 'https://api.relative-ci.com/save'; const WEBPACK_STATS = 'webpack.stats'; diff --git a/src/utils.js b/src/utils.js index 070273df..8ce195b5 100644 --- a/src/utils.js +++ b/src/utils.js @@ -20,4 +20,48 @@ module.exports.getCommitMessage = () => childProcess .execSync('git log -1 --pretty=%B') .toString().trim(); -module.exports.getEnvCI = () => pick(envCI(), CI_ENV_VAR_NAMES); +// Match slug on `git@github.com:relative-ci/agent.git` +const GIT_SSH_URL_SLUG_PATTERN = /^git@(?:.*):(.*)\.git$/; + +// Match slug on `/relative-ci/agent.git` +const GIT_PATHNAME_SLUG_PATTERN = /^\/(.*)\.git$/; + +/** + * Extract repository slug(owner/repo) from the repo URL + * + * @param {string} repoURL + * @returns {string} + */ +const extractRepoSlug = (repoURL) => { + if (!repoURL) { + return ''; + } + + if (repoURL.match(/^git@/)) { + return repoURL.replace(GIT_SSH_URL_SLUG_PATTERN, '$1'); + } + + try { + const url = new URL(repoURL); + return url.pathname.replace(GIT_PATHNAME_SLUG_PATTERN, '$1'); + } catch (err) { + console.warn(err.message); + return ''; + } +}; + +module.exports.getEnvCI = () => { + const envVars = pick(envCI(), CI_ENV_VAR_NAMES); + + // env-ci does not provide a slug for jenkins + // https://github.com/semantic-release/env-ci/blob/master/services/jenkins.js#LL18 + // https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables + // https://plugins.jenkins.io/git/#plugin-content-environment-variables + if (envVars.service === 'jenkins' && !envVars.slug) { + envVars.slug = extractRepoSlug(process.env.GIT_URL); + } + + return envVars; +}; + +module.exports.extractRepoSlug = extractRepoSlug;