From bea8942aa220f9f88595ef64aeed2b2e4fb4e22b Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 19 Jul 2022 17:13:26 +0100 Subject: [PATCH 1/2] Support @mscbot commands in PR review bodies This is the text that appears as part of a PR review (not the one or more comments on the actual diff of the changes. --- command_handler.py | 24 ++++++++++++++++++------ webhook.py | 5 +++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/command_handler.py b/command_handler.py index e967224..e70be01 100644 --- a/command_handler.py +++ b/command_handler.py @@ -53,12 +53,25 @@ def __init__(self, config: Config, store: Storage, repo: Repository): # Set up FCP timer handler, and callback functions self.fcp_timers = FCPTimers(store, self._on_fcp_timer_fired) - def handle_comment(self, comment: Dict): + def handle_comment(self, comment: Dict) -> None: + # If this is a pull request review (not a comment or a comment in a review) then + # the field containing comment text etc. will be "review" instead of "comment" + if "comment" in comment: + comment_fields = comment["comment"] + elif "review" in comment: + comment_fields = comment["review"] + else: + log.debug("Unrecognised structure of comment. Ignoring.") + return + + # Extract the comment's text + comment_text = comment_fields["body"] + # Replace any instances of \r\n with just \n - comment["comment"]["body"] = comment["comment"]["body"].replace("\r\n", "\n") + comment_text = comment_text.replace("\r\n", "\n") # Check for any commands - commands = self.parse_commands_from_text(comment["comment"]["body"]) + commands = self.parse_commands_from_text(comment_text) # Retrieve the issue this comment is attached to # Account for issue and pull request review comments @@ -68,10 +81,9 @@ def handle_comment(self, comment: Dict): original_labels = self.proposal_labels_str.copy() self.comment = comment - self.comment_link = comment["comment"]["html_url"] + self.comment_link = comment_fields["html_url"] # Check if this is a new comment or an edit - comment_body = self.comment["comment"]["body"] if comment["action"] == "edited": # Check if this is an edit of a status comment known_status_comment = self._get_status_comment() @@ -83,7 +95,7 @@ def handle_comment(self, comment: Dict): return # Process status comment update - self._process_status_comment_update_with_body(comment_body) + self._process_status_comment_update_with_body(comment_text) else: # Run command functions for command in commands: diff --git a/webhook.py b/webhook.py index 9d1e6cd..0a8aced 100644 --- a/webhook.py +++ b/webhook.py @@ -58,6 +58,11 @@ def on_issue_comment(data): log.debug(f"Got comment: {json.dumps(data, indent=4, sort_keys=True)}") self._process_comment(data) + @webhook.hook("pull_request_review") + def on_pull_request_review(data): + log.debug(f"Got PR review: {json.dumps(data, indent=4, sort_keys=True)}") + self._process_comment(data) + @webhook.hook("pull_request_review_comment") def on_pull_request_review_comment(data): log.debug( From 21f4ddb54f8579f2ba7d6163c8253d7615fd7eaa Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 19 Jul 2022 17:15:31 +0100 Subject: [PATCH 2/2] Add note in README for checking 'Pull Request reviews' in webhook setup --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ab2014b..73fc551 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ On github: 1. Come up with and set a secret (hint: use the `uuid` terminal command) 1. Under events, select "Let me select individual events". Only the following need to be checked: - Issue comments + - Pull request reviews - Pull request review comments On your server: