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.

127 lines
4.6 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
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. # workaround for directory not existing before get in 3.0.0
  6. mkdir -p "${1}"
  7. cd "${1}"
  8. exec 3>&1
  9. exec 1>&2
  10. # for jq
  11. PATH=/usr/local/bin:$PATH
  12. payload=$(mktemp /tmp/resource-in.XXXXXX)
  13. cat > "${payload}" <&0
  14. echo "Matrix-Notification-Resource Version: 1.0.0"
  15. matrix_server_url="$(jq -r '.source.matrix_server_url' < "${payload}")"
  16. token="$(jq -r '.source.token' < "${payload}")"
  17. room_id="$(jq -r '.source.room_id' < "${payload}")"
  18. msgtype="$(jq -r '.source.msgtype // "m.notice"' < "${payload}")"
  19. data_file="$(jq -r '.source.data_file // ""' < "${payload}")"
  20. ts="$(date +%s)"
  21. matrix_endpoint="$matrix_server_url/_matrix/client/r0/rooms/$room_id/send/m.room.message/$ts?access_token=$token"
  22. text_file="$(jq -r '.params.text_file // ""' < "${payload}")"
  23. thisdata_file="$(jq -r '.params.data_file // ""' < "${payload}")"
  24. [[ -n $thisdata_file && $thisthisdata_file != $data_file ]] && data_file="${thisdata_file}"
  25. from="$(jq -r '.params.from // ""' < "${payload}")"
  26. trigger="$(jq -r '.params.trigger // ""' < "${payload}")"
  27. text="$(jq -r '(.params.text // "${TEXT_FILE_CONTENT}")' < "${payload}")"
  28. thismsgtype="$(jq -r '.params.msgtype // ""' < "${payload}")"
  29. [[ -n $thismsgtype && $thismsgtype != $msgtype ]] && msgtype="${thismsgtype}"
  30. always_notify="$(jq -r '.params.always_notify // "false"' < "${payload}")"
  31. debug="$(jq -r '.params.debug // "false"' < "${payload}")"
  32. silent="$(jq -r '.params.silent // "true"' < "${payload}")"
  33. link="$(jq -r '.params.link // "false"' < "${payload}")"
  34. team="$(jq -r '.params.team // "main"' < "${payload}")"
  35. prefix="$(jq -r '.params.prefix // ""' < "${payload}")"
  36. is_html="$(jq -r '.params.is_html // "false"' < "${payload}")"
  37. TEXT_FILE_CONTENT=""
  38. [[ -n "${text_file}" && ! -f "${text_file}" ]] && TEXT_FILE_CONTENT="_(text_file not found)_"
  39. if [[ -n "${text_file}" && -f "${text_file}" ]]; then
  40. TEXT_FILE_CONTENT="$(cat "${text_file}")"
  41. TEXT_FILE_CONTENT="${TEXT_FILE_CONTENT:-_(text_file empty)_}"
  42. fi
  43. DATA_FILE_CONTENT=""
  44. [[ -n "${data_file}" && -f "${data_file}" ]] && DATA_FILE_CONTENT="$(cat "${data_file}")"
  45. if [[ "$always_notify" == "true" || -n "$TEXT_FILE_CONTENT" || -z "$text_file" || -n "$DATA_FILE_CONTENT" ]]
  46. then
  47. export TEXT_FILE_CONTENT="${TEXT_FILE_CONTENT:-_(text_file not provided)_}"
  48. text=$(echo -en "${text}" | envsubst)
  49. [[ -z "${text}" ]] && text="_(missing notification text)_"
  50. [ "${link}" == "true" ] && formatted_body="<a href=\"$ATC_EXTERNAL_URL/teams/${team}/pipelines/$BUILD_PIPELINE_NAME/jobs/$BUILD_JOB_NAME/builds/$BUILD_NAME\">${text}</a>"
  51. if [ -n "$prefix" ]; then
  52. prefix=$(echo "$prefix" | envsubst)
  53. text="${prefix}: ${text}"
  54. [ -n "${formatted_body}" ] && formatted_body="${prefix}: ${formatted_body}"
  55. fi
  56. if [[ "${is_html}" == "true" && -n "${formatted_body}" ]]; then
  57. formatted_body="${text}"
  58. fi
  59. text=$(echo -n "${text}" | jq -R -s .)
  60. [ -n "${formatted_body}" ] && formatted_body=$(echo -n "${formatted_body}" | jq -R -s .)
  61. [[ "${token}" != "null" ]] && username=$(echo "$token" | envsubst | jq -R -s .)
  62. [[ "${room_id}" != "null" ]] && room_id=$(echo "$room_id" | envsubst | jq -R -s .)
  63. body="$(cat <<EOF
  64. {
  65. "msgtype": "${msgtype}",
  66. "body": ${text}
  67. }
  68. EOF
  69. )"
  70. # if [ "${msgtype}" != "m.notice" ]; then
  71. # we can attach custom data...
  72. builddata="$(cat <<EOF
  73. {
  74. "build_job_name": "${BUILD_JOB_NAME}",
  75. "build_name": "${BUILD_NAME}",
  76. "build_pipeline_name": "${BUILD_PIPELINE_NAME}",
  77. "build_team_name": "${BUILD_TEAM_NAME}",
  78. "build_id": "${BUILD_ID}",
  79. "atc_external_url": "${ATC_EXTERNAL_URL}"
  80. }
  81. EOF
  82. )"
  83. body=$(echo -n "$body" | jq -c ".build=$builddata")
  84. if [ -n "${formatted_body}" ]; then
  85. body=$(echo -n "$body" | jq -c ".formatted_body=$formatted_body")
  86. body=$(echo -n "$body" | jq -c ".format=\"org.matrix.custom.html\"")
  87. fi
  88. if [ -n "$DATA_FILE_CONTENT" ]; then
  89. body=$(echo -n "$body" | jq -c ".data=$DATA_FILE_CONTENT")
  90. fi
  91. if [ -n "${trigger}" ]; then
  92. body=$(echo -n "$body" | jq -c ".trigger=\"${trigger}\"")
  93. fi
  94. compact_body=$(echo -n "${body}" | jq -c '.')
  95. if [[ "$silent" == "true" ]]
  96. then
  97. echo "Using silent output"
  98. curl -s -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "${compact_body}" "${matrix_endpoint}"
  99. else
  100. curl -v -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "${compact_body}" "${matrix_endpoint}"
  101. fi
  102. fi
  103. jq -n "{version:{timestamp:\"$(date +%s)\"}}" >&3