forked from samccone/cloud-build-discord-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
192 lines (164 loc) · 5.25 KB
/
main.go
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
// Copyright 2020 Google LLC
// (modified in 2022 for thorswap)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"github.com/GoogleCloudPlatform/cloud-build-notifiers/lib/notifiers"
cbpb "google.golang.org/genproto/googleapis/devtools/cloudbuild/v1"
)
const (
webhookURLSecretName = "webhookUrl"
swapkitAPIProjectPrefix = "swapkit-api"
discordUserToMention = "816849636601888800" // Replace with the actual Discord user ID
)
func main() {
if err := notifiers.Main(new(discordNotifier)); err != nil {
log.Fatalf("fatal error: %v", err)
}
}
type discordNotifier struct {
filter notifiers.EventFilter
webhookURL string
urls map[string]string
}
type embed struct {
Title string `json:"title"`
Color int `json:"color"`
Description string `json:"description"`
}
type discordMessage struct {
Username string `json:"username"`
Content string `json:"content"`
Embeds []embed `json:"embeds"`
}
func (s *discordNotifier) SetUp(ctx context.Context, cfg *notifiers.Config, sg notifiers.SecretGetter, _ notifiers.BindingResolver) error {
if cfg.Spec.Notification.Filter != "" {
prd, err := notifiers.MakeCELPredicate(cfg.Spec.Notification.Filter)
if err != nil {
return fmt.Errorf("failed to make a CEL predicate: %w", err)
}
s.filter = prd
}
s.urls = make(map[string]string)
for k, v := range cfg.Spec.Notification.Delivery {
log.Printf("Found entry for service %s", k)
notification := make(map[string]interface{})
for nk, nv := range v.(map[interface{}]interface{}) {
notification[fmt.Sprintf("%v", nk)] = nv
}
wuRef, err := notifiers.GetSecretRef(notification, webhookURLSecretName)
if err != nil {
return fmt.Errorf("failed to get Secret ref from delivery config (%v) field %q: %w", cfg.Spec.Notification.Delivery, webhookURLSecretName, err)
}
wuResource, err := notifiers.FindSecretResourceName(cfg.Spec.Secrets, wuRef)
if err != nil {
return fmt.Errorf("failed to find Secret for ref %q: %w", wuRef, err)
}
wu, err := sg.GetSecret(ctx, wuResource)
if err != nil {
return fmt.Errorf("failed to get token secret: %w", err)
}
s.urls[k] = wu
}
return nil
}
func (s *discordNotifier) SendNotification(ctx context.Context, build *cbpb.Build) error {
if s.filter != nil && s.filter.Apply(ctx, build) {
return nil
}
log.Printf("sending discord webhook for Build %q (status: %q)", build.Id, build.Status)
msg, err := s.buildMessage(build)
if err != nil {
return fmt.Errorf("failed to write discord message: %w", err)
}
if msg == nil {
return nil
}
payload, err := json.Marshal(msg)
if err != nil {
return fmt.Errorf("Unable to marshal payload %w", err)
}
svcName := build.Substitutions["_SERVICE_NAME"]
url, ok := s.urls[svcName]
if !ok {
url, ok = s.urls["default"]
if !ok {
return fmt.Errorf("failed to find delivery URL for service %v and no default provided", svcName)
}
}
log.Printf("sending payload %s", string(payload))
resp, err := http.Post(url, "application/json", bytes.NewBuffer(payload))
if err != nil {
return err
}
log.Printf("got resp %+v", resp)
return nil
}
func (s *discordNotifier) buildMessage(build *cbpb.Build) (*discordMessage, error) {
var embeds []embed
var content string
log.Printf("%+v", build)
repoName := build.Substitutions["REPO_NAME"]
triggerName := build.Substitutions["TRIGGER_NAME"]
projectID := build.ProjectId
svcName := build.Substitutions["_SERVICE_NAME"]
log.Printf("Triggered from repo, service, project: %v, %v, %v", repoName, svcName, projectID)
switch build.Status {
case cbpb.Build_WORKING:
embeds = append(embeds, embed{
Title: "🔨 BUILDING " + svcName,
Color: 1027128,
})
case cbpb.Build_SUCCESS:
embeds = append(embeds, embed{
Title: "✅ SUCCESS " + svcName,
Color: 1127128,
})
case cbpb.Build_FAILURE, cbpb.Build_INTERNAL_ERROR, cbpb.Build_TIMEOUT:
embeds = append(embeds, embed{
Title: fmt.Sprintf("❌ ERROR on %s - %s", svcName, build.Status),
Color: 14177041,
},
embed{
Title: "Log",
Description: build.LogUrl,
},
)
// Add @mention for failed builds in swapkit-api-dev project
if strings.HasPrefix(projectID, swapkitAPIProjectPrefix) {
content = fmt.Sprintf("<@%s> Build failed for %s in %s", discordUserToMention, svcName, projectID)
}
default:
log.Printf("Unknown status %s", build.Status)
}
if len(embeds) > 0 && len(repoName) > 0 {
embeds[0].Description = fmt.Sprintf("Source repo: %v\nTrigger: %v/%v", repoName, projectID, triggerName)
}
if len(embeds) == 0 {
log.Printf("unhandled status - skipping notification %s", build.Status)
return nil, nil
}
return &discordMessage{
Username: "Cloud Build Notifier",
Content: content,
Embeds: embeds,
}, nil
}