Skip to content

Fix some Python2→3 error in publish_toolstate.py #82340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions src/tools/publish_toolstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
import textwrap
try:
import urllib2
from urllib2 import HTTPError
except ImportError:
import urllib.request as urllib2
from urllib.error import HTTPError
try:
import typing
except ImportError:
pass

# List of people to ping when the status of a tool or a book changed.
# These should be collaborators of the rust-lang/rust repository (with at least
Expand Down Expand Up @@ -63,20 +69,23 @@
}

def load_json_from_response(resp):
# type: (typing.Any) -> typing.Any
content = resp.read()
if isinstance(content, bytes):
content = content.decode('utf-8')
content_str = content.decode('utf-8')
else:
print("Refusing to decode " + str(type(content)) + " to str")
return json.loads(content)
return json.loads(content_str)

def validate_maintainers(repo, github_token):
# type: (str, str) -> None
'''Ensure all maintainers are assignable on a GitHub repo'''
next_link_re = re.compile(r'<([^>]+)>; rel="next"')

# Load the list of assignable people in the GitHub repo
assignable = []
url = 'https://api.github.com/repos/%s/collaborators?per_page=100' % repo
assignable = [] # type: typing.List[str]
url = 'https://api.github.com/repos/' \
+ '%s/collaborators?per_page=100' % repo # type: typing.Optional[str]
while url is not None:
response = urllib2.urlopen(urllib2.Request(url, headers={
'Authorization': 'token ' + github_token,
Expand Down Expand Up @@ -116,9 +125,10 @@ def validate_maintainers(repo, github_token):


def read_current_status(current_commit, path):
# type: (str, str) -> typing.Mapping[str, typing.Any]
'''Reads build status of `current_commit` from content of `history/*.tsv`
'''
with open(path, 'rU') as f:
with open(path, 'r') as f:
for line in f:
(commit, status) = line.split('\t', 1)
if commit == current_commit:
Expand All @@ -127,10 +137,12 @@ def read_current_status(current_commit, path):


def gh_url():
# type: () -> str
return os.environ['TOOLSTATE_ISSUES_API_URL']


def maybe_delink(message):
# type: (str) -> str
if os.environ.get('TOOLSTATE_SKIP_MENTIONS') is not None:
return message.replace("@", "")
return message
Expand All @@ -143,8 +155,10 @@ def issue(
relevant_pr_number,
relevant_pr_user,
labels,
github_token,
):
# Open an issue about the toolstate failure.
# type: (str, str, typing.Iterable[str], str, str, typing.List[str], str) -> None
'''Open an issue about the toolstate failure.'''
if status == 'test-fail':
status_description = 'has failing tests'
else:
Expand All @@ -168,7 +182,7 @@ def issue(
print("Creating issue:\n{}".format(request))
response = urllib2.urlopen(urllib2.Request(
gh_url(),
request,
request.encode(),
{
'Authorization': 'token ' + github_token,
'Content-Type': 'application/json',
Expand All @@ -183,8 +197,10 @@ def update_latest(
relevant_pr_url,
relevant_pr_user,
pr_reviewer,
current_datetime
current_datetime,
github_token,
):
# type: (str, str, str, str, str, str, str) -> str
'''Updates `_data/latest.json` to match build result of the given commit.
'''
with open('_data/latest.json', 'r+') as f:
Expand Down Expand Up @@ -243,13 +259,14 @@ def update_latest(
if create_issue_for_status is not None:
try:
issue(
tool, create_issue_for_status, MAINTAINERS.get(tool, ''),
relevant_pr_number, relevant_pr_user, LABELS.get(tool, ''),
tool, create_issue_for_status, MAINTAINERS.get(tool, ()),
relevant_pr_number, relevant_pr_user, LABELS.get(tool, []),
github_token,
)
except urllib2.HTTPError as e:
except HTTPError as e:
# network errors will simply end up not creating an issue, but that's better
# than failing the entire build job
print("HTTPError when creating issue for status regression: {0}\n{1}"
print("HTTPError when creating issue for status regression: {0}\n{1!r}"
.format(e, e.read()))
except IOError as e:
print("I/O error when creating issue for status regression: {0}".format(e))
Expand Down Expand Up @@ -318,7 +335,8 @@ def update_latest(
relevant_pr_url,
relevant_pr_user,
pr_reviewer,
cur_datetime
cur_datetime,
github_token,
)
if not message:
print('<Nothing changed>')
Expand All @@ -337,13 +355,13 @@ def update_latest(
issue_url = gh_url() + '/{}/comments'.format(number)
response = urllib2.urlopen(urllib2.Request(
issue_url,
json.dumps({'body': maybe_delink(message)}),
json.dumps({'body': maybe_delink(message)}).encode(),
{
'Authorization': 'token ' + github_token,
'Content-Type': 'application/json',
}
))
response.read()
except urllib2.HTTPError as e:
print("HTTPError: %s\n%s" % (e, e.read()))
except HTTPError as e:
print("HTTPError: %s\n%r" % (e, e.read()))
raise