forked from bodin/ci-teams-notification-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathout
58 lines (41 loc) · 1.38 KB
/
out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash
CURL_OPTION=""
set -e
cd "${1}"
exec 3>&1
exec 1>&2
#set -x
payload=$(mktemp /tmp/resource-in.XXXXXX)
message_file=$(mktemp /tmp/resource-in.XXXXXX)
cat > "${payload}" <&0
echo "Using temporary file ${payload} for payload"
echo "Using temporary file ${message_file} for message content"
text_file="$(jq -r '.params.text_file // ""' < "${payload}")"
text=$(jq -r '(.params.text // null)' < $payload)
FILE_CONTENT=""
if [[ -n "${text_file}" && -f "${text_file}" ]]; then
FILE_CONTENT="$(cat "${text_file}")"
fi
if [[ "${text}" == "null" && -n $FILE_CONTENT ]]; then
text=$FILE_CONTENT
fi
#text=$(echo "$text" | envsubst)
text="$(echo "${text}" | jq -c '.')"
echo $text
echo $text > $message_file
webhook_url="$(jq -r '.source.url' < "${payload}")"
redacted_webhook_url=$(echo "${webhook_url}" | sed -e 's#/\([^/\.]\{2\}\)[^/.]\{5,\}\([^/.]\{2\}\)#/\1…\2#g' | jq -R .)
url_path="$(echo ${webhook_url} | sed -e "s/https\{0,1\}:\/\/[^\/]*\(\/[^?&#]*\).*/\1/")"
curl -v -H "Content-Type: application/json" -d "@${message_file}" "${webhook_url}" 2>&1 | sed -e "s#${url_path}#***WEBHOOK URL REDACTED***#g"
timestamp=$(date +%s)
metadata="$(cat <<EOF
{
"version": {"timestamp": "${timestamp}"},
"metadata": [
{"name": "url", "value": ${redacted_webhook_url}},
{"name": "BUILD_PIPELINE_NAME", "value": "${BUILD_PIPELINE_NAME}"}
]
}
EOF
)"
echo "$metadata" >&3