Skip to content

Commit

Permalink
fix: Extract repo slug from git origin URL
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Dec 6, 2022
1 parent 75bfa10 commit 18948b6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
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');
});
});
6 changes: 4 additions & 2 deletions 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, getGitSlug,
} from './utils';

const DEFAULT_ENDPOINT = 'https://api.relative-ci.com/save';
const WEBPACK_STATS = 'webpack.stats';
Expand All @@ -25,7 +27,7 @@ export const agent = (artifactsData, config, args = {}, logger = console) => {

// Resolved params
const envVars = {
slug: args.slug || process.env.RELATIVE_CI_SLUG || envCIVars.slug,
slug: args.slug || process.env.RELATIVE_CI_SLUG || envCIVars.slug || getGitSlug(),
// env-ci is reporting the branch of the PR as prBranch
branch: args.branch || envCIVars.prBranch || envCIVars.branch,
pr: args.pr || envCIVars.pr,
Expand Down
46 changes: 46 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,50 @@ module.exports.getCommitMessage = () => childProcess
.execSync('git log -1 --pretty=%B')
.toString().trim();

// 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}
*/
module.exports.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.getGitSlug = () => {
let repoURL = '';

try {
repoURL = childProcess
.execSync('git config --get remote.config.url')
.toString()
.trim();
} catch (err) {
console.warn(err.message);
return '';
}

return module.exports.extractRepoSlug(repoURL);
};

module.exports.getEnvCI = () => pick(envCI(), CI_ENV_VAR_NAMES);

0 comments on commit 18948b6

Please sign in to comment.