-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.py
executable file
·94 lines (79 loc) · 2.58 KB
/
sync.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
#!/usr/bin/env python3
import json
import subprocess
import argparse
import traceback
from datetime import datetime, timedelta
parser = argparse.ArgumentParser(description='Process some integers.')
thirty_days_ago = datetime.today() - timedelta(days=30)
START_DATE = thirty_days_ago
DEFAULT_TIME_DELTA = timedelta(days=10)
def FormatDate(d):
return d.strftime('%Y-%m-%d')
def InvokeGH(cmd):
print(cmd)
output = subprocess.check_output(cmd, shell=True)
return json.loads(output)
PR_COLUMNS = [
'additions',
'assignees',
'author',
'closed',
'closedAt',
'createdAt',
'deletions',
'isDraft',
'labels',
'mergedAt',
'mergedBy',
'number',
'state',
'title',
'updatedAt',
'url',]
def GetPullRequestsWithFallback(gh_bin, start_date, owner, repo):
try:
return GetPullRequests(gh_bin, start_date, owner, repo)
except:
traceback.print_exc()
return GetPullRequests(gh_bin, start_date, owner, repo, step=timedelta(days=10))
def GetPullRequests(gh_bin, start_date, owner, repo, step=None):
prs = []
if step:
start = start_date
while start <= datetime.today():
start_str = FormatDate(start)
end_str = FormatDate(start + step)
prs.extend(InvokeGH(f'{gh_bin} pr list -L 1000 -R {owner}/{repo} -s all -S "created:>={start_str} created:<{end_str}" --json={",".join(PR_COLUMNS)}'))
start += step
else:
start_str = FormatDate(start_date)
prs = InvokeGH(f'{gh_bin} pr list -L 1000 -R {owner}/{repo} -s all -S "created:>={start_str}" --json={",".join(PR_COLUMNS)}')
for pr in prs:
pr['repo'] = repo
pr['owner'] = owner
pr['author'] = pr['author']['login']
for k, v in pr.items():
if isinstance(v, list) or isinstance(v, dict):
pr[k] = str(v)
return prs
def GetRepos(gh_bin, owner):
repos = InvokeGH('{} repo list -L 1000 {} --json=name'.format(gh_bin, owner))
return (x['name'] for x in repos)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='github PR sync')
parser.add_argument('--owners', type=str, nargs='?', default='tidbcloud')
parser.add_argument('--gh_bin', type=str, nargs='?', default='gh')
parser.add_argument('--out', type=str, nargs='?', default='/tmp/result.json')
args = parser.parse_args()
result = []
for owner in args.owners.strip().split(','):
repos = list(GetRepos(args.gh_bin, owner))
for repo in repos:
print(owner, repo)
result.extend(GetPullRequestsWithFallback(args.gh_bin, START_DATE, owner, repo))
print(len(result))
with open(args.out, 'w') as fp:
for x in result:
json.dump(x, fp)
fp.write('\n')