-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Extract repo slug from git origin URL
- Loading branch information
Showing
3 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); |