Skip to content

Commit 97c7b65

Browse files
committed
767: add token from env, and exception handler
1 parent 5482946 commit 97c7b65

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

triage/weekly_report.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datetime import date
55
from enum import Enum
66
from math import log
7+
from os import getenv
78
from pathlib import Path
89
from pprint import pp
910
from sys import exit
@@ -58,8 +59,6 @@ def eprint(*args, **kwargs):
5859
'mixed': [],
5960
}
6061

61-
pr_titles = dict()
62-
6362

6463
def get_username():
6564
usernames = {'mackendy': 'ecstaticmorse', 'joshua': 'jyn514'}
@@ -175,14 +174,23 @@ def gh_link(pr):
175174

176175

177176
def gh_pr_title(pr):
178-
if pr in pr_titles:
179-
return pr_titles.get(pr)
177+
def make_req():
178+
url = f'https://api.github.com/repos/rust-lang/rust/pulls/{pr}'
179+
req = urllib.request.Request(url)
180+
req.add_header('Content-Type', 'application/json')
181+
req.add_header('Authorization', f'token {getenv("GITHUB_TOKEN")}')
182+
183+
with urllib.request.urlopen(req) as f:
184+
data = json.loads(f.read())
185+
return data.get('title', '')
180186

181-
url = f'https://api.github.com/repos/rust-lang/rust/pulls/{pr}'
182-
with urllib.request.urlopen(url) as f:
183-
data = json.loads(f.read())
184-
pr_titles[pr] = data['title']
185-
return pr_titles[pr]
187+
result = ''
188+
try:
189+
result = make_req()
190+
except urllib.error.HTTPError as e:
191+
eprint(e)
192+
finally:
193+
return result
186194

187195

188196
def compare_link(start, end, stat):
@@ -195,7 +203,7 @@ def write_section(res, *changes):
195203
end = res['b']['commit']
196204
title = gh_pr_title(pr)
197205

198-
msg = f'{title} [#{pr}]({gh_link(pr)})'
206+
msg = f'{title}[#{pr}]({gh_link(pr)})'
199207

200208
for change in changes:
201209
msg += '\n- '

0 commit comments

Comments
 (0)