Skip to content

Commit

Permalink
Issue #3467 - Part 4. Add a comment_closed_reason method to the WebHo…
Browse files Browse the repository at this point in the history
…okIssue model
  • Loading branch information
Mike Taylor committed Sep 1, 2020
1 parent 34956f5 commit 5ca9dbc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
19 changes: 19 additions & 0 deletions tests/unit/test_webhook_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@ def test_comment_public_uri(mock_mr):
assert str(issue.number) in uri


@patch('webcompat.webhooks.model.make_request')
def test_comment_closed_reason(mock_mr):
"""Test comment API request that is sent to GitHub."""
mock_mr.return_value.status_code == 200
json_event, signature = event_data('private_issue_opened.json')
payload = json.loads(json_event)
issue = WebHookIssue.from_dict(payload)
reasons = ['invalid', 'incomplete']
for reason in reasons:
issue.comment_closed_reason(reason)
method, uri, data = mock_mr.call_args[0]
# make sure we sent a post with the right data to GitHub
assert method == 'post'
assert reason in data['body'].lower()
assert str(issue.get_public_issue_number()) in uri
with pytest.raises(ValueError):
issue.comment_closed_reason('boring garbage')


@patch('webcompat.webhooks.model.make_request')
def test_moderate_public_issue(mock_mr):
"""Test issue state and API request that is sent to GitHub."""
Expand Down
22 changes: 20 additions & 2 deletions webcompat/webhooks/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from webcompat.webhooks.helpers import oops
from webcompat.webhooks.helpers import prepare_rejected_issue
from webcompat.webhooks.helpers import repo_scope
from webcompat.issues import moderation_template

PUBLIC_REPO = app.config['ISSUES_REPO_URI']
PRIVATE_REPO = app.config['PRIVATE_REPO_URI']
Expand Down Expand Up @@ -82,6 +83,19 @@ def close_private_issue(self):
else:
self.state = 'closed'

def comment_closed_reason(self, reason):
"""Publish a comment on the public issue about why it was closed."""
if reason == 'invalid':
comment = moderation_template(reason).get('body')
elif reason == 'incomplete':
comment = moderation_template(reason).get('body')
else:
raise ValueError("Invalid reason")
payload = {'body': comment}
issue_number = self.get_public_issue_number()
path = f'repos/{PUBLIC_REPO}/{issue_number}/comments'
make_request('post', path, payload)

def comment_public_uri(self):
"""Publish a comment on the private issue with the public uri."""
comment = self.prepare_public_comment()
Expand Down Expand Up @@ -264,7 +278,9 @@ def process_issue_action(self):
self.number)
return oops()
else:
# we didn't get exceptions, so it's safe to close it
# we didn't get exceptions, so it's safe to comment why
# it was closed as incomplete, and close it.
self.comment_closed_reason(reason='incomplete')
self.close_private_issue()
return make_response('Moderated issue closed as incomplete',
200)
Expand All @@ -281,7 +297,9 @@ def process_issue_action(self):
self.number)
return oops()
else:
# we didn't get exceptions, so it's safe to close it
# we didn't get exceptions, so it's safe to comment why
# it was closed as invalid, and close it.
self.comment_closed_reason(reason='invalid')
self.close_private_issue()
return make_response('Moderated issue closed as invalid', 200)
elif (scope == 'private' and self.action == 'closed' and
Expand Down

0 comments on commit 5ca9dbc

Please sign in to comment.