Skip to content

Commit

Permalink
fix: Git slug - support generic SSH URL patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Dec 30, 2023
1 parent fdf0b6a commit d7bf03c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
16 changes: 9 additions & 7 deletions src/__tests__/utils-extract-repo-slug.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const { extractRepoSlug } = require('../utils');
const { getSlugFromGitURL } = require('../utils');

describe('Utils - extractRepoSlug', () => {
describe('Utils - getSlugFromGitURL', () => {
test('should fallback when the data is missing or invalid', () => {
expect(extractRepoSlug(undefined)).toEqual('');
expect(extractRepoSlug('')).toEqual('');
expect(extractRepoSlug('invalid-url')).toEqual('');
expect(getSlugFromGitURL(undefined)).toEqual(undefined);
expect(getSlugFromGitURL('')).toEqual(undefined);
expect(getSlugFromGitURL('invalid-url')).toEqual(undefined);
});

test('should extract slug from ssh URLs', () => {
expect(extractRepoSlug('git@github.com:relative-ci/agent.git')).toEqual('relative-ci/agent');
expect(getSlugFromGitURL('git@github.com:relative-ci/agent.git')).toEqual('relative-ci/agent');
expect(getSlugFromGitURL('user@host.com:9418/relative-ci/agent.git')).toEqual('relative-ci/agent');
expect(getSlugFromGitURL('user@192.169.0.1:9418/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');
expect(getSlugFromGitURL('https://github.com/relative-ci/agent.git')).toEqual('relative-ci/agent');
});
});
30 changes: 15 additions & 15 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,33 @@ export function getCommitMessage() {
return message;
}

// Match slug on `git@github.com:relative-ci/agent.git`
const GIT_SSH_URL_SLUG_PATTERN = /^git@(?:.*):(.*)\.git$/;
// Match slug on SSH URLs (ex: `USER@HOST:PORT/ORG/REPO.git`)
const GIT_SSH_URL_SLUG_PATTERN = /^(?:.*)@(?:.*):(?:\d+\/)?(.*)\.git$/;

// Match slug on `/relative-ci/agent.git`
// Match slug on HTTP(S) URLs `https://github.com/semantic-release/env-ci.git`
const GIT_PATHNAME_SLUG_PATTERN = /^\/(.*)\.git$/;

/**
* Extract repository slug(owner/repo) from the repo URL
*
* @param {string} repoURL
* @returns {string}
* @param {string} repositoryURL
* @returns {string|undefined}
*/
export function extractRepoSlug(repoURL) {
if (!repoURL) {
return '';
export function getSlugFromGitURL(repositoryURL) {
if (!repositoryURL) {
return undefined;
}

if (repoURL.match(/^git@/)) {
return repoURL.replace(GIT_SSH_URL_SLUG_PATTERN, '$1');
if (repositoryURL.match(GIT_SSH_URL_SLUG_PATTERN)) {
return repositoryURL.replace(GIT_SSH_URL_SLUG_PATTERN, '$1');
}

try {
const url = new URL(repoURL);
const url = new URL(repositoryURL);
return url.pathname.replace(GIT_PATHNAME_SLUG_PATTERN, '$1');
} catch (err) {
console.warn(err.message);
return '';
console.warn(err.message); // eslint-disable-line no-console
return undefined;
}
}

Expand All @@ -91,13 +91,13 @@ function resolveSlug(envVars) {
// https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables
// https://plugins.jenkins.io/git/#plugin-content-environment-variables
if ('service' in envVars && envVars.service === 'jenkins') {
return extractRepoSlug(process.env.GIT_URL);
return getSlugFromGitURL(process.env.GIT_URL);
}

// env-ci does not read repository slug, but buildkite org/project
// https://buildkite.com/docs/pipelines/environment-variables#BUILDKITE_REPO
if ('service' in envVars && envVars.service === 'buildkite') {
return extractRepoSlug(process.env.BUILDKITE_REPO);
return getSlugFromGitURL(process.env.BUILDKITE_REPO);
}

return 'slug' in envVars ? envVars.slug : '';
Expand Down

0 comments on commit d7bf03c

Please sign in to comment.