Browse Source

Initial commit

master
John Locke 8 years ago
commit
6daae05edf
  1. 7
      Dockerfile
  2. 87
      README.md
  3. 5
      bin/check
  4. 3
      bin/in
  5. 81
      bin/out

7
Dockerfile

@ -0,0 +1,7 @@
FROM debian:jessie
RUN apt-get update && apt-get -y install jq curl
COPY bin/ /opt/resource/
COPY matrix_bash_bot/matrix-bashbot.sh /usr/local/bin/
RUN chmod +x /opt/resource/* /usr/local/bin/matrix-bashbot.sh

87
README.md

@ -0,0 +1,87 @@
# Matrix Notification Resource for Concourse CI
Send notification messages to [Matrix](http://matrix.org) using a string message or templated message.
This resource borrows heavily from the [Slack notification resource](https://github.com/cloudfoundry-community/slack-notification-resource). Usage and behavior around text and text_file parameters should be handled exactly the same as in that.
## Installing
```
resource-types:
- name: matrix-notification-resource
type: docker-image
source:
repository: freelock/matrix-notification-resource
```
## Registering with Matrix
This resource needs an access token for a valid user account. It will not create the user account for you, or retrieve the token.
To get a token, first create a Matrix user account on your homeserver of choice. Then you can use Curl to get an access token for the account:
```
curl -XPOST -d '{"type":"m.login.password", "user":"example", "password":"wordpass"}' "http://matrix.org/_matrix/client/api/r0/login"
{
"access_token": "QGV4YW1wbGU6bG9jYWxob3N0.vRDLTgxefmKWQEtgGd",
"home_server": "matrix.org",
"user_id": "@example:matrix.org"
}
```
... add the returned access_token to the resource.
Then, a user will need to invite the account to the appropriate room, and the account will need to accept the invitation.
## Source Configuration
* `matrix_server_url`: *Required.* Example: https://matrix.org
* `token`: *Required.* token to authenticate with Matrix server
* `room_id`: *Required.* The room to send notifications to -- this account must already be a member of this room.
Pull requests accepted for room_alias, user logins, auto-joins.
#### `out`: Sends message to Matrix room
Send message to specified Matrix Room, with the configured parameters
#### Parameters
* `text`: (string) Text to send to the Matrix room
* `text_file`: (string) File containing text to send to the Matrix room
* `msgtype`: *Optional.* Message type, m.notice, m.text (default: m.notice)
## Example
### Check
```
---
resources:
- name: matrix-notification
type: matrix-notification-resource
source:
matrix_server_url: https://matrix.org
token: {{matrix_token}}
room_id: {{matrix_room_id}}
```
### In
*Not supported*
### Out
```
---
---
- put: matrix-notification
params:
text_file: results/message.txt
text: |
The build had a result. Check it out at:
http://my.concourse.url/pipelines/$BUILD_PIPELINE_NAME/jobs/$BUILD_JOB_NAME/builds/$BUILD_NAME
or at:
http://my.concourse.url/builds/$BUILD_ID
Result: $TEXT_FILE_CONTENT
```

5
bin/check

@ -0,0 +1,5 @@
#!/bin/bash
read json
echo '[]'
exit 0

3
bin/in

@ -0,0 +1,3 @@
#!/bin/bash
echo '{"version":{"ref":"none"}}'

81
bin/out

@ -0,0 +1,81 @@
#!/bin/bash
# Send message to a matrix room. API call looks like this:
# curl -XPOST -d '{"msgtype":"m.text", "body":"hello"}' "http://matrix.org/_matrix/client/api/v1/rooms/%21CvcvRuDYDzTOzfKKgh%3Alocalhost/send/m.room.message?access_token=YOUR_ACCESS_TOKEN"
set -e
cd "${1}"
exec 3>&1
exec 1>&2
# for jq
PATH=/usr/local/bin:$PATH
payload=$(mktemp /tmp/resource-in.XXXXXX)
cat > "${payload}" <&0
matrix_server_url="$(jq -r '.source.matrix_server_url' < "${payload}")"
token="$(jq -r '.source.token' < "${payload}")"
room_id="$(jq -r '.source.room_id' < "${payload}")"
ts="$(date +%s)"
matrix_endpoint="$matrix_server_url/_matrix/client/r0/rooms/$room_id/send/m.room.message/$ts?access_token=$token"
text_file="$(jq -r '.params.text_file // ""' < "${payload}")"
from="$(jq -r '.params.from // ""' < "${payload}")"
text="$(jq '(.params.text // "${TEXT_FILE_CONTENT}")' < "${payload}")"
msgtype="$(jq '(.params.msgtype // "m.notice")' < "${payload}")"
always_notify="$(jq -r '.params.always_notify // "true"' < "${payload}")"
debug="$(jq -r '.params.debug // "false"' < "${payload}")"
silent="$(jq -r '.params.silent // "false"' < "${payload}")"
TEXT_FILE_CONTENT=""
[[ -n "${text_file}" && -f "${text_file}" ]] && TEXT_FILE_CONTENT="$(cat "${text_file}")"
if [[ "$always_notify" == "true" || -n "$TEXT_FILE_CONTENT" || -z "$text_file" ]]
then
TEXT_FILE_CONTENT="${TEXT_FILE_CONTENT:-_(no notification provided)_}"
text="$(eval printf ${text} )"
[[ -z "${text}" ]] && text="_(missing notification text)_"
text="$(echo "${text}" | jq -R -s .)"
[[ "${token}" != "null" ]] && username="$(eval "printf ${token}" | jq -R -s .)"
[[ "${room_id}" != "null" ]] && room_id="$(eval "printf ${room_id}" | jq -R -s .)"
body="$(cat <<EOF
{
"msgtype": ${msgtype},
"body": ${text}
}
EOF
)"
compact_body="$(echo "${body}" | jq -c '.')"
if [[ "$debug" == "true" ]]
then
json="$(cat <<EOF
{
"matrix_endpoint": "${matrix_endpoint}",
"body": ${body}
}
EOF
)"
echo "$json" | jq -c '.'
exit 0
elif [[ "$silent" == "true" ]]
then
echo "Using silent output"
curl -s -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{compact_body}' "${matrix_endpoint}"
else
curl -v -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{compact_body}' "${matrix_endpoint}"
fi
fi
jq -n "{version:{timestamp:\"$(date +%s)\"}}" >&3
Loading…
Cancel
Save