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.

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