diff --git a/eessi_bot_event_handler.py b/eessi_bot_event_handler.py index 024615ce..be48306f 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("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.