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: Extract repo slug from git origin URL #758

Merged
merged 1 commit into from
Dec 6, 2022
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
17 changes: 17 additions & 0 deletions src/__tests__/utils-extract-repo-slug.js
Original file line number Diff line number Diff line change
@@ -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('[email protected]: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');
});
});
4 changes: 3 additions & 1 deletion src/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
46 changes: 45 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `[email protected]: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;