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.

116 lines
4.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
8 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. trigger="$(jq -r '.params.trigger // ""' < "${payload}")"
  24. text="$(jq '(.params.text // "${TEXT_FILE_CONTENT}")' < "${payload}")"
  25. thismsgtype="$(jq -r '.params.msgtype // ""' < "${payload}")"
  26. [[ -n $thismsgtype && $thismsgtype != $msgtype ]] && msgtype="${thismsgtype}"
  27. always_notify="$(jq -r '.params.always_notify // "false"' < "${payload}")"
  28. debug="$(jq -r '.params.debug // "false"' < "${payload}")"
  29. silent="$(jq -r '.params.silent // "true"' < "${payload}")"
  30. link="$(jq -r '.params.link // "false"' < "${payload}")"
  31. team="$(jq -r '.params.team // "main"' < "${payload}")"
  32. prefix="$(jq -r '.params.prefix // ""' < "${payload}")"
  33. TEXT_FILE_CONTENT=""
  34. [[ -n "${text_file}" && -f "${text_file}" ]] && TEXT_FILE_CONTENT="$(cat "${text_file}")"
  35. DATA_FILE_CONTENT=""
  36. [[ -n "${data_file}" && -f "${data_file}" ]] && DATA_FILE_CONTENT="$(cat "${data_file}")"
  37. if [[ "$always_notify" == "true" || -n "$TEXT_FILE_CONTENT" || -z "$text_file" || -n "$DATA_FILE_CONTENT" ]]
  38. then
  39. TEXT_FILE_CONTENT="${TEXT_FILE_CONTENT:-_(no notification provided)_}"
  40. text="$(eval printf ${text} )"
  41. [[ -z "${text}" ]] && text="_(missing notification text)_"
  42. [ "${link}" == "true" ] && formatted_body="<a href=\"$ATC_EXTERNAL_URL/teams/${team}/pipelines/$BUILD_PIPELINE_NAME/jobs/$BUILD_JOB_NAME/builds/$BUILD_NAME\">${text}</a>"
  43. if [ -n "$prefix" ]; then
  44. prefix="$(eval printf ${prefix} )"
  45. text="${prefix}: ${text}"
  46. [ -n "${formatted_body}" ] && formatted_body="${prefix}: ${formatted_body}"
  47. fi
  48. text="$(echo -n "${text}" | jq -R -s .)"
  49. [ -n "${formatted_body}" ] && formatted_body="$(echo -n "${formatted_body}" | jq -R -s .)"
  50. [[ "${token}" != "null" ]] && username="$(eval "printf ${token}" | jq -R -s .)"
  51. [[ "${room_id}" != "null" ]] && room_id="$(eval "printf ${room_id}" | jq -R -s .)"
  52. body="$(cat <<EOF
  53. {
  54. "msgtype": "${msgtype}",
  55. "body": ${text}
  56. }
  57. EOF
  58. )"
  59. # if [ "${msgtype}" != "m.notice" ]; then
  60. # we can attach custom data...
  61. builddata="$(cat <<EOF
  62. {
  63. "build_job_name": "${BUILD_JOB_NAME}",
  64. "build_name": "${BUILD_NAME}",
  65. "build_pipeline_name": "${BUILD_PIPELINE_NAME}",
  66. "build_team_name": "${BUILD_TEAM_NAME}",
  67. "build_id": "${BUILD_ID}",
  68. "atc_external_url": "${ATC_EXTERNAL_URL}"
  69. }
  70. EOF
  71. )"
  72. body=$(echo -n $body | jq -c ".build=$builddata")
  73. if [ -n "${formatted_body}" ]; then
  74. body=$(echo -n $body | jq -c ".formatted_body=$formatted_body")
  75. body=$(echo -n $body | jq -c ".format=\"org.matrix.custom.html\"")
  76. fi
  77. if [ -n "$DATA_FILE_CONTENT" ]; then
  78. body=$(echo -n $body | jq -c ".data=$DATA_FILE_CONTENT")
  79. fi
  80. if [ -n "${trigger}" ]; then
  81. body=$(echo -n $body | jq -c ".trigger=\"${trigger}\"")
  82. fi
  83. # fi
  84. compact_body="$(echo -n "${body}" | jq -c '.')"
  85. if [[ "$silent" == "true" ]]
  86. then
  87. echo "Using silent output"
  88. curl -s -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "${compact_body}" "${matrix_endpoint}"
  89. else
  90. curl -v -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "${compact_body}" "${matrix_endpoint}"
  91. fi
  92. fi
  93. jq -n "{version:{timestamp:\"$(date +%s)\"}}" >&3