From 0705a8dafbfa987f6001e41901da31c2da55d50a Mon Sep 17 00:00:00 2001 From: Joey Parrish Date: Wed, 21 Feb 2024 11:03:59 -0800 Subject: [PATCH] ci: Tweak command parsing - Fix wrong index variable in loop ("i" vs "INDEX") that prevented detection of commands at arbitrary places in a comment. - Fix parsing of comments with newlines. - Normalize commands to lowercase before checking them. - Enhance debugging of command parsing. - Allow the user to say "please" because it just feels nicer to me, even when I'm talking to a robot. --- .github/workflows/shaka-bot-commands/lib.sh | 38 ++++++++++++++------ .github/workflows/shaka-bot-commands/main.sh | 6 ++-- .github/workflows/talk-to-shaka-bot.yaml | 1 + 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/.github/workflows/shaka-bot-commands/lib.sh b/.github/workflows/shaka-bot-commands/lib.sh index e8f018300e..d8fa24aa00 100644 --- a/.github/workflows/shaka-bot-commands/lib.sh +++ b/.github/workflows/shaka-bot-commands/lib.sh @@ -64,19 +64,35 @@ function start_workflow() { gh workflow run "$WORKFLOW" -R "$THIS_REPO" "${GH_ARGS[@]}" } +# Simple alias for converting a stream of text to lowercase. +function tolower() { + tr '[:upper:]' '[:lower:]' +} + # Outputs to global variables SHAKA_BOT_COMMAND and SHAKA_BOT_ARGUMENTS (array). function parse_command() { - # Tokenize the comment by whitespace. - local TOKENS=( $COMMENT_BODY ) - - local INDEX - for (( INDEX=0; INDEX < ${#TOKENS[@]}; INDEX++ )); do - if [[ "${TOKENS[i]}" == "@shaka-bot" ]]; then - SHAKA_BOT_COMMAND="${TOKENS[i+1]}" - # A slice of all tokens starting with index i+2. - SHAKA_BOT_ARGUMENTS=( "${TOKENS[@]:i+2}" ) - return 0 - fi + # Read each line one at a time. Tokens from one line won't affect another. + echo "$COMMENT_BODY" | while read COMMENT_LINE; do + # Tokenize the comment by whitespace. + local TOKENS=( $COMMENT_LINE ) + + local INDEX + for (( INDEX=0; INDEX < ${#TOKENS[@]}; INDEX++ )); do + if [[ "${TOKENS[INDEX]}" == "@shaka-bot" ]]; then + # The next word is the command. + SHAKA_BOT_COMMAND=$(echo "${TOKENS[INDEX+1]}" | tolower) + + # Unless it's please, then it's the word after that. + if [[ "$SHAKA_BOT_COMMAND" == "please" ]]; then + INDEX=$((INDEX + 1)) + SHAKA_BOT_COMMAND=$(echo "${TOKENS[INDEX+1]}" | tolower) + fi + + # A slice of all tokens starting with index INDEX+2. + SHAKA_BOT_ARGUMENTS=( "${TOKENS[@]:INDEX+2}" ) + return 0 + fi + done done return 1 diff --git a/.github/workflows/shaka-bot-commands/main.sh b/.github/workflows/shaka-bot-commands/main.sh index 52a1e19568..c9339a8c1b 100755 --- a/.github/workflows/shaka-bot-commands/main.sh +++ b/.github/workflows/shaka-bot-commands/main.sh @@ -42,10 +42,12 @@ if [[ "$SHAKA_BOT_COMMAND" == "" ]]; then exit 0 fi -echo "PR $PR_NUMBER, detected command $SHAKA_BOT_COMMAND" +echo "PR $PR_NUMBER" +echo "Detected command \"$SHAKA_BOT_COMMAND\"" +echo "Detected arguments \"$SHAKA_BOT_ARGUMENTS\"" case "$SHAKA_BOT_COMMAND" in help) . command-help.sh ;; test) . command-test.sh ;; - *) echo "Unknown command!" ;; + *) echo "Unknown command" ;; esac diff --git a/.github/workflows/talk-to-shaka-bot.yaml b/.github/workflows/talk-to-shaka-bot.yaml index b0f1f5da1f..f13fd99941 100644 --- a/.github/workflows/talk-to-shaka-bot.yaml +++ b/.github/workflows/talk-to-shaka-bot.yaml @@ -8,6 +8,7 @@ on: jobs: handle_command: + name: Handle Command # Only runs on PRs that contain '@shaka-bot' comments, but not comments # made by shaka-bot itself, who will sometimes use its own name. # Note that contains() is not case sensitive.