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: Git slug - support generic SSH URL patterns #1056

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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/relative-ci/agent.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