-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
conflicted.py
108 lines (83 loc) · 2.8 KB
/
conflicted.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
"""
Find which of my PRs have merge conflicts.
"""
from __future__ import annotations
import argparse
import datetime
import sys
import time
from pprint import pprint
import requests
from termcolor import colored
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def bleep(url):
"""Call the API and return JSON and next URL"""
# print(url)
r = requests.get(url)
# pprint(r)
try:
next_url = r.links["next"]["url"]
except KeyError:
next_url = None
if int(r.headers["X-Ratelimit-Remaining"]) < 12:
print("r.status_code", r.status_code)
print("X-Ratelimit-Limit", r.headers["X-Ratelimit-Limit"])
print("X-Ratelimit-Remaining", r.headers["X-Ratelimit-Remaining"])
print("X-RateLimit-Reset", r.headers["X-RateLimit-Reset"])
remaining_time = int(r.headers["X-RateLimit-Reset"]) - time.time()
print(remaining_time, "seconds")
print(remaining_time / 60, "minutes")
reset_time = datetime.datetime.fromtimestamp(
int(int(r.headers["X-RateLimit-Reset"]))
).strftime("%Y-%m-%d %H:%M:%S")
print(reset_time)
if r.status_code == 200:
return r.json(), next_url
elif r.status_code == 403:
if int(r.headers["X-Ratelimit-Remaining"]) == 0:
eprint(colored("Rate limit exceeded", "red"))
sys.exit(403)
return None, None
def get_prs(start_url):
issues = []
# Fetch all issues from GitHub
next_url = start_url
while True:
data, next_url = bleep(next_url)
new_issues = data["items"]
print("total_count", data["total_count"])
for issue in new_issues:
# pprint(issue)
pr_url = issue["pull_request"]["url"]
pr_data, _ = bleep(pr_url)
# pprint(pr_data)
if pr_data["mergeable"]:
# print("mergeable:\t{}".format(issue["html_url"]))
pass
else:
print(colored("unmergeable:\t{}".format(issue["html_url"]), "red"))
issues.append(issue)
if not next_url:
break
return issues
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Find which of my PRs have merge conflicts.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-a", "--author", default="hugovk", help="Find PRs created by this user"
)
args = parser.parse_args()
# https://developer.github.com/v3/search/#search-issues
start_url = (
f"https://api.github.com/search/issues?q=is:pr+author:{args.author}"
"+sort:updated-asc+is:open"
)
print(start_url)
prs = get_prs(start_url)
pprint(prs)
print(f"{len(prs)} total unmergeable PRs")
# End of file