-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_forks.py
executable file
·90 lines (82 loc) · 2.47 KB
/
github_forks.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
#!/usr/bin/env python3
import requests
from collections import namedtuple
from sys import argv
Repository = namedtuple('Repository', 'name stars forks last_change')
ENDPOINT = "https://api.github.com/graphql"
AUTH_TOKEN = "***" # If you want to generate your own, see there
# https://docs.github.com/en/graphql/guides/forming-calls-with-graphql#authenticating-with-graphql and give it the public_repo scope
GRAPHQL_QUERY = '''query Forks($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
nameWithOwner
stargazerCount
forkCount
object(expression: \"master\") {
... on Commit {
committedDate
history {
totalCount
__typename
}
__typename
}
__typename
}
forks(first: 100, orderBy: {field: STARGAZERS, direction: DESC}) {
nodes {
nameWithOwner
stargazerCount
forkCount
object(expression: \"master\") {
... on Commit {
committedDate
history {
totalCount
__typename
}
__typename
}
__typename
}
__typename
}
__typename
}
__typename
}
}
'''
def queryGitHub(repository):
request = {
'operationName': 'Forks',
'variables': {
'owner': repository[0],
'name': repository[1]
},
'query': GRAPHQL_QUERY
}
response = requests.post(ENDPOINT, headers={'Authorization': f'Bearer {AUTH_TOKEN}'}, json=request)
return response.json()
def parseRepository(repro):
return Repository(
repro['nameWithOwner'],
repro['stargazerCount'],
repro['forkCount'],
repro['object']['committedDate'])
def fetchChildren(response):
forks = response['data']['repository']['forks']['nodes']
repos = []
for fork in forks:
repos += [parseRepository(fork)]
if fork['forkCount'] > 0:
repos += fetchChildren(queryGitHub(fork['nameWithOwner'].split('/')))
return repos
response = queryGitHub(argv[1].split('/')[-2:])
base = parseRepository(response['data']['repository'])
repositories = [base]
if base.forks > 0:
repositories += fetchChildren(response)
r = Repository('Name', 'Stars', 'Forks', 'LastChange')
print(f'{r.name:50s} {r.stars:5s} {r.forks:5s} {r.last_change}')
for r in sorted(repositories, key=lambda a: a.last_change, reverse=True):
print(f'{r.name:50s} {r.stars:5d} {r.forks:5d} {r.last_change}')