Skip to content

Instantly share code, notes, and snippets.

@amitsingh-007
Created July 4, 2020 06:34
Show Gist options
  • Save amitsingh-007/11d082db693cb65ddaaa56e73e9fa7eb to your computer and use it in GitHub Desktop.
Save amitsingh-007/11d082db693cb65ddaaa56e73e9fa7eb to your computer and use it in GitHub Desktop.
Get base branch name of current pull request raised
/*
* Only works if github cli is installed and logged in
*/
function getPRDetails() {
exec("gh pr view").then(
stdout => {
const response = stdout.toString();
const prDetails = response.match(/into (\S+) from (\S+)/i);
const [, baseBranch, targetBranch] = prDetails;
console.log("Base branch: ", baseBranch);
console.log("Target branch: ", targetBranch);
},
err => {
console.log("Failed to fetch PR details. Install Github CLI in order to get base branch / No open PR for this branch");
}
);
}
@pritam-patil
Copy link

Could you help with how you're using the exec command with .then?
I tried using a promise and shelljs exec (also child_process exec) but the command gh pr view gives different output when I ran it on terminal vs in the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment