From 0194c3af720ea5a080243951d838f5bc1d72494a Mon Sep 17 00:00:00 2001 From: Bryan Burgers Date: Wed, 12 Jun 2019 12:10:09 -0500 Subject: [PATCH] Stop parsing when we reach an unknown word When parsing a @bors command, stop parsing when we reach any unknown word. This enables commands like @bors retry (yielding priority to the rollup) to include a description of why we retried (which gets put in the retry log) without also setting `rollup`. The only tricky bit to this is a command like @bors r=person 0123456789abcdef where the commit sha is part of the `r=` command, and we need to parse it that way. --- homu/parse_issue_comment.py | 18 ++++++++++++------ homu/tests/test_parse_issue_comment.py | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/homu/parse_issue_comment.py b/homu/parse_issue_comment.py index 7fdd3c5..d53c2ea 100644 --- a/homu/parse_issue_comment.py +++ b/homu/parse_issue_comment.py @@ -158,13 +158,21 @@ def parse_issue_comment(username, body, sha, botname, hooks=[]): if words[1:] == ["are", "you", "still", "there?"]: commands.append(IssueCommentCommand.ping('portal')) - for i, word in reversed(list(enumerate(words))): - found = True + for i, word in enumerate(words): + if word is None: + # We already parsed the next word, and we set it to an empty string + # to signify that we did. + continue + + if word == '@' + botname: + continue + if word == 'r+' or word.startswith('r='): approved_sha = sha if i + 1 < len(words) and is_sha(words[i + 1]): approved_sha = words[i + 1] + words[i + 1] = None approver = word[len('r='):] if word.startswith('r=') else username @@ -238,9 +246,7 @@ def parse_issue_comment(username, body, sha, botname, hooks=[]): commands.append(IssueCommentCommand.hook(hook_name, hook_extra)) else: - found = False - - if found: - words[i] = '' + # First time we reach an unknown word, stop parsing. + break return commands diff --git a/homu/tests/test_parse_issue_comment.py b/homu/tests/test_parse_issue_comment.py index 3db4a77..5525b26 100644 --- a/homu/tests/test_parse_issue_comment.py +++ b/homu/tests/test_parse_issue_comment.py @@ -465,6 +465,31 @@ def test_multiple_hooks(): assert thirdhook_commands[0].hook_extra is None +def test_parse_up_to_first_unknown_word(): + """ + Test that when parsing, once we arrive at an unknown word, we stop parsing + """ + + author = "jack" + body = """ + @bors retry -- yielding priority to the rollup + """ + commands = parse_issue_comment(author, body, commit, "bors") + + assert len(commands) == 1 + command = commands[0] + assert command.action == 'retry' + + body = """ + @bors retry (yielding priority to the rollup) + """ + commands = parse_issue_comment(author, body, commit, "bors") + + assert len(commands) == 1 + command = commands[0] + assert command.action == 'retry' + + def test_ignore_commands_before_bors_line(): """ Test that when command-like statements appear before the @bors part,