forked from nanoframework/nf-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord-webhook-task.yml
166 lines (135 loc) · 5.24 KB
/
discord-webhook-task.yml
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
# Copyright (c) .NET Foundation and Contributors
# Portions Copyright (c) Sankarsan Kampa (a.k.a. k3rn31p4nic). All rights reserved.
# See LICENSE file in the project root for full license information.
parameters:
- name: status
type: string
default: failure
- name: webhookUrl
type: string
default: ''
- name: message
type: string
default: ''
- name: workingDirectory
type: string
default: '$(Build.SourcesDirectory)'
steps:
- task: PowerShell@2
displayName: Call Discord webhook
condition: failed()
env:
GITHUB_TOKEN: $(GitHubToken)
inputs:
workingDirectory: '${{ parameters.workingDirectory }}'
errorActionPreference: 'silentlyContinue'
targetType: 'inline'
script: |
# check if this is a PR
if($env:System_PullRequest_PullRequestId -ne $null)
{
Write-Host "Building from PR"
$user = $(gh pr view $env:System_PullRequest_PullRequestNumber --json author --jq .author.login)
Write-Host "PR from user: $user"
# only report to Discord if this is from an automated build
if($user -eq 'nfbot' -or $user -eq 'github-actions[bot]' -or $user -eq 'github-actions' -or $user -eq 'dependabot[bot]' -or $user -eq 'dependabot')
{
Write-Host "Go and report failure to Discord"
$CallDiscord = $TRUE
}
else
{
Write-Host "Don't report failure to Discord"
$CallDiscord = $FALSE
}
}
else
{
# build not from PR, report to Discord
Write-Host "Build from other than PR, report to Discord"
$CallDiscord = $TRUE
}
if($CallDiscord)
{
Write-Host "Preparing call to Discord webhook..."
$STATUS = "${{ parameters.status }}"
Switch ($STATUS) {
"success" {
$EMBED_COLOR=3066993
$STATUS_MESSAGE="Passed"
Break
}
"failure" {
$EMBED_COLOR=15158332
$STATUS_MESSAGE="Failed"
Break
}
"warning" {
$EMBED_COLOR=12370112
$STATUS_MESSAGE="Warning"
Break
}
default {
Write-Output "Default!"
Break
}
}
$AUTHOR_NAME="$(git log -1 "$env:Build_SourceVersion" --pretty="%aN")"
$COMMITTER_NAME="$(git log -1 "$env:Build_SourceVersion" --pretty="%cN")"
$COMMIT_SUBJECT="$(git log -1 "$env:Build_SourceVersion" --pretty="%s")"
$COMMIT_MESSAGE="$(git log -1 "$env:Build_SourceVersion" --pretty="%b")"
if ($AUTHOR_NAME -eq $COMMITTER_NAME)
{
$CREDITS = "$AUTHOR_NAME authored & committed"
}
else
{
$CREDITS = "$AUTHOR_NAME authored & $COMMITTER_NAME committed"
}
$WEBHOOK_URL = "${{ parameters.webhookUrl }}"
$MESSAGE = "${{ parameters.message }}"
if ($env:System_PullRequest_PullRequestNumber) {
$URL="/~https://github.com/$env:Build_Repository_Name/pull/$env:System_PullRequest_PullRequestNumber"
}
$BUILD_VERSION = [uri]::EscapeDataString($env:Build_BuildNumber)
[System.Collections.ArrayList]$embedArray = @()
$name = "Job #$env:Build_BuildId (Build #$env:Build_BuildNumber) $STATUS_MESSAGE - $env:Build_Repository_Name"
$url = "https://dev.azure.com/nanoframework/$env:System_TeamProject/_build/results?buildId=$env:Build_BuildId&view=results"
$description = "$COMMIT_MESSAGE`\n$CREDITS`\n$MESSAGE"
$authorObject = [PSCustomObject]@{
name = $name
url = $url
icon_url = $AVATAR
}
$field1Value = "[``$($env:Build_SourceVersion.substring(0, 7))``](/~https://github.com/$env:Build_Repository_Name/commit/$env:Build_SourceVersion)"
$field1Object = [PSCustomObject]@{
name = "Commit"
value = $field1Value
inline = 'true'
}
$field2Value = "[``$env:Build_SourceBranchName``](/~https://github.com/$env:Build_Repository_Name/tree/$env:Build_SourceBranchName)"
$field2Object = [PSCustomObject]@{
name = "Branch/Tag"
value = $field2Value
inline = 'true'
}
[System.Collections.ArrayList]$fieldsArray = @()
$fieldsArray.Add($field1Object)
$fieldsArray.Add($field2Object)
$embedObject = [PSCustomObject]@{
color = $EMBED_COLOR
author = $authorObject
title = $COMMIT_SUBJECT
url = $url
description = $description
fields = $fieldsArray
}
$embedArray.Add($embedObject)
$payload = [PSCustomObject]@{
embeds = $embedArray
}
[Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls,Tls11,Tls12'
$sC = Invoke-RestMethod -Uri $WEBHOOK_URL -Body ($payload | ConvertTo-Json -Depth 4) -Method 'Post' -ContentType 'application/json'
Write-Host "Status: $sC"
}
- template: report-build-failure.yml