Skip to content

Commit 7f45435

Browse files
authored
Merge pull request #257 from trz42/only_respond_to_bot_commands
only respond to bot commands
2 parents 41c1ab1 + e7e69b6 commit 7f45435

2 files changed

Lines changed: 34 additions & 13 deletions

File tree

eessi_bot_event_handler.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
from tasks.deploy import deploy_built_artefacts
3232
from tools import config
3333
from tools.args import event_handler_parse
34-
from tools.commands import EESSIBotCommand, EESSIBotCommandError, get_bot_command
34+
from tools.commands import EESSIBotCommand, EESSIBotCommandError, \
35+
contains_any_bot_command, get_bot_command
3536
from tools.permissions import check_command_permission
3637
from tools.pr_comments import create_comment
3738

@@ -113,7 +114,25 @@ def handle_issue_comment_event(self, event_info, log_file=None):
113114
# currently, only commands in new comments are supported
114115
# - commands have the syntax 'bot: COMMAND [ARGS*]'
115116

116-
# first check if sender is authorized to send any command
117+
# only scan for commands in newly created comments
118+
if action == 'created':
119+
comment_received = request_body['comment']['body']
120+
self.log(f"comment action '{action}' is handled")
121+
else:
122+
# NOTE we do not respond to an updated PR comment with yet another
123+
# new PR comment, because it would make the bot very noisy or
124+
# worse could result in letting the bot enter an endless loop
125+
self.log(f"comment action '{action}' not handled")
126+
return
127+
# at this point we know that we are handling a new comment
128+
129+
# check if comment does not contain a bot command
130+
if not contains_any_bot_command(comment_received):
131+
self.log("comment does not contain a bot comment; not processing it further")
132+
return
133+
# at this point we know that the comment contains a bot command
134+
135+
# check if sender is authorized to send any command
117136
# - this serves a double purpose:
118137
# 1. check permission
119138
# 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):
150169
else:
151170
self.log(f"account `{sender}` has permission to send commands to bot")
152171

153-
# only scan for commands in newly created comments
154-
if action == 'created':
155-
comment_received = request_body['comment']['body']
156-
self.log(f"comment action '{action}' is handled")
157-
else:
158-
# NOTE we do not respond to an updated PR comment with yet another
159-
# new PR comment, because it would make the bot very noisy or
160-
# worse could result in letting the bot enter an endless loop
161-
self.log(f"comment action '{action}' not handled")
162-
return
163-
164172
# search for commands in comment
165173
comment_response = ''
166174
commands = []

tools/commands.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@
2020
from tools.filter import EESSIBotActionFilter, EESSIBotActionFilterError
2121

2222

23+
def contains_any_bot_command(body):
24+
"""
25+
Checks if argument contains any bot command.
26+
27+
Args:
28+
body (string): possibly multi-line string that may contain a bot command
29+
30+
Returns:
31+
(bool): True if bot command found, False otherwise
32+
"""
33+
return any(map(get_bot_command, body.split('\n')))
34+
35+
2336
def get_bot_command(line):
2437
"""
2538
Retrieve bot command from a line.

0 commit comments

Comments
 (0)