From ded7991dd6d68e5a5bae58fa10b4747246e8dc0b Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 25 May 2021 09:55:45 +0200 Subject: [PATCH] CI: different tactic for notifications --- .github/workflows/push.yml | 8 ++++++ ci/notify.sh | 55 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100755 ci/notify.sh diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 2a54f1600..15418dcfc 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -85,3 +85,11 @@ jobs: if: ${{ failure() && github.repository == 'calamares/calamares' }} run: | curl -s -XPOST -d '{"msgtype":"m.text", "body": "FAIL ${{ github.workflow }} in ${{ github.repository }} ${{ github.actor }} on ${{ github.event.ref }}\n.. ${{ steps.pre_build.outputs.message }}\n.. DIFF ${{ github.event.compare }}" }' 'https://matrix.org/_matrix/client/r0/rooms/%21${{ secrets.MATRIX_ROOM }}/send/m.room.message?access_token=${{ secrets.MATRIX_TOKEN }}' > /dev/null + - name: "notify: check" + env: + MATRIX_TOKEN: ${{ secrets.MATRIX_TOKEN }} + MATRIX_ROOM: ${{ secrets.MATRIX_ROOM }} + run: | + "$SRCDIR/ci/notify.sh" OK '${{ github.workflow }}' '${{ github.repository }}' '${{ github.actor }}' '${{ github.event.ref }}' \ + '(test)${{ steps.pre_build.outputs.message }}' \ + '${{ github.event.compare }}' diff --git a/ci/notify.sh b/ci/notify.sh new file mode 100755 index 000000000..a51d48cd3 --- /dev/null +++ b/ci/notify.sh @@ -0,0 +1,55 @@ +#! /bin/sh +# +# SPDX-FileCopyrightText: 2021 Adriaan de Groot +# SPDX-License-Identifier: BSD-2-Clause +# +### +# +# Sends a notification to wherever the notifications need to be sent. +# +# Called with the following environment (for tokens / secrets): +# MATRIX_ROOM +# MATRIX_TOKEN +# +# Called with the following arguments (in order): +# - "OK" or "FAIL" +# - github.workflow +# - github.repository +# - github.actor +# - github.event.ref +# - commit-message-summary (produced in the workflow) +# - github.event.compare +# + +test -z "$MATRIX_ROOM" && { echo "! No secrets" ; exit 1 ; } +test -z "$MATRIX_TOKEN" && { echo "! No secrets" ; exit 1 ; } + +STATUS="$1" + +WORKFLOW="$2" +REPOSITORY="$3" +ACTOR="$4" +EVENT="$5" +SUMMARY="$6" +COMPARE="$7" + +test "x$STATUS" = "xOK" -o "x$STATUS" = "xFAIL" || { echo "! Invalid status" ; exit 1 ; } + +test -z "$WORKFLOW" && { echo "! No event data" ; exit 1 ; } +test -z "$REPOSITORY" && { echo "! No event data" ; exit 1 ; } +test -z "$ACTOR" && { echo "! No event data" ; exit 1 ; } +test -z "$EVENT" && { echo "! No event data" ; exit 1 ; } +# It's ok for summary or the compare URL to be empty + +url="https://matrix.org/_matrix/client/r0/rooms/%21${MATRIX_ROOM}/send/m.room.message?access_token=${MATRIX_TOKEN}" + +status_line="${STATUS} ${WORKFLOW} in ${REPOSITORY} ${ACTOR} on ${EVENT}" +summary_line="" +compare_line="" + +test -n "$SUMMARY" && summary_line="\n.. ${SUMMARY}" +test -n "$COMPARE" && compare_line="\n.. DIFF ${COMPARE}" + +message_data=$(jq -Rs --arg body "${status_line}${summary_line}${compare_line}" '{"msgtype": "m.text", $body}' < /dev/null) + +curl -s -XPOST -d "$message_data" "$url" > /dev/null