-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathsend.sh
117 lines (104 loc) · 3.3 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
#!/bin/bash
case $1 in
"success" )
EMBED_COLOR=3066993
STATUS_MESSAGE="Passed"
ARTIFACT_URL="$CI_JOB_URL/artifacts/download"
;;
"failure" )
EMBED_COLOR=15158332
STATUS_MESSAGE="Failed"
ARTIFACT_URL="Not available"
;;
* )
EMBED_COLOR=0
STATUS_MESSAGE="Status Unknown"
ARTIFACT_URL="Not available"
;;
esac
shift
if [ $# -lt 1 ]; then
echo -e "WARNING!!\nYou need to pass the WEBHOOK_URL environment variable as the second argument to this script.\nFor details & guide, visit: https://github.com/DiscordHooks/gitlab-ci-discord-webhook" && exit
fi
AUTHOR_NAME="$(git log -1 "$CI_COMMIT_SHA" --pretty="%aN")"
COMMITTER_NAME="$(git log -1 "$CI_COMMIT_SHA" --pretty="%cN")"
COMMIT_SUBJECT="$(git log -1 "$CI_COMMIT_SHA" --pretty="%s")"
COMMIT_MESSAGE="$(git log -1 "$CI_COMMIT_SHA" --pretty="%b")" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g'
if [ "$AUTHOR_NAME" == "$COMMITTER_NAME" ]; then
CREDITS="$AUTHOR_NAME authored & committed"
else
CREDITS="$AUTHOR_NAME authored & $COMMITTER_NAME committed"
fi
if [ -z $CI_MERGE_REQUEST_ID ]; then
URL=""
else
URL="$CI_PROJECT_URL/merge_requests/$CI_MERGE_REQUEST_ID"
fi
TIMESTAMP=$(date --utc +%FT%TZ)
if [ -z $LINK_ARTIFACT ] || [ $LINK_ARTIFACT = false ] ; then
WEBHOOK_DATA='{
"avatar_url": "https://gitlab.com/favicon.png",
"embeds": [ {
"color": '$EMBED_COLOR',
"author": {
"name": "Pipeline #'"$CI_PIPELINE_IID"' '"$STATUS_MESSAGE"' - '"$CI_PROJECT_PATH_SLUG"'",
"url": "'"$CI_PIPELINE_URL"'",
"icon_url": "https://gitlab.com/favicon.png"
},
"title": "'"$COMMIT_SUBJECT"'",
"url": "'"$URL"'",
"description": "'"${COMMIT_MESSAGE//$'\n'/ }"\\n\\n"$CREDITS"'",
"fields": [
{
"name": "Commit",
"value": "'"[\`$CI_COMMIT_SHORT_SHA\`]($CI_PROJECT_URL/commit/$CI_COMMIT_SHA)"'",
"inline": true
},
{
"name": "Branch",
"value": "'"[\`$CI_COMMIT_REF_NAME\`]($CI_PROJECT_URL/tree/$CI_COMMIT_REF_NAME)"'",
"inline": true
}
],
"timestamp": "'"$TIMESTAMP"'"
} ]
}'
else
WEBHOOK_DATA='{
"avatar_url": "https://gitlab.com/favicon.png",
"embeds": [ {
"color": '$EMBED_COLOR',
"author": {
"name": "Pipeline #'"$CI_PIPELINE_IID"' '"$STATUS_MESSAGE"' - '"$CI_PROJECT_PATH_SLUG"'",
"url": "'"$CI_PIPELINE_URL"'",
"icon_url": "https://gitlab.com/favicon.png"
},
"title": "'"$COMMIT_SUBJECT"'",
"url": "'"$URL"'",
"description": "'"${COMMIT_MESSAGE//$'\n'/ }"\\n\\n"$CREDITS"'",
"fields": [
{
"name": "Commit",
"value": "'"[\`$CI_COMMIT_SHORT_SHA\`]($CI_PROJECT_URL/commit/$CI_COMMIT_SHA)"'",
"inline": true
},
{
"name": "Branch",
"value": "'"[\`$CI_COMMIT_REF_NAME\`]($CI_PROJECT_URL/tree/$CI_COMMIT_REF_NAME)"'",
"inline": true
},
{
"name": "Artifacts",
"value": "'"[\`$CI_JOB_ID\`]($ARTIFACT_URL)"'",
"inline": true
}
],
"timestamp": "'"$TIMESTAMP"'"
} ]
}'
fi
for ARG in "$@"; do
echo -e "[Webhook]: Sending webhook to Discord...\\n";
(curl --fail --progress-bar -A "GitLabCI-Webhook" -H Content-Type:application/json -H X-Author:k3rn31p4nic#8383 -d "$WEBHOOK_DATA" "$ARG" \
&& echo -e "\\n[Webhook]: Successfully sent the webhook.") || echo -e "\\n[Webhook]: Unable to send webhook."
done