-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathout
135 lines (109 loc) · 4.2 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env bash
CURL_OPTION=""
set -e
exec 3>&1
exec 1>&2
#set -x
function evaluate {
__var=$1
# escape ( and ) from markdown
__var="${__var//\(/\\(}"
__var="${__var//\)/\\)}"
__var="${__var//\:/\\:}"
__var=`eval echo $__var`
echo $__var
}
payload=$(mktemp /tmp/resource-in.XXXXXX)
cat > "${payload}" <&0
format="markdown"
actionName=$(evaluate "$(jq -r '.params.actionName // "Open Concourse"' < "${payload}")")
actionTarget=$(evaluate "$(jq -r '.params.actionTarget // "https://concourse-ci.org/"' < "${payload}")")
title=$(evaluate "$(jq -r '.params.title // "Concourse CI"' < "${payload}")")
text=$(evaluate "$(jq -r '.params.text // ""' < "${payload}")")
text_file="$1/"$(evaluate "$(jq -r '.params.text_file // ""' < "${payload}")")
# - 'text' always overrides 'text_file'
# - '_(NO MSG)_' being the default if neither are provded
# - If text file is missing: _(NO MSG FILE)_
# - If text file is blank: _(EMPTY MSG FILE)_"
text_output="_(NO MSG)_"
if [[ -n "${text_file}" ]]; then
if [[ ! -f "${text_file}" ]]; then
text_output="_(NO MSG FILE)_"
else
text_output="$(cat "${text_file}")" && \
[[ -z "${text_output}" ]] && \
text_output="_(EMPTY MSG FILE)_"
fi
fi
[[ -n "${text}" ]] && text_output="${text}"
# style should be one of 'good', 'attention', 'warning', 'default'
# good (green) if the build succeeded
# attention (red) if the build failed
# warning (yellow) if the build errored
# default (gray) by default
# look for pass/fail/error in the title
style=$(evaluate "$(jq -r '.params.style // "default"' < "${payload}")")
[[ "${title,,}" =~ (pass|succeed|success) ]] && style="good"
[[ "${title,,}" =~ (fail) ]] && style="attention"
[[ "${title,,}" =~ (error|warning) ]] && style="warning"
# or in the text_output if we didn't find it in the title
[[ "${style}" == "default" ]] && [[ "${text_output,,}" =~ (pass|succeed|success) ]] && style="good"
[[ "${style}" == "default" ]] && [[ "${text_output,,}" =~ (fail) ]] && style="attention"
[[ "${style}" == "default" ]] && [[ "${text_output,,}" =~ (error|warning) ]] && style="warning"
DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
body=$(jq -n --arg format "${format}" \
--arg actionName "${actionName}" \
--arg actionTarget "${actionTarget}" \
--arg title "${title}" \
--arg text "${text_output}" \
--arg style "${style}" \
-f ${DIR}/card.jq)
webhook_url="$(jq -r '.source.url // empty' < "${payload}")"
proxy_url="$(jq -r '.source.proxy_url // empty' < "${payload}")"
proxy_port="$(jq -r '.source.proxy_port // empty' < "${payload}")"
proxy_username="$(jq -r '.source.proxy_username // empty' < "${payload}")"
proxy_password="$(jq -r '.source.proxy_password // empty' < "${payload}")"
skip_cert_verification="$(jq -r '.source.skip_cert_verification // empty' < "${payload}")"
verbose="$(jq -r '.source.verbose // empty' < "${payload}")"
silent="$(jq -r '.source.silent // empty' < "${payload}")"
if [ -z ${proxy_url} ] || [ -z ${proxy_port} ]; then
PROXY_OPTIONS=""
else
PROXY_OPTIONS="--proxy ${proxy_url}:${proxy_port}"
fi
if [ -z ${proxy_url} ] || [ -z ${proxy_port} ] || [ -z ${proxy_username} ] || [ -z ${proxy_password} ]; then
PROXY_OPTIONS="${PROXY_OPTIONS}"
else
PROXY_OPTIONS="--proxy-user ${proxy_username}:${proxy_password} ${PROXY_OPTIONS}"
fi
if [ -z ${skip_cert_verification} ]; then
CURL_OPTION="${CURL_OPTION}"
else
CURL_OPTION="-k ${CURL_OPTION}"
fi
if [ -z ${verbose} ]; then
CURL_OPTION="${CURL_OPTION}"
else
CURL_OPTION="-v ${CURL_OPTION}"
fi
if [ -z ${silent} ]; then
CURL_OPTION="${CURL_OPTION}"
else
CURL_OPTION="-s ${CURL_OPTION}"
fi
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 ${CURL_OPTION} ${PROXY_OPTIONS} -H 'Content-Type: application/json' -d "${body}" "${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