-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.ps1
executable file
·341 lines (298 loc) · 9.82 KB
/
send.ps1
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
# 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"
$OS_NAME="Windows"
$STATUS="$($args[0])"
$WEBHOOK_URL="$($args[1])"
$CURRENT_TIME=[int64](([datetime]::UtcNow)-(get-date "1/1/1970")).TotalSeconds
$STRICT_MODE=$false
if ("$($args[3])" -eq "strict") {
$STRICT_MODE=$true
}
if (!$WEBHOOK_URL) {
Write-Output "WARNING!"
Write-Output "You need to pass the WEBHOOK_URL environment variable as the second argument to this script."
Write-Output "For details & guidance, visit: https://github.com/symboxtra/universal-ci-discord-webhook"
if ($STRICT_MODE) {
exit 1
}
else {
exit
}
}
# 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 ( "${env:JENKINS_URL}" -ne "" ) {
$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 ( "${env:ghprbSourceBranch}" -ne "" ) {
$BRANCH_NAME="${env:ghprbSourceBranch}"
}
elseif ( "${env:GIT_BRANCH}" -ne "" ) {
$BRANCH_NAME="${env:GIT_BRANCH}"
}
elseif ( "${env:BRANCH_NAME}" -ne "" ) {
$BRANCH_NAME="${env:BRANCH_NAME}"
}
# COMMIT_HASH
if ( "${env:ghprbActualCommit}" -ne "" ) {
$COMMIT_HASH="${env:ghprbActualCommit}"
}
elseif ( "${env:GIT_COMMIT}" -ne "" ) {
$COMMIT_HASH="${env:GIT_COMMIT}"
}
# PULL_REQUEST_ID
if ( "${env:ghprbPullId}" -ne "" ) {
$PULL_REQUEST_ID="${env:ghprbPullId}"
}
elseif ( "${env:CHANGE_ID}" -ne "" ) {
$PULL_REQUEST_ID="${env:CHANGE_ID}"
}
$REPO_URL="$(git remote get-url origin)"
$REPO_SLUG=$($REPO_URL -replace '.*github.com/', '' -replace '\.git.*', '')
$BUILD_NUMBER="${env:BUILD_NUMBER}"
$BUILD_URL="${env:BUILD_URL}/console"
}
elseif ( "${env:CI}" -eq "True" -And "${env:APPVEYOR}" -eq "True" ) {
$CI_PROVIDER="AppVeyor"
$DISCORD_AVATAR="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Appveyor_logo.svg/256px-Appveyor_logo.svg.png"
$SUCCESS_AVATAR="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Appveyor_logo.svg/256px-Appveyor_logo.svg.png"
$FAILURE_AVATAR="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Appveyor_logo.svg/256px-Appveyor_logo.svg.png"
$UNKNOWN_AVATAR="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Appveyor_logo.svg/256px-Appveyor_logo.svg.png"
$BRANCH_NAME="${env:APPVEYOR_REPO_BRANCH}"
$COMMIT_HASH="${env:APPVEYOR_REPO_COMMIT}"
$PULL_REQUEST_ID="$env:APPVEYOR_PULL_REQUEST_NUMBER"
$REPO_SLUG="$env:APPVEYOR_REPO_NAME"
$BUILD_NUMBER="$env:APPVEYOR_BUILD_NUMBER"
$BUILD_URL="https://ci.appveyor.com/project/$env:APPVEYOR_ACCOUNT_NAME/$env:APPVEYOR_PROJECT_SLUG/build/$env:APPVEYOR_BUILD_VERSION"
}
else {
Write-Output "No CI service detected. Service not found or not supported? Open an issue on GitHub!"
if ($STRICT_MODE) {
exit 1
}
else {
exit
}
}
# Check that all variables were found
$ALL_FOUND=$true
if ( "${CI_PROVIDER}" -eq "") {
Write-Output "CI_PROVIDER not defined."
$ALL_FOUND=$false
}
if ( "${DISCORD_AVATAR}" -eq "") {
Write-Output "DISCORD_AVATAR not defined."
$ALL_FOUND=$false
}
if ( "${SUCCESS_AVATAR}" -eq "") {
Write-Output "SUCCESS_AVATAR not defined."
$ALL_FOUND=$false
}
if ( "${FAILURE_AVATAR}" -eq "") {
Write-Output "FAILURE_AVATAR not defined."
$ALL_FOUND=$false
}
if ( "${UNKNOWN_AVATAR}" -eq "") {
Write-Output "UNKNOWN_AVATAR not defined."
$ALL_FOUND=$false
}
if ( "${BRANCH_NAME}" -eq "") {
Write-Output "BRANCH_NAME not defined."
$ALL_FOUND=$false
}
if ( "${COMMIT_HASH}" -eq "") {
Write-Output "COMMIT_HASH not defined."
$ALL_FOUND=$false
}
if ( "${REPO_SLUG}" -eq "") {
Write-Output "REPO_SLUG not defined."
$ALL_FOUND=$false
}
if ( "${BUILD_NUMBER}" -eq "") {
Write-Output "BUILD_NUMBER not defined."
$ALL_FOUND=$false
}
if ( "${BUILD_URL}" -eq "") {
Write-Output "BUILD_URL not defined."
$ALL_FOUND=$false
}
if ($STRICT_MODE -And !$ALL_FOUND) {
Write-Output "[Webhook]: CI detection failed. Strict mode was enabled and one or more variables was undefined."
exit 1
}
""
Write-Output "[Webhook]: ${CI_PROVIDER} CI detected."
Write-Output "[Webhook]: Sending webhook to Discord..."
""
Switch ($STATUS) {
"success" {
$EMBED_COLOR=3066993
$STATUS_MESSAGE="Passed"
$AVATAR="${SUCCESS_AVATAR}"
Break
}
"failure" {
$EMBED_COLOR=15158332
$STATUS_MESSAGE="Failed"
$AVATAR="${FAILURE_AVATAR}"
Break
}
default {
$EMBED_COLOR=8421504
$STATUS_MESSAGE="Unknown"
$AVATAR="${UNKNOWN_AVATAR}"
Break
}
}
$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} -eq ${COMMITTER_NAME}) {
$CREDITS="${AUTHOR_NAME} authored & committed"
}
else {
$CREDITS="${AUTHOR_NAME} authored & ${COMMITTER_NAME} committed"
}
# Calculate approximate build time based on commit
$BUILD_TIME=${CURRENT_TIME}-${COMMIT_TIME}
$TIME_STAMP=$([timespan]::fromseconds(${BUILD_TIME}))
$DISPLAY_TIME=$("{0:mm:ss}" -f ([datetime]${TIME_STAMP}.Ticks))
# Regex match co-author names
if (${COMMIT_MESSAGE} -match 'Co-authored-by:') {
[array] $RESULTS=[regex]::Matches("${COMMIT_MESSAGE}", '\w+\s\w+')
if ($RESULTS.Count -gt 0)
{
$CO_AUTHORS=$RESULTS.Value
}
$CO_AUTHORS = ${CO_AUTHORS} -join ', '
# Clear the commit message
# This could be improved to just regex out the Co-authored-by messages
$COMMIT_MESSAGE=""
}
else {
$CO_AUTHORS="None"
}
# Replace git hashes in merge commits
if (${COMMIT_SUBJECT} -match 'Merge \w{40}\b into \w{40}\b') {
$IS_PR=$true
[array] $RESULTS=[regex]::Matches("${COMMIT_SUBJECT}", '\w{40}\b')
foreach ($MATCH in $RESULTS)
{
$HASH=$MATCH.Value
$BRANCH_NAME="$(git name-rev ${HASH} --name-only)"
if ($BRANCH_NAME) {
$COMMIT_SUBJECT="${COMMIT_SUBJECT}" -replace "${HASH}", "${BRANCH_NAME}"
}
}
}
if ( ${IS_PR} -AND "${PULL_REQUEST_ID}" -eq "") {
Write-Output "PULL_REQUEST_ID not defined."
if ( ${STRICT_MODE} ) {
Write-Output "[Webhook]: CI detection failed. Strict mode was enabled and one or more variables was undefined."
exit 1
}
}
# Remove repo owner: symboxtra/project -> project
$REPO_NAME=${REPO_SLUG} -replace '^[^/]*\/', ''
# Create appropriate link
if ("${PULL_REQUEST_ID}" -ne "" -Or ${IS_PR}) {
if (${PULL_REQUEST_ID}) {
$URL="https://github.com/${REPO_SLUG}/pull/${PULL_REQUEST_ID}"
}
else {
$URL="https://github.com/${REPO_SLUG}/pulls"
}
}
else {
$URL="https://github.com/${REPO_SLUG}/commit/${COMMIT_HASH}"
}
$TIMESTAMP="$(Get-Date -format s)Z"
$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.substring(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}""
} ]
}"
Invoke-RestMethod -Uri "${WEBHOOK_URL}" -Method "POST" -UserAgent "${CI_PROVIDER}-Webhook" `
-ContentType "application/json" -Header @{"X-Author"="jmcker#6584"} `
-Body ${WEBHOOK_DATA}
if ( -not $? ) {
""
Write-Output "Webhook Data:\n${WEBHOOK_DATA}"
Write-Output "[Webhook]: Unable to send webhook."
# Don't exit with error unless we're in strict mode
if ($STRICT_MODE) {
exit 1
}
}
else {
Write-Output "[Webhook]: Successfully sent the webhook."
}
# It's been tested on Windows Jenkins now :)