Skip to content

Commit

Permalink
ci: Tweak command parsing
Browse files Browse the repository at this point in the history
 - 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.
  • Loading branch information
joeyparrish committed Feb 21, 2024
1 parent d228ca4 commit 0705a8d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
38 changes: 27 additions & 11 deletions .github/workflows/shaka-bot-commands/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/shaka-bot-commands/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions .github/workflows/talk-to-shaka-bot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0705a8d

Please sign in to comment.