From b2dab9206e2df36e7cc80377f49b0fef9521488c Mon Sep 17 00:00:00 2001 From: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com> Date: Tue, 22 Nov 2022 13:21:58 -0500 Subject: [PATCH] Support @mscbot commands in PR review bodies (#26) --- README.md | 1 + command_handler.py | 24 ++++++++++++++++++------ webhook.py | 5 +++++ 3 files changed, 24 insertions(+), 6 deletions(-) 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: 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(