forked from metalmatze/alertmanager-bot
/
chat_info.go
60 lines (53 loc) · 1.66 KB
/
chat_info.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
package telegram
import (
"gopkg.in/tucnak/telebot.v2"
"strings"
)
type ChatInfo struct {
Chat *telebot.Chat
AlertEnvironments []string
AlertProjects []string
MutedEnvironments []string
MutedProjects []string
}
func (ch *ChatInfo) UnmuteEnvironment(env string, allEnvs []string) {
var index int
for i, value := range ch.MutedEnvironments {
if 0 == strings.Compare(value, env) {
index = i
break
}
}
ch.MutedEnvironments = append(ch.MutedEnvironments[:index], ch.MutedEnvironments[index+1:]...)
ch.AlertEnvironments = arrayDifference(allEnvs, ch.MutedEnvironments)
}
func (ch *ChatInfo) UnmuteProject(pr string, allPrs []string) {
var index int
for i, value := range ch.MutedProjects {
if 0 == strings.Compare(value, pr) {
index = i
break
}
}
ch.MutedProjects = append(ch.MutedProjects[:index], ch.MutedProjects[index+1:]...)
ch.AlertProjects = arrayDifference(allPrs, ch.MutedProjects)
}
func (ch *ChatInfo) MuteEnvironments(envsToMute []string, allEnvs []string) {
ch.MutedEnvironments = getUniqueStrings(append(ch.MutedEnvironments, envsToMute...))
ch.AlertEnvironments = arrayDifference(allEnvs, ch.MutedEnvironments)
}
func (ch *ChatInfo) MuteProjects(prsToMute []string, allPrs []string) {
ch.MutedProjects = getUniqueStrings(append(ch.MutedProjects, prsToMute...))
ch.AlertProjects = arrayDifference(allPrs, ch.MutedProjects)
}
func getUniqueStrings(values []string) []string {
uniqueSet := make(map[string]bool, len(values))
for _, x := range values {
uniqueSet[x] = true
}
uniqueValues := make([]string, 0, len(uniqueSet))
for x := range uniqueSet {
uniqueValues = append(uniqueValues, x)
}
return uniqueValues
}