Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

Gracefully handle r? of invalid user #300

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
21 changes: 15 additions & 6 deletions highfive/newpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
review_with_reviewer = 'r? @%s\n\n(rust-highfive has picked a reviewer for you, use r? to override)'
review_without_reviewer = '@%s: no appropriate reviewer found, use r? to override'

reviewer_re = re.compile("\\b[rR]\?[:\- ]*@([a-zA-Z0-9\-]+)")
submodule_re = re.compile(".*\+Subproject\scommit\s.*", re.DOTALL | re.MULTILINE)
reviewer_re = re.compile(r"\\b[rR]\?[:\- ]*@([a-zA-Z0-9\-]+)")
submodule_re = re.compile(r".*\+Subproject\scommit\s.*", re.DOTALL | re.MULTILINE)

rustaceans_api_url = "http://www.ncameron.org/rustaceans/user?username={username}"

Expand Down Expand Up @@ -105,16 +105,25 @@ def api_req(self, method, url, data=None, media_type=None):

def set_assignee(self, assignee, owner, repo, issue, user, author, to_mention):
try:
assignees = [] if assignee == 'ghost' else [assignee]

self.api_req(
"PATCH", issue_url % (owner, repo, issue),
{"assignee": assignee}
{"assignees": assignees}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR also updates highfive to no longer use assignee: it's deprecated, and I don't we would be able to clear assignees easily otherwise. I'm not sure what the old behavior was when there were multiple assignees though. When looking at the paylod, I just look at the first assignee.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually now I'm just checking if the assignee is a member of assignees.

)['body']
except urllib.error.HTTPError as e:
if e.code == 201:
pass
else:
print(f"failed to assign {assignee} to {owner}/{repo}#{issue}")
raise e
print(f"error was: {e}")
if assignee == 'ghost':
raise Exception('assigned ghost')
print("posting error comment")
error_msg = f":stop_sign: @{assignee} could not be assigned. " \
"(They may not be a member of **`@rust-lang`**!) " \
"Please assign someone else with `r? @person`."
self.post_comment(error_msg, owner, repo, issue)

self.run_commands(to_mention, owner, repo, issue, user)

Expand Down Expand Up @@ -418,8 +427,8 @@ def new_comment(self):
# Check the commenter is the submitter of the PR or the previous assignee.
author = self.payload['issue', 'user', 'login']
if not (author == commenter or (
self.payload['issue', 'assignee'] \
and commenter == self.payload['issue', 'assignee', 'login']
self.payload['issue', 'assignees'] \
and commenter in map(lambda user: user['login'], self.payload['issue', 'assignees'])
)):
# Check if commenter is a collaborator.
if not self.is_collaborator(commenter, owner, repo):
Expand Down
12 changes: 6 additions & 6 deletions highfive/tests/test_integration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_new_pr_non_contributor(self):
(
(
'PATCH', newpr.issue_url % ('rust-lang', 'rust', '7'),
{'assignee': 'nrc'}
{'assignees': ['nrc']}
),
{'body': {}},
),
Expand Down Expand Up @@ -130,7 +130,7 @@ def test_new_pr_empty_body(self):
(
(
'PATCH', newpr.issue_url % ('rust-lang', 'rust', '7'),
{'assignee': 'nrc'}
{'assignees': ['nrc']}
),
{'body': {}},
),
Expand Down Expand Up @@ -171,7 +171,7 @@ def test_new_pr_contributor(self):
(
(
'PATCH', newpr.issue_url % ('rust-lang', 'rust', '7'),
{'assignee': 'nrc'}
{'assignees': ['nrc']}
),
{'body': {}},
),
Expand Down Expand Up @@ -215,7 +215,7 @@ def test_new_pr_contributor_with_labels(self):
(
(
'PATCH', newpr.issue_url % ('rust-lang', 'rust', '7'),
{'assignee': 'nrc'}
{'assignees': ['nrc']}
),
{'body': {}},
),
Expand Down Expand Up @@ -266,7 +266,7 @@ def test_author_is_commenter(self):
(
(
'PATCH', newpr.issue_url % ('rust-lang', 'rust', '1'),
{'assignee': 'davidalber'}
{'assignees': ['davidalber']}
),
{'body': {}},
),
Expand All @@ -293,7 +293,7 @@ def test_author_not_commenter_is_collaborator(self):
(
(
'PATCH', newpr.issue_url % ('rust-lang', 'rust', '1'),
{'assignee': 'davidalber'}
{'assignees': ['davidalber']}
),
{'body': {}},
),
Expand Down
6 changes: 3 additions & 3 deletions highfive/tests/test_newpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def assert_api_req_call(self, assignee=''):
'PATCH',
'https://api.github.com/repos/%s/%s/issues/%s' % (
self.owner, self.repo, self.issue
), {"assignee": assignee}
), {"assignees": [assignee]}
)

def test_api_req_good(self):
Expand Down Expand Up @@ -908,7 +908,7 @@ def make_handler(
'issue': {
'state': state,
'number': issue_number,
'assignee': None,
'assignees': [],
'user': {
'login': author,
},
Expand All @@ -930,7 +930,7 @@ def make_handler(
if is_pull_request:
payload._payload['issue']['pull_request'] = {}
if assignee is not None:
payload._payload['issue']['assignee'] = {'login': assignee}
payload._payload['issue']['assignees'] = [{'login': assignee}]

return HighfiveHandlerMock(payload).handler

Expand Down