From 6daae05edf6f5903a440dccd1d6f466ef619cd5a Mon Sep 17 00:00:00 2001 From: John Locke Date: Sat, 30 Apr 2016 11:06:25 -0700 Subject: [PATCH] Initial commit --- Dockerfile | 7 +++++ README.md | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ bin/check | 5 ++++ bin/in | 3 ++ bin/out | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 183 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100755 bin/check create mode 100755 bin/in create mode 100755 bin/out diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..12f5ad9 --- /dev/null +++ b/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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..ecf7d1b --- /dev/null +++ b/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 +``` diff --git a/bin/check b/bin/check new file mode 100755 index 0000000..10691ee --- /dev/null +++ b/bin/check @@ -0,0 +1,5 @@ +#!/bin/bash + +read json +echo '[]' +exit 0 diff --git a/bin/in b/bin/in new file mode 100755 index 0000000..8b3ffd3 --- /dev/null +++ b/bin/in @@ -0,0 +1,3 @@ +#!/bin/bash + +echo '{"version":{"ref":"none"}}' diff --git a/bin/out b/bin/out new file mode 100755 index 0000000..2815b92 --- /dev/null +++ b/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 <&3