From 8478d3db29196c16530c8c189fbcfe12f59fabda Mon Sep 17 00:00:00 2001 From: Joey Parrish Date: Wed, 21 Feb 2024 11:40:46 -0800 Subject: [PATCH] Sync with latest bot parsing improvements --- .github/workflows/commands/lib.sh | 49 ++++++++++++++++++++++-------- .github/workflows/commands/main.sh | 6 ++-- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/.github/workflows/commands/lib.sh b/.github/workflows/commands/lib.sh index f48305d..bafc24c 100644 --- a/.github/workflows/commands/lib.sh +++ b/.github/workflows/commands/lib.sh @@ -65,20 +65,45 @@ 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 BOT_COMMAND and 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[INDEX]}" == "/bot" ]]; then - BOT_COMMAND="${TOKENS[INDEX+1]}" - # A slice of all tokens starting with INDEX+2. - BOT_ARGUMENTS=( "${TOKENS[@]:INDEX+2}" ) - return 0 - fi - done + echo "Parsing comment. Body: \"$COMMENT_BODY\"" + + # Read each line one at a time. Tokens from one line won't affect another. + local COMMENT_LINE + while read COMMENT_LINE; do + echo "Parsing comment. Line: \"$COMMENT_LINE\"" + + # Tokenize the line 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. + BOT_COMMAND=$(echo "${TOKENS[INDEX+1]}" | tolower) + + # Unless it's please, then it's the word after that. + if [[ "$BOT_COMMAND" == "please" ]]; then + INDEX=$((INDEX + 1)) + BOT_COMMAND=$(echo "${TOKENS[INDEX+1]}" | tolower) + fi + + # A slice of all tokens starting with index INDEX+2. + BOT_ARGUMENTS=( "${TOKENS[@]:INDEX+2}" ) + return 0 + fi + done + done < <(echo "$COMMENT_BODY" | tr -d '\r') + # The line above pipes COMMENT_BODY, without \r characters, into the loop. + # It is important to maintain the loop in the main shell, not a subshell, so + # that variables it writes to (BOT_COMMAND and BOT_ARGUMENTS) affect the + # caller. return 1 } diff --git a/.github/workflows/commands/main.sh b/.github/workflows/commands/main.sh index 35362d5..145002c 100755 --- a/.github/workflows/commands/main.sh +++ b/.github/workflows/commands/main.sh @@ -43,11 +43,13 @@ if [[ "$BOT_COMMAND" == "" ]]; then exit 0 fi -echo "Issue $ISSUE_NUMBER, detected command $BOT_COMMAND" +echo "Issue $ISSUE_NUMBER" +echo "Detected command \"$BOT_COMMAND\"" +echo "Detected arguments \"$BOT_ARGUMENTS\"" case "$BOT_COMMAND" in help) . command-help.sh ;; link) . command-link.sh ;; delete) . command-delete.sh ;; - *) echo "Unknown command!" ;; + *) echo "Unknown command" ;; esac