forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.go
109 lines (92 loc) · 3.34 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
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
package notify
import (
"encoding/json"
"fmt"
"github.com/drone/drone/shared/model"
)
const (
slackStartedMessage = "*Building* <%s|%s> (%s) by %s"
slackStartedFallbackMessage = "Building %s (%s) by %s"
slackSuccessMessage = "*Success* <%s|%s> (%s) by %s"
slackSuccessFallbackMessage = "Success %s (%s) by %s"
slackFailureMessage = "*Failed* <%s|%s> (%s) by %s"
slackFailureFallbackMessage = "Failed %s (%s) by %s"
drone_icon = "https://avatars.githubusercontent.com/drone"
)
type Slack struct {
WebhookUrl string `yaml:"webhook_url,omitempty"`
Channel string `yaml:"channel,omitempty"`
Username string `yaml:"username,omitempty"`
Started bool `yaml:"on_started,omitempty"`
Success bool `yaml:"on_success,omitempty"`
Failure bool `yaml:"on_failure,omitempty"`
Change bool `yaml:"on_change,omitempty"`
}
func (s *Slack) Send(context *model.Request) error {
switch {
case context.Commit.Status == "Started" && s.Started:
return s.sendStarted(context)
case context.Commit.Status == "Success":
if s.Success || (s.Change && context.Prior.Status != context.Commit.Status) {
return s.sendSuccess(context)
}
case context.Commit.Status == "Failure":
if s.Failure || (s.Change && context.Prior.Status != context.Commit.Status) {
return s.sendFailure(context)
}
}
return nil
}
func (s *Slack) getMessage(context *model.Request, message string) string {
url := getBuildUrl(context)
// drone/drone#3333333
linktext := context.Repo.Owner + "/" + context.Repo.Name + "#" + context.Commit.ShaShort()
return fmt.Sprintf(message, url, linktext, context.Commit.Branch, context.Commit.Author)
}
func (s *Slack) getFallbackMessage(context *model.Request, message string) string {
// drone/drone#3333333
text := context.Repo.Owner + "/" + context.Repo.Name + "#" + context.Commit.ShaShort()
return fmt.Sprintf(message, text, context.Commit.Branch, context.Commit.Author)
}
func (s *Slack) sendStarted(context *model.Request) error {
return s.send(s.getMessage(context, slackStartedMessage)+"\n - "+context.Commit.Message,
s.getFallbackMessage(context, slackStartedFallbackMessage), "warning")
}
func (s *Slack) sendSuccess(context *model.Request) error {
return s.send(s.getMessage(context, slackSuccessMessage),
s.getFallbackMessage(context, slackSuccessFallbackMessage), "good")
}
func (s *Slack) sendFailure(context *model.Request) error {
return s.send(s.getMessage(context, slackFailureMessage),
s.getFallbackMessage(context, slackFailureFallbackMessage), "danger")
}
// helper function to send HTTP requests
func (s *Slack) send(msg string, fallback string, color string) error {
type Attachment struct {
Fallback string `json:"fallback"`
Text string `json:"text"`
Color string `json:"color"`
MrkdwnIn []string `json:"mrkdwn_in"`
}
attachments := []Attachment{
Attachment{
fallback,
msg,
color,
[]string{"fallback", "text"},
},
}
// data will get posted in this format
data := struct {
Channel string `json:"channel"`
Username string `json:"username"`
Icon string `json:"icon_url"`
Attachments []Attachment `json:"attachments"`
}{s.Channel, s.Username, drone_icon, attachments}
// data json encoded
payload, err := json.Marshal(data)
if err != nil {
return err
}
return sendJson(s.WebhookUrl, payload, nil)
}