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.

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