Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the PR for a branch from the command line

Ari Sweedler
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 20, 2023

When I push to a remote branch that has a PR open, bitbucket responds with

remote:
remote: View pull request for feature => main:
remote: https://stash.my_company.io/projects/my_proj/repos/my_code/pull-requests/1234
remote:

I can parse this output and `open 'https://stash.my_company.io/projects/my_proj/repos/my_code/pull-requests/1234'` from the command line.

However, if I don't have anything to push but I still want to open up my PR from the command line, I cannot figure out how to do it.

***

I want to be able to learn what PR my current branch has open. How to ask the server this?

1 answer

0 votes
Ari Sweedler
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 20, 2023

I have a script that helps me open up a PR from the command line.

#!/bin/bash
function browser_pr_for_current_branch_in_pwd_stash() {
local spec repo branch
if ! branch="$(git branch --show-current)"; then
echo "ERROR"
return 1
fi

# We want to extract the "spec" and "repo", then prepend 'users' or
# 'projects' to them depending on what type of repo it is (use the tilde to
# determine this)
IFS="," read -r spec repo < <(git remote -v | awk -F '[/ ]' '/fetch/ {print $4 "," $5}')
repo="${repo%.git}"
case "$spec" in
~*) spec="users/$(tr -d '~' <<< "$spec")" ;;
*) spec="projects/$(tr 'a-z' 'A-Z' <<< "$spec")" ;;
esac

# Build the URL and return the answer
echo "https://my_company.ilabs.io/${spec}/repos/$repo/compare/commits?sourceBranch=refs/heads/$branch"
}

if url="$(browser_pr_for_current_branch_in_pwd_stash "$PWD")"; then
open "$url"
fi

And I invoke it like so:

git pr

But what I'd like is that I could first query the server and see if there is already a PR open before trying to create a new one.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events