From 2cf73ac79865df1c4e23132db8d0436d5ec673f8 Mon Sep 17 00:00:00 2001 From: Thomas Roeblitz Date: Mon, 26 Feb 2024 21:19:15 +0100 Subject: [PATCH 1/3] prevent bot from responding on comments without a bot command --- eessi_bot_event_handler.py | 34 +++++++++++++++++++++------------- tools/commands.py | 13 +++++++++++++ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/eessi_bot_event_handler.py b/eessi_bot_event_handler.py index 024615ce..60df7a3a 100644 --- a/eessi_bot_event_handler.py +++ b/eessi_bot_event_handler.py @@ -31,7 +31,8 @@ from tasks.deploy import deploy_built_artefacts from tools import config from tools.args import event_handler_parse -from tools.commands import EESSIBotCommand, EESSIBotCommandError, get_bot_command +from tools.commands import EESSIBotCommand, EESSIBotCommandError, + contains_any_bot_command, get_bot_command from tools.permissions import check_command_permission from tools.pr_comments import create_comment @@ -113,7 +114,25 @@ def handle_issue_comment_event(self, event_info, log_file=None): # currently, only commands in new comments are supported # - commands have the syntax 'bot: COMMAND [ARGS*]' - # first check if sender is authorized to send any command + # only scan for commands in newly created comments + if action == 'created': + comment_received = request_body['comment']['body'] + self.log(f"comment action '{action}' is handled") + else: + # NOTE we do not respond to an updated PR comment with yet another + # new PR comment, because it would make the bot very noisy or + # worse could result in letting the bot enter an endless loop + self.log(f"comment action '{action}' not handled") + return + # at this point we know that we are handling a new comment + + # check if comment does not contain a bot command + if not contains_any_bot_command(comment_received): + self.log(f"comment does not contain a bot comment; not processing it further") + return + # at this point we know that the comment contains a bot command + + # check if sender is authorized to send any command # - this serves a double purpose: # 1. check permission # 2. skip any comment updates that were done by the bot itself @@ -150,17 +169,6 @@ def handle_issue_comment_event(self, event_info, log_file=None): else: self.log(f"account `{sender}` has permission to send commands to bot") - # only scan for commands in newly created comments - if action == 'created': - comment_received = request_body['comment']['body'] - self.log(f"comment action '{action}' is handled") - else: - # NOTE we do not respond to an updated PR comment with yet another - # new PR comment, because it would make the bot very noisy or - # worse could result in letting the bot enter an endless loop - self.log(f"comment action '{action}' not handled") - return - # search for commands in comment comment_response = '' commands = [] diff --git a/tools/commands.py b/tools/commands.py index cdc2c8f7..5db8f7f7 100644 --- a/tools/commands.py +++ b/tools/commands.py @@ -20,6 +20,19 @@ from tools.filter import EESSIBotActionFilter, EESSIBotActionFilterError +def contains_any_bot_command(body): + """ + Checks if argument contains any bot command. + + Args: + body (string): possibly multi-line string that may contain a bot command + + Returns: + (bool): True if bot command found, False otherwise + """ + return any(map(get_bot_command, body.split('\n'))) + + def get_bot_command(line): """ Retrieve bot command from a line. From 6be82dd5fe4edcada083c3bdec0a3b1e00c10e2d Mon Sep 17 00:00:00 2001 From: Thomas Roeblitz Date: Mon, 26 Feb 2024 21:30:45 +0100 Subject: [PATCH 2/3] fix import syntax error --- eessi_bot_event_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eessi_bot_event_handler.py b/eessi_bot_event_handler.py index 60df7a3a..c0e6cd32 100644 --- a/eessi_bot_event_handler.py +++ b/eessi_bot_event_handler.py @@ -31,7 +31,7 @@ from tasks.deploy import deploy_built_artefacts from tools import config from tools.args import event_handler_parse -from tools.commands import EESSIBotCommand, EESSIBotCommandError, +from tools.commands import EESSIBotCommand, EESSIBotCommandError, \ contains_any_bot_command, get_bot_command from tools.permissions import check_command_permission from tools.pr_comments import create_comment From e7e69b6d887ec99ac862dff85f15aa025ea15561 Mon Sep 17 00:00:00 2001 From: Thomas Roeblitz Date: Tue, 27 Feb 2024 17:35:00 +0100 Subject: [PATCH 3/3] use plain string instead of f-string --- eessi_bot_event_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eessi_bot_event_handler.py b/eessi_bot_event_handler.py index c0e6cd32..be48306f 100644 --- a/eessi_bot_event_handler.py +++ b/eessi_bot_event_handler.py @@ -128,7 +128,7 @@ def handle_issue_comment_event(self, event_info, log_file=None): # check if comment does not contain a bot command if not contains_any_bot_command(comment_received): - self.log(f"comment does not contain a bot comment; not processing it further") + self.log("comment does not contain a bot comment; not processing it further") return # at this point we know that the comment contains a bot command