Skip to content

Instantly share code, notes, and snippets.

@Magnus167
Last active March 26, 2024 09:28
Show Gist options
  • Save Magnus167/b4188738d5767d87d337fa04ae23e640 to your computer and use it in GitHub Desktop.
Save Magnus167/b4188738d5767d87d337fa04ae23e640 to your computer and use it in GitHub Desktop.
Print list of PRs for a GitHub branch
import json
import urllib.request
import sys
import os
# 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")
headers = {"Authorization": f"token {os.getenv('GH_TOKEN', None)}"}
d = {"headers": headers} if headers["Authorization"] else {}
found = False
groups = [[], []]
for ix, ux in enumerate(
[
{
"url": dx["html_url"],
"title": dx["title"],
"base": dx["base"]["ref"],
"head": dx["head"]["ref"],
}
for dx in json.loads(
urllib.request.urlopen(
urllib.request.Request(
f"https://api.github.com/repos/{repo}/pulls", **d
)
)
.read()
.decode("utf-8")
)
# if (dx["head"]["ref"] == BRANCH or dx["base"]["ref"] == BRANCH)
]
):
if BRANCH in [ux["base"], ux["head"]]:
found = True
groups[0 if ux["head"] == BRANCH else 1].append(ux)
if not found:
print("No pull request found for branch", BRANCH, "in", REPO)
sys.exit(1)
def _fprint(group: list, msg: str):
if len(group) == 0:
return
print(msg)
for ix, ux in enumerate(group):
# print("\t", f"{ix+1}. ", end="")
print("\t", " - ", end="")
print(ux["title"])
print("\t\t", "Number: " + ux["url"].split("/")[-1])
print("\t\t", f"[{ux['base']} ← {ux['head']}]")
print("\t\t", ux["url"], "\n")
_fprint(groups[0], f"PRs from branch: {len(groups[0])}")
_fprint(groups[1], f"PRs to branch: {len(groups[1])}")
import json;import urllib.request;REPO='<REPO/OWNER>';BRANCH = '<CURRENT_BRANCH>'; [print('\t', f'{ix+1}.', ux) for ix, ux in enumerate(['\t' + dx['html_url'] 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])]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment