-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_prs.py
executable file
·180 lines (122 loc) · 5.14 KB
/
make_prs.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
import json
import os
import requests
import sys
import time
import re
from config import *
import config
def make_request(_url, _user, _data=None, _type='GET', _headers={}):
_headers['Authorization'] = 'token {0}'.format(get_github_token(_user))
url = 'https://api.github.com/repos/{0}'.format(GITHUB_REPO) + _url
time.sleep(2)
response = requests.request(_type, url, data=_data, headers=_headers)
return response
def make_pr(title, body, head, base, user, date):
# Create a PR on github.com using the given parameters
realuser = get_github_username(user)
orig_pr_num = head.split('/')[2]
body = '*Original PR: https://charm.cs.illinois.edu/gerrit/{0}'.format(orig_pr_num) + "*\n\n---\n" + body
body = '*Original date: {0}*\n'.format(date) + body
if not gerrit_user_has_token(user):
if gerrit_user_has_github_name(user):
body = "*Original author: " + user + " (@" + github_usermap[user] + ")*\n" + body
else:
body = "*Original author: " + user + "*\n" + body
data = {'title': title,
'body': body,
'head': head,
'base': base,
'maintainer_can_modify': True,
}
payload = json.dumps(data)
response = make_request('/pulls', realuser, _data=payload, _type="POST")
if response.status_code != 201:
print('Could not create pull request: title:"{0}", user: {1}, realuser: {2}, payload: {3}'.format(title,user,realuser,payload))
print('Response:', response.content)
sys.exit(1)
else:
data = json.loads(response.text)
myurl = data['html_url']
if "X-RateLimit-Remaining" in response.headers:
print(' PR: "{0}" {1} ({2})'.format(title.split('\n')[0][:40], myurl, response.headers["X-RateLimit-Remaining"]))
else:
print(' PR: "{0}" {1}'.format(title.split('\n')[0][:40]), myurl)
def gerrit_user_has_token(gerrit_user):
if gerrit_user in github_usermap:
github_user = github_usermap[gerrit_user]
if github_user in github_tokenmap:
return True
return False
def gerrit_user_has_github_name(gerrit_user):
if gerrit_user in github_usermap:
return True
return False
unknown_github_username = set()
unknown_github_token = set()
def get_github_username(gerrit_user):
if gerrit_user in github_usermap:
return(github_usermap[gerrit_user])
else:
if gerrit_user not in unknown_github_username:
print(' Gerrit user "{0}" not in github_usermap, using default GitHub user "{1}".'.format(gerrit_user, github_default_username))
unknown_github_username.add(gerrit_user)
return(github_default_username)
def get_github_token(github_user):
if github_user in github_tokenmap:
return(github_tokenmap[github_user])
else:
if github_user not in unknown_github_token:
print(' GitHub user "{0}" not in github_tokenmap, using token for default GitHub user "{1}".'.format(github_user, github_default_username))
unknown_github_token.add(github_user)
return(github_tokenmap[github_default_username])
def list_branches():
res = []
response = make_request('/branches?per_page=100', github_default_username)
data = json.loads(response.text)
for k in data:
name = k['name']
if name.startswith('review/'):
res.append(name)
while 'next' in response.links:
response = requests.get(response.links['next']['url'])
data = json.loads(response.text)
for k in data:
name = k['name']
if name.startswith('review/'):
res.append(name)
return res
def get_default_branch():
response = make_request("", github_default_username)
data = json.loads(response.text)
return data['default_branch']
def get_branch_data(branch):
response = make_request('/git/refs/heads/{0}'.format(branch), github_default_username)
data = json.loads(response.text)
commit = data['object']['sha']
response = make_request('/git/commits/{0}'.format(commit), github_default_username)
data = json.loads(response.text)
author = data['committer']['name']
text = data['message'].splitlines()
date = data['committer']['date'].replace("T", " ").replace("Z", "")
title = text[0]
body = '\n'.join(text[1:])
return(author, title, body, date)
print('=' * 80)
branches = list_branches()
# branches = [ "review/yan_ming_li/761" ]
def_branch = get_default_branch()
print('Creating {0} pull requests in GitHub repository "{1}". Base branch: "{2}"'.format(len(branches), GITHUB_REPO, def_branch))
print('=' * 80)
for branch in branches:
author, title, body, date = get_branch_data(branch)
make_pr(title, body, branch, def_branch, author, date)
print('=' * 80)
print('Finished.')
if len(unknown_github_username) > 0:
print('The following Gerrit users did not have a GitHub username associated with them:')
print(unknown_github_username)
if len(unknown_github_token) > 0:
print('The following GitHub users did not have a GitHub token associated with them:')
print(unknown_github_token)