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