Skip to content

Commit

Permalink
Buildkite slug (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
vio authored Jan 6, 2024
1 parent 45fd9bf commit 6207db4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
29 changes: 29 additions & 0 deletions lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,32 @@ export function branch(options) {
return undefined;
}
}

// 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/semantic-release/env-ci.git`
const GIT_PATHNAME_SLUG_PATTERN = /^\/(.*)\.git$/;

/**
* Extract repository slug(owner/repo) from a repository URL
*
* @param {String} repositoryURL
* @returns {String | undefined}
*/
export 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 {
return undefined;
}
}
3 changes: 2 additions & 1 deletion services/buildkite.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// https://buildkite.com/docs/builds/environment-variables
import { getSlugFromGitURL } from "../lib/git.js";

export default {
detect({ env }) {
Expand All @@ -21,7 +22,7 @@ export default {
branch: isPr
? env.BUILDKITE_PULL_REQUEST_BASE_BRANCH
: env.BUILDKITE_BRANCH,
slug: `${env.BUILDKITE_ORGANIZATION_SLUG}/${env.BUILDKITE_PROJECT_SLUG}`,
slug: getSlugFromGitURL(env.BUILDKITE_REPO),
pr,
isPr,
prBranch: isPr ? env.BUILDKITE_BRANCH : undefined,
Expand Down
11 changes: 10 additions & 1 deletion test/lib/git.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from "ava";
import { head, branch } from "../../lib/git.js";
import { getSlugFromGitURL, head, branch } from "../../lib/git.js";
import {
gitRepo,
gitCommit,
Expand Down Expand Up @@ -38,3 +38,12 @@ test("Git cloned repository with detached head", async (t) => {
t.is(head({ cwd }), await gitHead({ cwd }));
t.is(branch({ cwd }), "master");
});

test("Slug from git URL", (t) => {
t.is(getSlugFromGitURL(""), undefined);
t.is(getSlugFromGitURL("invalid url"), undefined);
t.is(getSlugFromGitURL("https://github.com/owner/repo.git"), "owner/repo");
t.is(getSlugFromGitURL("[email protected]:owner/repo.git"), "owner/repo");
t.is(getSlugFromGitURL("[email protected]:9418/owner/repo.git"), "owner/repo");
t.is(getSlugFromGitURL("[email protected]:9418/owner/repo.git"), "owner/repo");
});
3 changes: 1 addition & 2 deletions test/services/buildkite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ const env = {
BUILDKITE_BRANCH: "master",
BUILDKITE_PULL_REQUEST: "false",
BUILDKITE_BUILD_CHECKOUT_PATH: "/",
BUILDKITE_ORGANIZATION_SLUG: "owner",
BUILDKITE_PROJECT_SLUG: "repo",
BUILDKITE_REPO: "[email protected]:owner/repo.git",
};

test("Push", (t) => {
Expand Down

0 comments on commit 6207db4

Please sign in to comment.