17
17
import textwrap
18
18
try :
19
19
import urllib2
20
+ from urllib2 import HTTPError
20
21
except ImportError :
21
22
import urllib .request as urllib2
23
+ from urllib .error import HTTPError
24
+ try :
25
+ import typing
26
+ except ImportError :
27
+ pass
22
28
23
29
# List of people to ping when the status of a tool or a book changed.
24
30
# These should be collaborators of the rust-lang/rust repository (with at least
63
69
}
64
70
65
71
def load_json_from_response (resp ):
72
+ # type: (typing.Any) -> typing.Any
66
73
content = resp .read ()
67
74
if isinstance (content , bytes ):
68
- content = content .decode ('utf-8' )
75
+ content_str = content .decode ('utf-8' )
69
76
else :
70
77
print ("Refusing to decode " + str (type (content )) + " to str" )
71
- return json .loads (content )
78
+ return json .loads (content_str )
72
79
73
80
def validate_maintainers (repo , github_token ):
81
+ # type: (str, str) -> None
74
82
'''Ensure all maintainers are assignable on a GitHub repo'''
75
83
next_link_re = re .compile (r'<([^>]+)>; rel="next"' )
76
84
77
85
# Load the list of assignable people in the GitHub repo
78
- assignable = []
79
- url = 'https://api.github.com/repos/%s/collaborators?per_page=100' % repo
86
+ assignable = [] # type: typing.List[str]
87
+ url = 'https://api.github.com/repos/' \
88
+ + '%s/collaborators?per_page=100' % repo # type: typing.Optional[str]
80
89
while url is not None :
81
90
response = urllib2 .urlopen (urllib2 .Request (url , headers = {
82
91
'Authorization' : 'token ' + github_token ,
@@ -116,9 +125,10 @@ def validate_maintainers(repo, github_token):
116
125
117
126
118
127
def read_current_status (current_commit , path ):
128
+ # type: (str, str) -> typing.Mapping[str, typing.Any]
119
129
'''Reads build status of `current_commit` from content of `history/*.tsv`
120
130
'''
121
- with open (path , 'rU ' ) as f :
131
+ with open (path , 'r ' ) as f :
122
132
for line in f :
123
133
(commit , status ) = line .split ('\t ' , 1 )
124
134
if commit == current_commit :
@@ -127,10 +137,12 @@ def read_current_status(current_commit, path):
127
137
128
138
129
139
def gh_url ():
140
+ # type: () -> str
130
141
return os .environ ['TOOLSTATE_ISSUES_API_URL' ]
131
142
132
143
133
144
def maybe_delink (message ):
145
+ # type: (str) -> str
134
146
if os .environ .get ('TOOLSTATE_SKIP_MENTIONS' ) is not None :
135
147
return message .replace ("@" , "" )
136
148
return message
@@ -143,8 +155,10 @@ def issue(
143
155
relevant_pr_number ,
144
156
relevant_pr_user ,
145
157
labels ,
158
+ github_token ,
146
159
):
147
- # Open an issue about the toolstate failure.
160
+ # type: (str, str, typing.Iterable[str], str, str, typing.List[str], str) -> None
161
+ '''Open an issue about the toolstate failure.'''
148
162
if status == 'test-fail' :
149
163
status_description = 'has failing tests'
150
164
else :
@@ -168,7 +182,7 @@ def issue(
168
182
print ("Creating issue:\n {}" .format (request ))
169
183
response = urllib2 .urlopen (urllib2 .Request (
170
184
gh_url (),
171
- request ,
185
+ request . encode () ,
172
186
{
173
187
'Authorization' : 'token ' + github_token ,
174
188
'Content-Type' : 'application/json' ,
@@ -183,8 +197,10 @@ def update_latest(
183
197
relevant_pr_url ,
184
198
relevant_pr_user ,
185
199
pr_reviewer ,
186
- current_datetime
200
+ current_datetime ,
201
+ github_token ,
187
202
):
203
+ # type: (str, str, str, str, str, str, str) -> str
188
204
'''Updates `_data/latest.json` to match build result of the given commit.
189
205
'''
190
206
with open ('_data/latest.json' , 'r+' ) as f :
@@ -243,13 +259,14 @@ def update_latest(
243
259
if create_issue_for_status is not None :
244
260
try :
245
261
issue (
246
- tool , create_issue_for_status , MAINTAINERS .get (tool , '' ),
247
- relevant_pr_number , relevant_pr_user , LABELS .get (tool , '' ),
262
+ tool , create_issue_for_status , MAINTAINERS .get (tool , ()),
263
+ relevant_pr_number , relevant_pr_user , LABELS .get (tool , []),
264
+ github_token ,
248
265
)
249
- except urllib2 . HTTPError as e :
266
+ except HTTPError as e :
250
267
# network errors will simply end up not creating an issue, but that's better
251
268
# than failing the entire build job
252
- print ("HTTPError when creating issue for status regression: {0}\n {1}"
269
+ print ("HTTPError when creating issue for status regression: {0}\n {1!r }"
253
270
.format (e , e .read ()))
254
271
except IOError as e :
255
272
print ("I/O error when creating issue for status regression: {0}" .format (e ))
@@ -318,7 +335,8 @@ def update_latest(
318
335
relevant_pr_url ,
319
336
relevant_pr_user ,
320
337
pr_reviewer ,
321
- cur_datetime
338
+ cur_datetime ,
339
+ github_token ,
322
340
)
323
341
if not message :
324
342
print ('<Nothing changed>' )
@@ -337,13 +355,13 @@ def update_latest(
337
355
issue_url = gh_url () + '/{}/comments' .format (number )
338
356
response = urllib2 .urlopen (urllib2 .Request (
339
357
issue_url ,
340
- json .dumps ({'body' : maybe_delink (message )}),
358
+ json .dumps ({'body' : maybe_delink (message )}). encode () ,
341
359
{
342
360
'Authorization' : 'token ' + github_token ,
343
361
'Content-Type' : 'application/json' ,
344
362
}
345
363
))
346
364
response .read ()
347
- except urllib2 . HTTPError as e :
348
- print ("HTTPError: %s\n %s " % (e , e .read ()))
365
+ except HTTPError as e :
366
+ print ("HTTPError: %s\n %r " % (e , e .read ()))
349
367
raise
0 commit comments