Browse Source

Add support for params.data_file, source.msgtype, and params.msgtype

master
John Locke 8 years ago
parent
commit
11bf454409
  1. 16
      README.md
  2. 35
      bin/out

16
README.md

@ -39,6 +39,9 @@ Then, a user will need to invite the account to the appropriate room, and the ac
* `matrix_server_url`: *Required.* Example: https://matrix.org * `matrix_server_url`: *Required.* Example: https://matrix.org
* `token`: *Required.* token to authenticate with Matrix server * `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. * `room_id`: *Required.* The room to send notifications to -- this account must already be a member of this room.
* `msgtype`: Used to post a custom message type e.g. if you want to attach a json blob. If set to anything other than m.notice, the resource will attach a "build" json object containing the build metadata info. Defaults to `m.notice`, can be overridden by the put resource.
* `data_file`: *Optional.* (string) Default file to post to the data key of a custom message type. The contents of this file is generally assumed to be a JSON-encoded string. Can be overridden in the job parameters.
Pull requests accepted for room_alias, user logins, auto-joins. Pull requests accepted for room_alias, user logins, auto-joins.
@ -47,13 +50,15 @@ Pull requests accepted for room_alias, user logins, auto-joins.
Send message to specified Matrix Room, with the configured parameters Send message to specified Matrix Room, with the configured parameters
#### Parameters #### Parameters
* `text`: (string) Text to send to the Matrix room
* `text_file`: (string) File containing text to send to the Matrix room
* `text`: (string) Text to send to the Matrix room as the content.body.
* `text_file`: (string) File containing text to send to the Matrix room as the content.body.
* `msgtype`: *Optional.* Message type, m.notice, m.text (default: m.notice) * `msgtype`: *Optional.* Message type, m.notice, m.text (default: m.notice)
* `data_file`: *Optional.* (string) Filename to post using a custom_event message type. If unset, defaults to the data_file on the resource.
* `always_notify`: If true, send a notice even if text_file and data_file are empty. If false, and a text_file is specified but empty, a notification will not be sent.
## Example ## Example
### Check
### Resources
``` ```
--- ---
resources: resources:
@ -63,8 +68,13 @@ resources:
matrix_server_url: https://matrix.org matrix_server_url: https://matrix.org
token: {{matrix_token}} token: {{matrix_token}}
room_id: {{matrix_room_id}} room_id: {{matrix_room_id}}
data_file: data
``` ```
### Check
*Not supported*
### In ### In
*Not supported* *Not supported*

35
bin/out

@ -27,6 +27,7 @@ ts="$(date +%s)"
matrix_endpoint="$matrix_server_url/_matrix/client/r0/rooms/$room_id/send/m.room.message/$ts?access_token=$token" 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}")" text_file="$(jq -r '.params.text_file // ""' < "${payload}")"
data_file="$(jq -r '.params.data_file // ""' < "${payload}")"
from="$(jq -r '.params.from // ""' < "${payload}")" from="$(jq -r '.params.from // ""' < "${payload}")"
text="$(jq '(.params.text // "${TEXT_FILE_CONTENT}")' < "${payload}")" text="$(jq '(.params.text // "${TEXT_FILE_CONTENT}")' < "${payload}")"
msgtype="$(jq '(.params.msgtype // "m.notice")' < "${payload}")" msgtype="$(jq '(.params.msgtype // "m.notice")' < "${payload}")"
@ -37,8 +38,10 @@ silent="$(jq -r '.params.silent // "false"' < "${payload}")"
TEXT_FILE_CONTENT="" TEXT_FILE_CONTENT=""
[[ -n "${text_file}" && -f "${text_file}" ]] && TEXT_FILE_CONTENT="$(cat "${text_file}")" [[ -n "${text_file}" && -f "${text_file}" ]] && TEXT_FILE_CONTENT="$(cat "${text_file}")"
DATA_FILE_CONTENT=""
[[ -n "${data_file}" && -f "${data_file}" ]] && DATA_FILE_CONTENT="$(cat "${data_file}")"
if [[ "$always_notify" == "true" || -n "$TEXT_FILE_CONTENT" || -z "$text_file" ]]
if [[ "$always_notify" == "true" || -n "$TEXT_FILE_CONTENT" || -z "$text_file" || -n "$DATA_FILE_CONTENT" ]]
then then
TEXT_FILE_CONTENT="${TEXT_FILE_CONTENT:-_(no notification provided)_}" TEXT_FILE_CONTENT="${TEXT_FILE_CONTENT:-_(no notification provided)_}"
@ -55,21 +58,27 @@ then
} }
EOF EOF
)" )"
compact_body="$(echo "${body}" | jq -c '.')"
if [[ "$debug" == "true" ]]
then
json="$(cat <<EOF
if [ "$msgtype" != "m.notice" ]; then
// we can attach custom data...
builddata="$(cat <<EOF
{ {
"matrix_endpoint": "${matrix_endpoint}",
"body": ${body}
"build_job_name": "${BUILD_JOB_NAME}",
"build_name": "${BUILD_NAME}",
"build_pipeline_name": "${BUILD_PIPELINE_NAME}",
"build_id": "${BUILD_ID}",
"atc_external_url": "${ATC_EXTERNAL_URL}"
} }
EOF EOF
)"
echo "$json" | jq -c '.'
exit 0
elif [[ "$silent" == "true" ]]
)"
body=$(echo body | jq -c ".build=$builddata")
if [ -n "$DATA_FILE_CONTENT" ]; then
body=$(echo body | jq -c ".data=$DATA_FILE_CONTENT")
fi
fi
compact_body="$(echo "${body}" | jq -c '.')"
if [[ "$silent" == "true" ]]
then then
echo "Using silent output" echo "Using silent output"
curl -s -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "${compact_body}" "${matrix_endpoint}" curl -s -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "${compact_body}" "${matrix_endpoint}"

Loading…
Cancel
Save