-
Notifications
You must be signed in to change notification settings - Fork 15
/
function.go
216 lines (186 loc) · 5.03 KB
/
function.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package function
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/dustin/go-humanize"
)
type Notification struct {
Incident Incident `json:"incident"`
Version string `json:"version"`
}
type Incident struct {
ProjectID string `json:"scoping_project_id"`
IncidentID string `json:"incident_id"`
ResourceID string `json:"resource_id"`
ResourceName string `json:"resource_name"`
State string `json:"state"`
StartedAt int64 `json:"started_at"`
EndedAt int64 `json:"ended_at,omitempty"`
PolicyName string `json:"policy_name"`
ConditionName string `json:"condition_name"`
URL string `json:"url"`
Summary string `json:"summary"`
}
type MessageCard struct {
Type string `json:"@type"`
Context string `json:"@context"`
Summary string `json:"summary,omitempty"`
Title string `json:"title,omitempty"`
Text string `json:"text,omitempty"`
ThemeColor string `json:"themeColor,omitempty"`
Sections []Section `json:"sections,omitempty"`
PotentialActions []PotentialAction `json:"potentialAction,omitempty"`
}
type Section struct {
Facts []Fact `json:"facts,omitempty"`
}
type Fact struct {
Name string `json:"name"`
Value string `json:"value"`
}
type PotentialAction struct {
Type string `json:"@type"`
Name string `json:"name"`
Targets []map[string]string `json:"targets,omitempty"`
}
func toTeams(notification Notification) MessageCard {
var startedDt time.Time
var endedDt time.Time
if st := notification.Incident.StartedAt; st > 0 {
startedDt = time.Unix(st, 0)
}
if et := notification.Incident.EndedAt; et > 0 {
endedDt = time.Unix(et, 0)
}
projectId := notification.Incident.ProjectID
if projectId == "" {
projectId = "-"
}
policyName := notification.Incident.PolicyName
if policyName == "" {
policyName = "-"
}
conditionName := notification.Incident.ConditionName
if conditionName == "" {
conditionName = "-"
}
facts := []Fact{
{
Name: "Project ID",
Value: notification.Incident.ProjectID,
},
{
Name: "Incident ID",
Value: notification.Incident.IncidentID,
},
{
Name: "Condition",
Value: conditionName,
},
}
if !startedDt.IsZero() {
facts = append(facts, Fact{
Name: "Started at",
Value: startedDt.String(),
})
if !endedDt.IsZero() {
duration := strings.TrimSpace(humanize.RelTime(startedDt, endedDt, "", ""))
facts = append(facts, Fact{
Name: "Ended at",
Value: fmt.Sprintf("%s (%s)", endedDt.String(), duration),
})
}
}
// Green
colour := "#18FF3B"
title := fmt.Sprintf(`"%s" - Incident closed for "%s".`, projectId, policyName)
if notification.Incident.State == "open" {
// Red
title = fmt.Sprintf(`"%s" - Incident opened for "%s".`, projectId, policyName)
colour = "#F5222D"
}
summary := "No summary available."
if notification.Incident.Summary != "" {
summary = notification.Incident.Summary
}
return MessageCard{
Type: "MessageCard",
Context: "https://schema.org/extensions",
ThemeColor: colour,
Title: title,
Text: summary,
Summary: summary,
Sections: []Section{
{
Facts: facts,
},
},
PotentialActions: []PotentialAction{
{
Type: "OpenUri",
Name: "View Incident",
Targets: []map[string]string{
{
"os": "default",
"uri": notification.Incident.URL,
},
},
},
},
}
}
func F(w http.ResponseWriter, r *http.Request) {
authToken := os.Getenv("AUTH_TOKEN")
if authToken == "" {
log.Fatalln("`AUTH_TOKEN` is not set in the environment")
}
if r.URL.Query().Get("auth_token") != authToken {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("invalid request"))
return
}
teamsWebhookURL := os.Getenv("TEAMS_WEBHOOK_URL")
if teamsWebhookURL == "" {
log.Fatalln("`TEAMS_WEBHOOK_URL` is not set in the environment")
}
if _, err := url.Parse(teamsWebhookURL); err != nil {
log.Fatalln(err)
}
if contentType := r.Header.Get("Content-Type"); r.Method != "POST" || contentType != "application/json" {
log.Printf("\ninvalid method / content-type: %s / %s \n", r.Method, contentType)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("invalid request"))
return
}
var notification Notification
if err := json.NewDecoder(r.Body).Decode(¬ification); err != nil {
log.Fatalln(err)
}
teamsWebhook := toTeams(notification)
payload, err := json.Marshal(teamsWebhook)
if err != nil {
log.Fatalln(err)
}
res, err := http.Post(teamsWebhookURL, "application/json", bytes.NewBuffer(payload))
if err != nil {
log.Fatalln(err)
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 300 {
log.Println("payload", string(payload))
log.Fatalln("unexpected status code", res.StatusCode)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(teamsWebhook)
if err != nil {
log.Fatalln(err)
}
}