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.
 - Normalize commands to lowercase before checking them.
 - 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 e60d72f
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions .github/workflows/shaka-bot-commands/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,30 @@ 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}" )
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
Expand Down

0 comments on commit e60d72f

Please sign in to comment.