diff --git a/.github/actions/matrix-notify/action.yml b/.github/actions/matrix-notify/action.yml index 36ce6bf..f212759 100644 --- a/.github/actions/matrix-notify/action.yml +++ b/.github/actions/matrix-notify/action.yml @@ -6,7 +6,7 @@ inputs: description: Message text, URLs are shown as links by most clients required: true homeserver: - description: Base URL of the homeserver (e.g. 'https://matrix.org'), empty to skip + description: https base URL of the homeserver (e.g. 'https://matrix.org'), empty to skip required: false default: '' room-id: @@ -32,12 +32,22 @@ runs: echo "Matrix is not configured, no message sent" exit 0 fi + if [[ ! "$HOMESERVER" =~ ^https://[^[:space:][:cntrl:]]+$ ]]; then + echo "::error::homeserver must be the https base URL of the homeserver, e.g. https://matrix.org, not '$(tr -d '[:cntrl:]' <<< "$HOMESERVER")'" + exit 1 + fi ROOM="$(jq -rn --arg room "$ROOM_ID" '$room | @uri')" TXN="${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}-$(date +%s%N)" - jq -n --arg body "$MESSAGE" '{msgtype: "m.notice", body: $body}' \ - | curl -sS --fail-with-body --connect-timeout 10 --max-time 30 -X PUT \ + RESPONSE="$(mktemp)" + STATUS="$(jq -n --arg body "$MESSAGE" '{msgtype: "m.notice", body: $body}' \ + | curl -sS --connect-timeout 10 --max-time 30 -o "$RESPONSE" -w '%{http_code}' -X PUT \ -H "Authorization: Bearer ${TOKEN}" \ -H 'Content-Type: application/json' \ --data @- \ - "${HOMESERVER%/}/_matrix/client/v3/rooms/${ROOM}/send/m.room.message/${TXN}" - echo + "${HOMESERVER%/}/_matrix/client/v3/rooms/${ROOM}/send/m.room.message/${TXN}")" \ + || { echo "::error::the message could not be sent to ${HOMESERVER%/}"; exit 1; } + if [[ ! "$STATUS" =~ ^2[0-9][0-9]$ ]]; then + echo "::error::${HOMESERVER%/} answered with HTTP ${STATUS}: $(head -c 500 "$RESPONSE" | tr -d '[:cntrl:]')" + exit 1 + fi + echo "message sent to ${HOMESERVER%/}" diff --git a/README.md b/README.md index eed452e..7f7c7c8 100644 --- a/README.md +++ b/README.md @@ -152,11 +152,16 @@ The room is set by organization variables, which the workflows read from the cal | Variable | Example | Content | |---------------------|----------------------|-------------------------------------------| -| `MATRIX_HOMESERVER` | `https://matrix.org` | Base URL of the homeserver | +| `MATRIX_HOMESERVER` | `https://matrix.org` | https base URL of the homeserver | | `MATRIX_ROOM_ID` | `!AbCdEf:matrix.org` | Internal id of the room, see its settings | +The base URL is not the server name in the room id: it is `m.homeserver.base_url` of +`https:///.well-known/matrix/client`, e.g. `https://chat.example.org` for `example.org`. + A repository variable with the same name sends the messages of that repository to another room. -Without the variables or without `MATRIX_ACCESS_TOKEN`, no message is sent and nothing fails. +Without the variables or without `MATRIX_ACCESS_TOKEN`, no message is sent and nothing fails. A homeserver that is not +an https URL, or any answer of the homeserver but 2xx, fails the step with the answer in its error. The release and +version branch workflows go on with `continue-on-error`, their result does not change. The messages come from a separate Matrix account, which has to be a member of the room. Its access token is shown in Element under Settings, Help & About. The room should not be end-to-end encrypted, as the messages are sent unencrypted.