Skip to content

Fix JSON parsing errors in Mattermost notification workflows #39

Fix JSON parsing errors in Mattermost notification workflows

Fix JSON parsing errors in Mattermost notification workflows #39

name: Builds-Mattermost
on:
workflow_call:
secrets:
WEBHOOK:
required: true
push:
branches:
- main
jobs:
notify:
name: Mattermost
runs-on: ubuntu-latest
steps:
- uses: technote-space/workflow-conclusion-action@v3
- name: Create Mattermost Message
env:
REPOSITORY: ${{ github.repository }}
REPOSITORY_URL: ${{ github.repository.html_url }}
REF: ${{ github.ref_name }}
ACTOR: ${{ github.actor }}
TITLE: ${{ github.workflow }}
BODY: ${{ github.event.head_commit.message }}
ANSWER: ${{ github.event.issue.comments_url }}
LINK: ${{ github.event.head_commit.url }}
shell: bash
run: |
AUTHOR=${ACTOR}
ICON=":octagonal_sign:"
STATUS_TEXT="${{ env.WORKFLOW_CONCLUSION }}"
if [ "${{ env.WORKFLOW_CONCLUSION }}" == "success" ]; then
ICON=":robot:"
fi
if [ "${{ env.WORKFLOW_CONCLUSION }}" == "skipped" ]; then
ICON=":person_doing_cartwheel:"
fi
# Truncate body to 350 characters
TRIMMED_BODY="${BODY:0:350}"
# Create JSON using jq for proper escaping
jq -nc \
--arg icon "$ICON" \
--arg ref "$REF" \
--arg status "$STATUS_TEXT" \
--arg repo "$REPOSITORY" \
--arg repo_url "$REPOSITORY_URL" \
--arg author "$AUTHOR" \
--arg body "$TRIMMED_BODY" \
--arg link "$LINK" \
'{text: ("## \($icon) \($ref)\nStatus: \($status)\nin: [\($repo)](\($repo_url)) by: **\($author)**\n\n```text\n\($body)\n```\n----\n[Commit](\($link))")}' \
> mattermost.json
- name: Read Content
id: getcontent
run: echo "payload=$(cat mattermost.json)" >> $GITHUB_OUTPUT
- name: Send Mattermost Message
uses: mattermost/action-mattermost-notify@master
with:
MATTERMOST_WEBHOOK_URL: ${{ secrets.WEBHOOK }}
PAYLOAD: |-
${{ steps.getcontent.outputs.payload }}