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.

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