Last active
March 11, 2024 17:31
-
-
Save Magnus167/620024d7460fa32f6e7b7140ccf6c735 to your computer and use it in GitHub Desktop.
Print off pull requests associated with a branch (Github only)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[alias] | |
pr = "!f() { BRANCH=$(git branch --show-current); REPOURL=$(git config --get remote.origin.url); python ~/Code/work_HK/getpr.py $REPOURL $BRANCH; }; f" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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