Skip to content

Commit

Permalink
Merge pull request EESSI#257 from trz42/only_respond_to_bot_commands
Browse files Browse the repository at this point in the history
only respond to bot commands
  • Loading branch information
trz42 authored Feb 27, 2024
2 parents 41c1ab1 + e7e69b6 commit 7f45435
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
34 changes: 21 additions & 13 deletions eessi_bot_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand Down
13 changes: 13 additions & 0 deletions tools/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 7f45435

Please sign in to comment.