-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (111 loc) · 2.85 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/ashwanthkumar/slack-go-webhook"
"github.com/kelseyhightower/envconfig"
)
type config struct {
ArgoCDBaseURL string `required:"true" split_words:"true"`
ArgoCDProject string `required:"true" split_words:"true"`
ArgoCDApplication string `required:"true" split_words:"true"`
ArgoCDEventType string `required:"true" split_words:"true"`
SlackURL string `required:"true" split_words:"true"`
SlackChannel string `required:"true" split_words:"true"`
SlackUsername string `default:"Notifier"`
SlackIconEmoji string `default:":monkey:" split_words:"true"`
SlackText string `default:"Deployment notification" split_words:"true"`
SlackFooterText string `default:"" split_words:"true"`
}
func (c config) appLink() string {
return fmt.Sprintf("%s/applications/%s", c.ArgoCDBaseURL, c.ArgoCDApplication)
}
func (c config) projectLink() string {
return fmt.Sprintf("%s/settings/projects/%s", c.ArgoCDBaseURL, c.ArgoCDProject)
}
func (c config) rollbackLink() string {
return fmt.Sprintf("%s/applications/%s?rollback=0", c.ArgoCDBaseURL, c.ArgoCDApplication)
}
func main() {
var err error
var c config
err = envconfig.Process("notifier", &c)
if err != nil {
log.Println("Config Error:", err.Error())
os.Exit(1)
}
payload := slack.Payload{
Channel: c.SlackChannel,
Username: c.SlackUsername,
IconEmoji: c.SlackIconEmoji,
Attachments: []slack.Attachment{},
}
payload.Attachments = attachments(c)
if c.ArgoCDEventType == "PreSync" {
payload.Text = c.SlackText
}
errs := slack.Send(c.SlackURL, "", payload)
if len(errs) > 0 {
log.Println("Send Errors:")
for _, e := range errs {
log.Println(" - ", e.Error())
}
}
}
func attachments(c config) []slack.Attachment {
t := time.Now()
ts := t.Unix()
o := []slack.Attachment{}
a := slack.Attachment{
Footer: &c.SlackFooterText,
Timestamp: &ts,
}
status := ""
colour := ""
switch c.ArgoCDEventType {
case "PreSync":
status = "Starting Deployment 💥"
colour = "warning"
break
case "Sync":
status = "Synchronising Code 🤖"
colour = "warning"
break
case "PostSync":
status = "Deployment Completed 🚀"
colour = "good"
break
case "SyncFail":
status = "DeploymentFailed 💀"
colour = "danger"
break
}
a.Color = &colour
a.AddField(slack.Field{
Title: "",
Value: status,
Short: false,
})
a.AddField(slack.Field{
Title: "Project",
Value: fmt.Sprintf("<%s|%s>", c.projectLink(), c.ArgoCDProject),
Short: true,
})
a.AddField(slack.Field{
Title: "Application",
Value: fmt.Sprintf("<%s|%s>", c.appLink(), c.ArgoCDApplication),
Short: true,
})
if c.ArgoCDEventType == "PostSync" {
a.AddAction(slack.Action{
Type: "button",
Text: "Initiate Rollback",
Url: c.rollbackLink(),
Style: "danger",
})
}
o = append(o, a)
return o
}