/
google_chat.go
151 lines (126 loc) · 3.82 KB
/
google_chat.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
/*
Copyright 2020 The Flux authors
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 notifier
import (
"context"
"fmt"
"net/url"
"strings"
eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
)
// Slack holds the hook URL
type GoogleChat struct {
URL string
ProxyURL string
Username string
Channel string
}
// GoogleChatPayload holds the channel and attachments
type GoogleChatPayload struct {
Cards []GoogleChatCard `json:"cards"`
}
type GoogleChatCard struct {
Header GoogleChatCardHeader `json:"header"`
Sections []GoogleChatCardSection `json:"sections"`
}
type GoogleChatCardHeader struct {
Title string `json:"title"`
SubTitle string `json:"subtitle"`
ImageUrl *string `json:"imageUrl"`
ImageStyle *string `json:"imageStyle"`
}
type GoogleChatCardSection struct {
Header string `json:"header"`
Widgets []GoogleChatCardWidget `json:"widgets"`
}
type GoogleChatCardWidget struct {
TextParagraph *GoogleChatCardWidgetTextParagraph `json:"textParagraph"`
KeyValue *GoogleChatCardWidgetKeyValue `json:"keyValue"`
}
type GoogleChatCardWidgetTextParagraph struct {
Text string `json:"text"`
}
type GoogleChatCardWidgetKeyValue struct {
TopLabel string `json:"topLabel"`
Content string `json:"content"`
ContentMultiLine bool `json:"contentMultiline"`
BottomLabel *string `json:"bottomLabel"`
Icon *string `json:"icon"`
}
// NewGoogleChat validates the Google Chat URL and returns a GoogleChat object
func NewGoogleChat(hookURL string, proxyURL string) (*GoogleChat, error) {
_, err := url.ParseRequestURI(hookURL)
if err != nil {
return nil, fmt.Errorf("invalid Google Chat hook URL %s", hookURL)
}
return &GoogleChat{
URL: hookURL,
ProxyURL: proxyURL,
}, nil
}
// Post Google Chat message
func (s *GoogleChat) Post(ctx context.Context, event eventv1.Event) error {
// Skip Git commit status update event.
if event.HasMetadata(eventv1.MetaCommitStatusKey, eventv1.MetaCommitStatusUpdateValue) {
return nil
}
// Header
objName := fmt.Sprintf("%s/%s.%s", strings.ToLower(event.InvolvedObject.Kind), event.InvolvedObject.Name, event.InvolvedObject.Namespace)
header := GoogleChatCardHeader{
Title: objName,
SubTitle: event.ReportingController,
}
sections := make([]GoogleChatCardSection, 0)
// Message
messageText := event.Message
if event.Severity == eventv1.EventSeverityError {
messageText = fmt.Sprintf("<font color=\"#ff0000\">%s</font>", event.Message)
}
sections = append(sections, GoogleChatCardSection{
Widgets: []GoogleChatCardWidget{
{
TextParagraph: &GoogleChatCardWidgetTextParagraph{
Text: messageText,
},
},
},
})
// Meta-Data
if len(event.Metadata) > 0 {
kvfields := make([]GoogleChatCardWidget, 0, len(event.Metadata))
for k, v := range event.Metadata {
kvfields = append(kvfields, GoogleChatCardWidget{
KeyValue: &GoogleChatCardWidgetKeyValue{
TopLabel: k,
Content: v,
ContentMultiLine: false,
},
})
}
sections = append(sections, GoogleChatCardSection{
Widgets: kvfields,
})
}
card := GoogleChatCard{
Header: header,
Sections: sections,
}
payload := GoogleChatPayload{
Cards: []GoogleChatCard{card},
}
err := postMessage(ctx, s.URL, s.ProxyURL, nil, payload)
if err != nil {
return fmt.Errorf("postMessage failed: %w", err)
}
return nil
}