Skip to content

Commit 671fdb0

Browse files
committed
Fix JSON escaping in Mattermost workflow message creation
Replace unsafe sed-based JSON escaping with jq for proper JSON generation. This prevents syntax errors when commit messages contain special characters like quotes, backslashes, or control characters. Changes: - Use bash parameter expansion to strip outer quotes from toJSON() output - Use jq --arg for proper JSON string escaping - Generate complete JSON structure with jq instead of string concatenation - Maintains 350 character truncation limit Fixes JSON parsing errors when posting commit messages to Mattermost.
1 parent 0d1862b commit 671fdb0

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

.github/workflows/builds-mattermost.yaml

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,25 @@ jobs:
3737
if [ "${{ env.WORKFLOW_CONCLUSION }}" == "skipped" ]; then
3838
ICON=":person_doing_cartwheel:"
3939
fi
40-
BODYLINE=$(echo ${BODY} | sed 's/^\"//g' | sed 's/\"$//g' | sed 's/\"/\\"/g' )
41-
echo "
42-
{\"text\":
43-
\"## ${ICON} ${REF}\n
44-
Status: ${STATUS_TEXT} \n
45-
in: [${REPOSITORY}](${REPOSITORY_URL}) by: **${AUTHOR}**\n\n
46-
\`\`\`text\n
47-
${BODYLINE}\n
48-
\`\`\`
49-
\n----\n
50-
[Commit](${LINK})
51-
\"
52-
}
53-
" | paste -d' ' -s - > mattermost.json
40+
41+
# Properly handle JSON-escaped body - strip outer quotes and truncate
42+
BODYLINE='${{ toJSON(github.event.head_commit.message) }}'
43+
BODYLINE="${BODYLINE#\"}"
44+
BODYLINE="${BODYLINE%\"}"
45+
TRIMMED_BODY="${BODYLINE:0:350}"
46+
47+
# Create JSON using jq for proper escaping
48+
jq -n \
49+
--arg icon "$ICON" \
50+
--arg ref "$REF" \
51+
--arg status "$STATUS_TEXT" \
52+
--arg repo "$REPOSITORY" \
53+
--arg repo_url "$REPOSITORY_URL" \
54+
--arg author "$AUTHOR" \
55+
--arg body "$TRIMMED_BODY" \
56+
--arg link "$LINK" \
57+
'{text: ("## \($icon) \($ref)\nStatus: \($status)\nin: [\($repo)](\($repo_url)) by: **\($author)**\n\n```text\n\($body)\n```\n----\n[Commit](\($link))")}' \
58+
> mattermost.json
5459
- name: Read Content
5560
id: getcontent
5661
run: echo "::set-output name=payload::$(cat mattermost.json)"

0 commit comments

Comments
 (0)