-
Notifications
You must be signed in to change notification settings - Fork 27
/
octohatrack_graphql.py
97 lines (74 loc) · 2.26 KB
/
octohatrack_graphql.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
#!/usr/bin/env python
"""
Quick implementation of octhatrack with GraphQL
USAGE
./octohatrack_graphql.py user/repo
LIMITATIONS
Limitations in the github graphql api means that this will only return the:
- last 100 issues
- last 100 comments per issue
- last 100 pull requests
- last 100 comments per pull request
- last 100 commit comments
"""
import json
import os
import click
import requests
GITHUB_API = "https://api.github.com/graphql"
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")
HEADERS = {"Authorization": "token %s" % GITHUB_TOKEN}
graphql_query = """
query ($owner: String!, $name: String!, $history: Int!) {
repository(owner: $owner, name: $name) {
issues(last:$history) {
nodes {
author { login avatarUrl }
comments (last:$history){ nodes {author {login avatarUrl}}}
}
}
pullRequests(last: $history) {
edges { node {
author { avatarUrl login }
comments (last:$history){ nodes {author {login avatarUrl}}}
}}
}
commitComments(last: $history) {
edges { node { author { login avatarUrl }}}
}
}
}
"""
def reducejson(j):
"""
Not sure if there's a better way to walk the ... interesting result
"""
authors = []
for key in j["data"]["repository"]["commitComments"]["edges"]:
authors.append(key["node"]["author"])
for key in j["data"]["repository"]["issues"]["nodes"]:
authors.append(key["author"])
for c in key["comments"]["nodes"]:
authors.append(c["author"])
for key in j["data"]["repository"]["pullRequests"]["edges"]:
authors.append(key["node"]["author"])
for c in key["node"]["comments"]["nodes"]:
authors.append(c["author"])
unique = list({v["login"]: v for v in authors if v is not None}.values())
return unique
@click.command()
@click.argument("repo")
def main(repo):
owner, name = repo.split("/")
variables = {"owner": owner, "name": name, "history": 100}
result = requests.post(
GITHUB_API,
json.dumps({"query": graphql_query, "variables": variables}),
headers=HEADERS,
)
authors = reducejson(result.json())
for a in authors:
print(a)
print(len(authors))
if __name__ == "__main__":
main()