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.

139 lines
4.9 KiB

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