forked from kubernetes/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_pr.py
executable file
·89 lines (77 loc) · 2.69 KB
/
find_pr.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
#!/usr/bin/env python
import os
import json
import click
import requests
from jinja2 import Template
@click.command()
@click.argument("path")
@click.option("--tags",
multiple=True,
help="Tags of PullRequest (Can be passed multiple times)")
@click.option("--token",
help="GitHub API token. (Default env variable GITHUB_TOKEN)",
default=os.environ.get("GITHUB_TOKEN", ""))
@click.option("--last-n-pr",
help="Last n-th PullRequests",
default=100)
def main(tags, token, path, last_n_pr):
"""
Find what GitHub pull requests touch a given file.
ex:
./find_pr.py --tags "language/fr" "content/fr/_index.html"
"""
if not token:
print("GitHub token not provided (required)")
exit(1)
query = Template("""
query {
repository(name: "website", owner: "kubernetes") {
pullRequests({% if tags %}labels: [{% for tag in tags %}"{{ tag }}", {% endfor %}], {% endif %}last: {{ last_n_pr }}) {
edges {
node {
title
state
url
files (last: 100) {
edges {
node {
path
}
}
}
}
}
}
}
}
""").render(tags=tags, last_n_pr=last_n_pr)
try:
r = requests.post("https://api.github.com/graphql",
json={"query": query},
headers={
"Authorization": "token %s" % token,
"Accept": "application/vnd.github.ocelot-preview+json",
"Accept-Encoding": "gzip"
})
r.raise_for_status()
reply = r.json()
prs = reply['data']['repository']['pullRequests']['edges']
for pr in prs:
files = pr["node"]["files"]["edges"]
for f in files:
if path == f["node"]["path"]:
print("%s (%s)" % (pr["node"]["title"], pr["node"]["state"]))
print(pr["node"]["url"])
print("----------------")
except requests.exceptions.HTTPError as err:
gh_err_response = json.loads(err.response.text)
print("HTTP Error: %d %s" % (err.response.status_code, gh_err_response['message']))
except requests.exceptions.ConnectionError as err:
print("Error Connecting: %s" % err)
except requests.exceptions.Timeout as err:
print("Timeout Error: %s" % err)
except requests.exceptions.RequestException as err:
print("Oops, another error occurred: %s" % err)
if __name__ == '__main__':
main()