Skip to content

Add support for running local action against self-hosted GitHub remotes #177

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 18 additions & 10 deletions local.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ function deleteBranch(remote, branchName) {
}

function githubInfo(remote) {
const gitHubSSH = "git@github.com:";
const gitHubHTTPS = "https://github.com/";
const repoSSH = "git@";
const repoHTTPS = "https://";
/* Expecting to match something like:
* 'fork git@github.com:seebees/aws-codebuild-run-build.git (push)'
* 'fork git@<hostname>:seebees/aws-codebuild-run-build.git (push)'
* Which is the output of `git remote -v`
*/
const remoteMatch = new RegExp(`^${remote}.*\\(push\\)$`);
Expand All @@ -137,13 +137,21 @@ function githubInfo(remote) {
.filter((line) => line.trim().match(remoteMatch));
assert(gitRemote, `No remote found named ${remote}`);
const [, url] = gitRemote.split(/[\t ]/);
if (url.startsWith(gitHubHTTPS)) {
const [owner, repo] = url.slice(gitHubHTTPS.length, -4).split("/");
return { owner, repo };
} else if (url.startsWith(gitHubSSH)) {
const [owner, repo] = url.slice(gitHubSSH.length, -4).split("/");
return { owner, repo };
const url_path_elements = [];

if (url.startsWith(repoHTTPS)) {
url_path_elements.push(...url.slice(repoHTTPS.length, -4).split("/"));
} else if (url.startsWith(repoSSH)) {
url_path_elements.push(...url.slice(repoSSH.length, -4).split(':').pop().split("/"));
} else {
throw new Error(`Unsupported format: ${url}`);
throw new Error(`Unsupported format - not a valid HTTPS or SSH URL: ${url}`);
}

if (url_path_elements.length < 2) {
throw new Error(`Unsupported format - could not find owner and repo in path: ${url}`);
}

// The last two elements of any valid git remote URL path should be the owner and repo.
const {owner, repo} = url_path_elements.slice(-2);
return { owner, repo };
}