Skip to content

Commit ca9ce89

Browse files
committed
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.
1 parent d228ca4 commit ca9ce89

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

.github/workflows/shaka-bot-commands/lib.sh

+33-11
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,41 @@ function start_workflow() {
6464
gh workflow run "$WORKFLOW" -R "$THIS_REPO" "${GH_ARGS[@]}"
6565
}
6666

67+
# Simple alias for converting a stream of text to lowercase.
68+
function tolower() {
69+
tr '[:upper:]' '[:lower:]'
70+
}
71+
6772
# Outputs to global variables SHAKA_BOT_COMMAND and SHAKA_BOT_ARGUMENTS (array).
6873
function parse_command() {
69-
# Tokenize the comment by whitespace.
70-
local TOKENS=( $COMMENT_BODY )
71-
72-
local INDEX
73-
for (( INDEX=0; INDEX < ${#TOKENS[@]}; INDEX++ )); do
74-
if [[ "${TOKENS[i]}" == "@shaka-bot" ]]; then
75-
SHAKA_BOT_COMMAND="${TOKENS[i+1]}"
76-
# A slice of all tokens starting with index i+2.
77-
SHAKA_BOT_ARGUMENTS=( "${TOKENS[@]:i+2}" )
78-
return 0
79-
fi
74+
echo "Parsing comment. Body: \"$COMMENT_BODY\""
75+
76+
# Read each line one at a time. Tokens from one line won't affect another.
77+
local COMMENT_LINE
78+
echo "$COMMENT_BODY" | while read COMMENT_LINE; do
79+
echo "Parsing comment. Line: \"$COMMENT_LINE\""
80+
81+
# Tokenize the comment by whitespace.
82+
local TOKENS=( $COMMENT_LINE )
83+
echo "Parsing comment. Tokens: \"$TOKENS\""
84+
85+
local INDEX
86+
for (( INDEX=0; INDEX < ${#TOKENS[@]}; INDEX++ )); do
87+
if [[ "${TOKENS[INDEX]}" == "@shaka-bot" ]]; then
88+
# The next word is the command.
89+
SHAKA_BOT_COMMAND=$(echo "${TOKENS[INDEX+1]}" | tolower)
90+
91+
# Unless it's please, then it's the word after that.
92+
if [[ "$SHAKA_BOT_COMMAND" == "please" ]]; then
93+
INDEX=$((INDEX + 1))
94+
SHAKA_BOT_COMMAND=$(echo "${TOKENS[INDEX+1]}" | tolower)
95+
fi
96+
97+
# A slice of all tokens starting with index INDEX+2.
98+
SHAKA_BOT_ARGUMENTS=( "${TOKENS[@]:INDEX+2}" )
99+
return 0
100+
fi
101+
done
80102
done
81103

82104
return 1

.github/workflows/shaka-bot-commands/main.sh

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ if [[ "$SHAKA_BOT_COMMAND" == "" ]]; then
4242
exit 0
4343
fi
4444

45-
echo "PR $PR_NUMBER, detected command $SHAKA_BOT_COMMAND"
45+
echo "PR $PR_NUMBER"
46+
echo "Detected command \"$SHAKA_BOT_COMMAND\""
47+
echo "Detected arguments \"$SHAKA_BOT_ARGUMENTS\""
4648

4749
case "$SHAKA_BOT_COMMAND" in
4850
help) . command-help.sh ;;
4951
test) . command-test.sh ;;
50-
*) echo "Unknown command!" ;;
52+
*) echo "Unknown command" ;;
5153
esac

.github/workflows/talk-to-shaka-bot.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88

99
jobs:
1010
handle_command:
11+
name: Handle Command
1112
# Only runs on PRs that contain '@shaka-bot' comments, but not comments
1213
# made by shaka-bot itself, who will sometimes use its own name.
1314
# Note that contains() is not case sensitive.

0 commit comments

Comments
 (0)