Skip to content

Instantly share code, notes, and snippets.

@Magnus167
Last active March 11, 2024 17:31
Show Gist options
  • Save Magnus167/620024d7460fa32f6e7b7140ccf6c735 to your computer and use it in GitHub Desktop.
Save Magnus167/620024d7460fa32f6e7b7140ccf6c735 to your computer and use it in GitHub Desktop.
Print off pull requests associated with a branch (Github only)
[alias]
pr = "!f() { BRANCH=$(git branch --show-current); REPOURL=$(git config --get remote.origin.url); python ~/Code/work_HK/getpr.py $REPOURL $BRANCH; }; f"
git config --global alias.pr '!f() { BRANCH=$(git branch --show-current); REPOURL=$(git config --get remote.origin.url); python ~/Code/work_HK/getpr.py $REPOURL $BRANCH; }; f'
import json
import urllib.request
import sys
# get repo and branch from sys.argv
if len(sys.argv) < 3:
print("Usage: python getpr.py <repo> <branch>")
sys.exit(1)
REPO = sys.argv[1].strip()
BRANCH = sys.argv[2].strip()
repo = REPO.replace(".git", "").replace("https://github.com/", "")
print("\nrepo:", repo, "\nbranch:", BRANCH, "\n")
found = False
for ix, ux in enumerate(
[
"\t" + dx["html_url"] + ' -- ' + dx["title"]
for dx in json.loads(
urllib.request.urlopen(
urllib.request.Request(f"https://api.github.com/repos/{repo}/pulls")
)
.read()
.decode("utf-8")
)
if dx["head"]["ref"] == BRANCH
]
):
print("\t", f"{ix+1}.", ux, "\n")
found = True
if not found:
print("No pull request found for branch", BRANCH, "in", REPO)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment