-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.go
72 lines (64 loc) · 1.67 KB
/
slack.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
package main
import (
"fmt"
"strings"
"github.com/slack-go/slack"
)
func buildSlackMessage(filter Filter, findings []Finding) slack.MsgOption {
blocks := []slack.Block{}
headerString := fmt.Sprintf("Top %d findings of the day 🌞", filter.Limit)
if len(findings) == 0 {
headerString = "Found nothing 🏜️ this could be good news 🤞"
}
headerText := slack.NewTextBlockObject(
"plain_text",
headerString,
true,
false,
)
header := slack.NewHeaderBlock(headerText)
blocks = append(blocks, header)
labels := []string{}
for l, v := range filter.Labels {
labels = append(labels, fmt.Sprintf("%s=%s", l, v))
}
contextText := slack.NewTextBlockObject(
"plain_text",
fmt.Sprintf("Filter: %s", strings.Join(labels, ",")),
true,
false,
)
context := slack.NewContextBlock("context", contextText)
blocks = append(blocks, context)
for i, finding := range findings {
if i > 0 {
blocks = append(blocks, slack.NewDividerBlock())
}
text := slack.NewTextBlockObject("mrkdwn",
fmt.Sprintf(
"*[%d]* `%s` in %s *%s/%s*\n```%s```\n",
i+1,
finding.Policy,
strings.ToLower(finding.Resource.Kind),
finding.Resource.Namespace,
finding.Resource.Name,
finding.Message,
),
false,
false,
)
if finding.Properties != nil {
for k, v := range finding.Properties {
text.Text += fmt.Sprintf("*%s:* %s\n", k, v)
}
}
text.Text += fmt.Sprintf(
"```kubectl get -n %s %s/%s```",
finding.Resource.Namespace,
strings.ToLower(finding.Resource.Kind), finding.Resource.Name,
)
section := slack.NewSectionBlock(text, []*slack.TextBlockObject{}, nil)
blocks = append(blocks, section)
}
return slack.MsgOptionBlocks(blocks...)
}