Skip to content

Commit

Permalink
Sync with latest bot parsing improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyparrish committed Feb 21, 2024
1 parent ad826e9 commit 8478d3d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
49 changes: 37 additions & 12 deletions .github/workflows/commands/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 4 additions & 2 deletions .github/workflows/commands/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8478d3d

Please # to comment.