-
Notifications
You must be signed in to change notification settings - Fork 25
/
rhtap_bug_slis_alert.go
172 lines (141 loc) · 3.79 KB
/
rhtap_bug_slis_alert.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
package server
import (
"fmt"
"github.com/konflux-ci/quality-dashboard/api/server/router/jira"
"github.com/robfig/cron"
"github.com/slack-go/slack"
"go.uber.org/zap"
)
type Team struct {
ComponentName string
Bugs []jira.Bug
}
func getMention(team string) string {
switch team {
case "ic-appstudio-qe":
return "<!subteam^S03PD4MV58W|ic-appstudio-qe>"
case "quality":
return "<!subteam^S02G2PFJ4AV|appstudio-qe-team> and <!subteam^S03E4H1JLF5|hacbs-qe-team>"
case "spi":
return "<!subteam^SLRSHSU1K|spi-rhtap-team>"
case "has":
return "<!subteam^S04MSCVRF4Z|app-has>"
case "integration":
return "<!subteam^S041261DDEW|rhtap-test-team>"
case "pipeline":
return "<!subteam^S03GF42RBE2|plnsvc-team>"
case "gitops":
return "<!subteam^S01AC8DU22C|gitops-team>"
case "build":
return "<!subteam^S03DM1RL0TF|konfluxbld-green>"
case "release":
return "<!subteam^S03SVBS426R|stonesoup-release-team>"
case "o11y":
return "<!subteam^S04S21ECL8K|rhtap-o11y-all>"
case "ec":
return "<!subteam^S04123TQ599|hacbs-contract-team>"
case "hac":
return "<!subteam^S02J1EUMMNV|hac-core-team>"
case "sandbox":
return "<!subteam^SKBFYSRAL|sandbox-team>"
default:
return ""
}
// missing:
// core
// docs
// java-rebuild
// performance
// security
// sre
// uxd
}
func getMessage(alert jira.Alert, alertType string) string {
if alert.Signal == alertType {
return *alert.AlertMessage + "\n\n"
}
return ""
}
func (s *Server) sendAlert(team, msg, color, channelID string) {
// get team mention
mention := getMention(team)
message := slack.Attachment{
Pretext: fmt.Sprintf("Hello %s!", mention),
Text: msg,
Color: color,
}
_, _, err := s.cfg.Slack.PostMessage(
channelID,
slack.MsgOptionAttachments(message),
)
if err != nil {
s.cfg.Logger.Sugar().Errorf("Failed to post message on slack", zap.Error(err))
}
}
func teamExists(componentName string, teams []Team) int {
for i, team := range teams {
if team.ComponentName == componentName {
return i
}
}
return -1
}
func slisByTeam(bugs []jira.Bug) []Team {
teams := make([]Team, 0)
for _, bug := range bugs {
idx := teamExists(bug.Component, teams)
if idx != -1 {
teams[idx].Bugs = append(teams[idx].Bugs, bug)
} else {
teams = append(teams, Team{ComponentName: bug.Component, Bugs: []jira.Bug{bug}})
}
}
return teams
}
func (s *Server) sendAlerts() {
// only for KFLUXBUGS for now
bugs, err := s.cfg.Storage.GetAllOpenBugsForSliAlerts("KFLUXBUGS")
if err != nil {
s.cfg.Logger.Sugar().Errorf("Failed to get all open RHTAP Bug SLOs", zap.Error(err))
}
slis := jira.GetBugSLIs(bugs)
teams := slisByTeam(slis.Bugs)
// channel: konflux-bug-slis-alerts
channelID := "C062AF1RFK8"
for _, team := range teams {
redMsg := ""
yellowMsg := ""
componentAssignmentMsg := ""
for _, bug := range team.Bugs {
redMsg += getMessage(*bug.TriageSLI, "red")
redMsg += getMessage(*bug.ResponseSLI, "red")
redMsg += getMessage(*bug.ResolutionSLI, "red")
if team.ComponentName == "undefined" {
// should mention ic-appstudio-qe
componentAssignmentMsg += getMessage(*bug.ComponentAssignmentTriageSLI, "red")
}
yellowMsg += getMessage(*bug.TriageSLI, "yellow")
yellowMsg += getMessage(*bug.ResolutionSLI, "yellow")
}
if redMsg != "" {
s.sendAlert(team.ComponentName, redMsg, "#FF0000", channelID)
}
if componentAssignmentMsg != "" {
s.sendAlert("ic-appstudio-qe", componentAssignmentMsg, "#FF0000", channelID)
}
if yellowMsg != "" {
s.sendAlert(team.ComponentName, yellowMsg, "#FFFF00", channelID)
}
}
}
func (s *Server) SendBugSLIAlerts() {
cron := cron.New()
// every day at 9am
err := cron.AddFunc("0 0 9 * * *", func() {
s.sendAlerts()
})
if err != nil {
s.cfg.Logger.Sugar().Errorf("Failed to add cron", zap.Error(err))
}
cron.Start()
}