@relative-ci/agent
Version:
Send bundle stats and CI build information to RelativeCI
47 lines (45 loc) • 1.76 kB
JavaScript
// Match slug on SSH URLs (ex: `USER@HOST:PORT/ORG/REPO.git`)
const GIT_SSH_URL_SLUG_PATTERN = /^(?:.*)@(?:.*):(?:\d+\/)?(.*)\.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
*/
function getSlugFromGitURL(repositoryURL) {
if (!repositoryURL) {
return undefined;
}
if (repositoryURL.match(GIT_SSH_URL_SLUG_PATTERN)) {
return repositoryURL.replace(GIT_SSH_URL_SLUG_PATTERN, '$1');
}
try {
const url = new URL(repositoryURL);
return url.pathname.replace(GIT_PATHNAME_SLUG_PATTERN, '$1');
}
catch (err) {
if (err instanceof Error) {
console.warn(err.message);
}
return undefined;
}
}
/**
* Resolve repository slug
*/
function getSlug(envVars) {
// 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 ('service' in envVars && envVars.service === 'jenkins') {
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 getSlugFromGitURL(process.env.BUILDKITE_REPO || '') || '';
}
return 'slug' in envVars ? envVars.slug : '';
}
export { getSlug, getSlugFromGitURL };
//# sourceMappingURL=get-slug.js.map