-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.sh
executable file
·342 lines (298 loc) · 9.55 KB
/
send.sh
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/bin/bash
# Author: Symboxtra Software
# Author of travis-ci-discord Webhook and appveyor-discord-webhook:
# Sankarsan Kampa (a.k.a. k3rn31p4nic)
# License: MIT
WEBHOOK_VERSION="2.0.0.0"
unamestr=`uname`
if [ "${unamestr}" = 'Darwin' ]; then
OS_NAME="OSX"
else
OS_NAME="Linux"
fi
STATUS="$1"
WEBHOOK_URL="$2"
CURRENT_TIME=`date +%s`
STRICT_MODE=false
if [ "$3" = "strict" ]; then
STRICT_MODE=true
fi
if [ "${WEBHOOK_URL}" = "" ]; then
echo -e "WARNING!"
echo -e "You need to pass the WEBHOOK_URL environment variable as the second argument to this script."
echo -e "For details & guidance, visit: https://github.com/symboxtra/universal-ci-discord-webhook"
if [ "${STRICT_MODE}" = "true" ]; then
exit 1
else
exit 0
fi
fi
# The following variables must be defined for each CI service:
# CI_PROVIDER="" # Name of CI provider
# DISCORD_AVATAR="" # Large avatar for Discord user icon
# SUCCESS_AVATAR="" # Avatar for successful build
# FAILURE_AVATAR="" # Avatar for failed build
# UNKNOWN_AVATAR="" # Avatar for unknown build
# BRANCH_NAME="" # Branch name
# COMMIT_HASH="" # Hash of current commit
# PULL_REQUEST_ID="" # Id of PR if present
# REPO_SLUG="" # "owner/project" format for GitHub
# BUILD_NUMBER="" # Identifier for build
# BUILD_URL="" # Link to the build page
# These conditions come from the codecov bash script
# Apache License Version 2.0, January 2004
# https://github.com/codecov/codecov-bash/blob/master/LICENSE
if [ "${CI}" = "true" ] && [ "${TRAVIS}" = "true" ] && [ "${SHIPPABLE}" != "true" ];
then
CI_PROVIDER="Travis"
DISCORD_AVATAR="https://travis-ci.org/images/logos/TravisCI-Mascot-1.png"
SUCCESS_AVATAR="https://travis-ci.org/images/logos/TravisCI-Mascot-blue.png"
FAILURE_AVATAR="https://travis-ci.org/images/logos/TravisCI-Mascot-red.png"
UNKNOWN_AVATAR="https://travis-ci.org/images/logos/TravisCI-Mascot-1.png"
BRANCH_NAME="${TRAVIS_BRANCH}"
COMMIT_HASH="${TRAVIS_PULL_REQUEST_SHA:-$TRAVIS_COMMIT}"
PULL_REQUEST_ID="${TRAVIS_PULL_REQUEST}"
REPO_SLUG="${TRAVIS_REPO_SLUG}"
BUILD_NUMBER="${TRAVIS_BUILD_NUMBER}"
if [ "${TRAVIS_JOB_NUMBER}" != "" ]; then
BUILD_NUMBER="${TRAVIS_JOB_NUMBER}"
fi
BUILD_URL="https://travis-ci.org/${TRAVIS_REPO_SLUG}/builds/${TRAVIS_BUILD_ID}"
elif [ "${JENKINS_URL}" != "" ];
then
CI_PROVIDER="Jenkins"
DISCORD_AVATAR="https://wiki.jenkins.io/download/attachments/2916393/headshot.png?version=1&modificationDate=1302753947000&api=v2"
SUCCESS_AVATAR="https://jenkins.io/images/logos/cute/cute.png"
FAILURE_AVATAR="https://jenkins.io/images/logos/fire/fire.png"
UNKNOWN_AVATAR="https://wiki.jenkins.io/download/attachments/2916393/headshot.png?version=1&modificationDate=1302753947000&api=v2"
# BRANCH_NAME
if [ "${ghprbSourceBranch}" != "" ];
then
BRANCH_NAME="${ghprbSourceBranch}"
elif [ "${GIT_BRANCH}" != "" ];
then
BRANCH_NAME="${GIT_BRANCH}"
elif [ "${BRANCH_NAME}" != "" ];
then
BRANCH_NAME="${BRANCH_NAME}"
fi
# COMMIT_HASH
if [ "${ghprbActualCommit}" != "" ];
then
COMMIT_HASH="${ghprbActualCommit}"
elif [ "${GIT_COMMIT}" != "" ];
then
COMMIT_HASH="${GIT_COMMIT}"
fi
# PULL_REQUEST_ID
if [ "${ghprbPullId}" != "" ];
then
PULL_REQUEST_ID="${ghprbPullId}"
elif [ "${CHANGE_ID}" != "" ];
then
PULL_REQUEST_ID="${CHANGE_ID}"
fi
REPO_URL="$(git remote get-url origin)"
REPO_SLUG="$(echo ${REPO_URL} | sed -e 's/.*github.com\///g' -e 's/\.git.*//g')"
BUILD_NUMBER="${BUILD_NUMBER}"
BUILD_URL="${BUILD_URL}/console"
else
echo "No CI service detected. Service not found or not supported? Open an issue on GitHub!"
if [ "${STRICT_MODE}" = "true" ]; then
exit 1
else
exit 0
fi
fi
# Check that all variables were found
ALL_FOUND=true
if [ "${CI_PROVIDER}" = "" ]; then
echo "CI_PROVIDER not defined."
ALL_FOUND=false
fi
if [ "${DISCORD_AVATAR}" = "" ]; then
echo "DISCORD_AVATAR not defined."
ALL_FOUND=false
fi
if [ "${SUCCESS_AVATAR}" = "" ]; then
echo "SUCCESS_AVATAR not defined."
ALL_FOUND=false
fi
if [ "${FAILURE_AVATAR}" = "" ]; then
echo "FAILURE_AVATAR not defined."
ALL_FOUND=false
fi
if [ "${UNKNOWN_AVATAR}" = "" ]; then
echo "UNKNOWN_AVATAR not defined."
ALL_FOUND=false
fi
if [ "${BRANCH_NAME}" = "" ]; then
echo "BRANCH_NAME not defined."
ALL_FOUND=false
fi
if [ "${COMMIT_HASH}" = "" ]; then
echo "COMMIT_HASH not defined."
ALL_FOUND=false
fi
if [ "${REPO_SLUG}" = "" ]; then
echo "REPO_SLUG not defined."
ALL_FOUND=false
fi
if [ "${BUILD_NUMBER}" = "" ]; then
echo "BUILD_NUMBER not defined."
ALL_FOUND=false
fi
if [ "${BUILD_URL}" = "" ]; then
echo "BUILD_URL not defined."
ALL_FOUND=false
fi
if [ "${STRICT_MODE}" = "true" ] && [ "${ALL_FOUND}" = "false" ]; then
echo "[Webhook]: CI detection failed. Strict mode was enabled and one or more variables was undefined."
exit 1
fi
echo
echo -e "[Webhook]: ${CI_PROVIDER} CI detected."
echo -e "[Webhook]: Sending webhook to Discord..."
echo
case $1 in
"success" )
EMBED_COLOR=3066993
STATUS_MESSAGE="Passed"
AVATAR="${SUCCESS_AVATAR}"
;;
"failure" )
EMBED_COLOR=15158332
STATUS_MESSAGE="Failed"
AVATAR="${FAILURE_AVATAR}"
;;
* )
EMBED_COLOR=8421504
STATUS_MESSAGE="Unknown"
AVATAR="${UNKNOWN_AVATAR}"
;;
esac
AUTHOR_NAME="$(git log -1 ${COMMIT_HASH} --pretty="%aN")"
COMMITTER_NAME="$(git log -1 ${COMMIT_HASH} --pretty="%cN")"
COMMIT_SUBJECT="$(git log -1 ${COMMIT_HASH} --pretty="%s")"
COMMIT_MESSAGE="$(git log -1 ${COMMIT_HASH} --pretty="%b")"
COMMIT_TIME="$(git log -1 ${COMMIT_HASH} --pretty="%ct")"
if [ "${AUTHOR_NAME}" = "${COMMITTER_NAME}" ]; then
CREDITS="${AUTHOR_NAME} authored & committed"
else
CREDITS="${AUTHOR_NAME} authored & ${COMMITTER_NAME} committed"
fi
# Calculate approximate build time based on commit
DIFF_TIME=$(( ${CURRENT_TIME}-${COMMIT_TIME} ))
DISPLAY_TIME="$( printf "%02d" $(( ${DIFF_TIME} / 60 ))):$( printf "%02d" $(( ${DIFF_TIME} % 60 )))"
# Regex match co-author names
if [[ "${COMMIT_MESSAGE}" =~ Co-authored-by: ]]; then
IFS=$'\n'
CO_AUTHORS=($(echo "${COMMIT_MESSAGE}" | grep -o "\([A-Za-z]\+ \)\+[A-Za-z]\+"))
if [ ${#CO_AUTHORS[@]} -gt 0 ]; then
IFS=","
CO_AUTHORS="${CO_AUTHORS[*]}"
CO_AUTHORS="${CO_AUTHORS//,/, }"
fi
unset IFS
# Clear the commit message
# This could be improved to just regex out the Co-authored-by messages
COMMIT_MESSAGE=""
else
CO_AUTHORS="None"
fi
# Replace git hashes in merge commits
IFS=$'\n'
MATCHES=($(echo "${COMMIT_SUBJECT}" | grep -o "Merge \w\{40\}\b into \w\{40\}\b"))
if [ "${#MATCHES[@]}" -gt 0 ]; then
IS_PR=true
MATCHES=($(echo "${COMMIT_SUBJECT}" | grep -o "\w\{40\}\b"))
for MATCH in "${MATCHES[@]}"
do
HASH="${MATCH}"
BRANCH_NAME="$(git name-rev ${HASH} --name-only)"
COMMIT_SUBJECT="${COMMIT_SUBJECT//$HASH/${BRANCH_NAME:-$HASH}}"
done
fi
unset IFS
if [ $IS_PR ] && [ "${PULL_REQUEST_ID}" = "" ]; then
echo "PULL_REQUEST_ID not defined."
if [ "${STRICT_MODE}" = "true" ]; then
echo "[Webhook]: CI detection failed. Strict mode was enabled and one or more variables was undefined."
exit 1
fi
fi
# Remove repo owner: symboxtra/project -> project
REPO_NAME=${REPO_SLUG#*/}
# Create appropriate link
if ([ "${PULL_REQUEST_ID}" != "" ] || [ "${IS_PR}" = "true" ]) && [ "${PULL_REQUEST_ID}" != "false" ]; then
if [ "${PULL_REQUEST_ID}" != "" ]; then
URL="https://github.com/${REPO_SLUG}/pull/${PULL_REQUEST_ID}"
else
URL="https://github.com/${REPO_SLUG}/pulls"
fi
else
URL="https://github.com/${REPO_SLUG}/commit/${COMMIT_HASH}"
fi
TIMESTAMP=$(date -u +%FT%TZ)
WEBHOOK_DATA='{
"username": "",
"avatar_url": "'"${DISCORD_AVATAR}"'",
"embeds": [ {
"color": '${EMBED_COLOR}',
"author": {
"name": "'"${CI_PROVIDER}"' #'"${BUILD_NUMBER}"' - '"${REPO_NAME}"' - '"${STATUS_MESSAGE}"'",
"url": "'"${BUILD_URL}"'",
"icon_url": "'"${AVATAR}"'"
},
"title": "'"${COMMIT_SUBJECT}"'",
"url": "'"${URL}"'",
"description": "'"${COMMIT_MESSAGE}"\\n\\n"${CREDITS}"'",
"fields": [
{
"name": "OS",
"value": "'"${OS_NAME}"'",
"inline": true
},
{
"name": "Build Time",
"value": "'"~${DISPLAY_TIME}"'",
"inline": true
},
{
"name": "Build ID",
"value": "'"${BUILD_NUMBER%.*}CI"'",
"inline": true
},
{
"name": "Commit",
"value": "'"[\`${COMMIT_HASH:0:7}\`](https://github.com/${REPO_SLUG}/commit/${COMMIT_HASH})"'",
"inline": true
},
{
"name": "Branch/Tag",
"value": "'"[\`${BRANCH_NAME}\`](https://github.com/${REPO_SLUG}/tree/${BRANCH_NAME})"'",
"inline": true
},
{
"name": "Co-Authors",
"value": "'"${CO_AUTHORS}"'",
"inline": true
}
],
"footer": {
"text": "'"v${WEBHOOK_VERSION}"'"
},
"timestamp": "'"${TIMESTAMP}"'"
} ]
}'
curl -v --fail --progress-bar -A "${CI_PROVIDER}-Webhook" -H Content-Type:application/json -H X-Author:jmcker#6584 -d "${WEBHOOK_DATA}" "${WEBHOOK_URL}"
if [ $? -ne 0 ]; then
echo -e "Webhook data:\\n${WEBHOOK_DATA}"
echo -e "\\n[Webhook]: Unable to send webhook."
# Don't exit with error unless we're in strict mode
if [ "${STRICT_MODE}" = "true" ]; then
exit 1
fi
else
echo -e "\\n[Webhook]: Successfully sent the webhook."
fi