You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.2 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/bin/bash
  2. # Send message to a matrix room. API call looks like this:
  3. # 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"
  4. set -e
  5. cd "${1}"
  6. exec 3>&1
  7. exec 1>&2
  8. # for jq
  9. PATH=/usr/local/bin:$PATH
  10. payload=$(mktemp /tmp/resource-in.XXXXXX)
  11. cat > "${payload}" <&0
  12. matrix_server_url="$(jq -r '.source.matrix_server_url' < "${payload}")"
  13. token="$(jq -r '.source.token' < "${payload}")"
  14. room_id="$(jq -r '.source.room_id' < "${payload}")"
  15. msgtype="$(jq -r '.source.msgtype // "m.notice"' < "${payload}")"
  16. data_file="$(jq -r '.source.data_file // ""' < "${payload}")"
  17. ts="$(date +%s)"
  18. matrix_endpoint="$matrix_server_url/_matrix/client/r0/rooms/$room_id/send/m.room.message/$ts?access_token=$token"
  19. text_file="$(jq -r '.params.text_file // ""' < "${payload}")"
  20. thisdata_file="$(jq -r '.params.data_file // ""' < "${payload}")"
  21. [[ -n $thisdata_file && $thisthisdata_file != $data_file ]] && data_file="${thisdata_file}"
  22. from="$(jq -r '.params.from // ""' < "${payload}")"
  23. text="$(jq '(.params.text // "${TEXT_FILE_CONTENT}")' < "${payload}")"
  24. thismsgtype="$(jq -r '.params.msgtype // ""' < "${payload}")"
  25. [[ -n $thismsgtype && $thismsgtype != $msgtype ]] && msgtype="${thismsgtype}"
  26. always_notify="$(jq -r '.params.always_notify // "false"' < "${payload}")"
  27. debug="$(jq -r '.params.debug // "false"' < "${payload}")"
  28. silent="$(jq -r '.params.silent // "true"' < "${payload}")"
  29. TEXT_FILE_CONTENT=""
  30. [[ -n "${text_file}" && -f "${text_file}" ]] && TEXT_FILE_CONTENT="$(cat "${text_file}")"
  31. DATA_FILE_CONTENT=""
  32. [[ -n "${data_file}" && -f "${data_file}" ]] && DATA_FILE_CONTENT="$(cat "${data_file}")"
  33. if [[ "$always_notify" == "true" || -n "$TEXT_FILE_CONTENT" || -z "$text_file" || -n "$DATA_FILE_CONTENT" ]]
  34. then
  35. TEXT_FILE_CONTENT="${TEXT_FILE_CONTENT:-_(no notification provided)_}"
  36. text="$(eval printf ${text} )"
  37. [[ -z "${text}" ]] && text="_(missing notification text)_"
  38. text="$(echo "${text}" | jq -R -s .)"
  39. [[ "${token}" != "null" ]] && username="$(eval "printf ${token}" | jq -R -s .)"
  40. [[ "${room_id}" != "null" ]] && room_id="$(eval "printf ${room_id}" | jq -R -s .)"
  41. body="$(cat <<EOF
  42. {
  43. "msgtype": ${msgtype},
  44. "body": ${text}
  45. }
  46. EOF
  47. )"
  48. if [ "${msgtype}" != "m.notice" ]; then
  49. # we can attach custom data...
  50. builddata="$(cat <<EOF
  51. {
  52. "build_job_name": "${BUILD_JOB_NAME}",
  53. "build_name": "${BUILD_NAME}",
  54. "build_pipeline_name": "${BUILD_PIPELINE_NAME}",
  55. "build_id": "${BUILD_ID}",
  56. "atc_external_url": "${ATC_EXTERNAL_URL}"
  57. }
  58. EOF
  59. )"
  60. body=$(echo $body | jq -c ".build=$builddata")
  61. if [ -n "$DATA_FILE_CONTENT" ]; then
  62. body=$(echo $body | jq -c ".data=$DATA_FILE_CONTENT")
  63. fi
  64. fi
  65. compact_body="$(echo "${body}" | jq -c '.')"
  66. if [[ "$silent" == "true" ]]
  67. then
  68. echo "Using silent output"
  69. curl -s -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "${compact_body}" "${matrix_endpoint}"
  70. else
  71. curl -v -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "${compact_body}" "${matrix_endpoint}"
  72. fi
  73. fi
  74. jq -n "{version:{timestamp:\"$(date +%s)\"}}" >&3