forked from drone-plugins/drone-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
186 lines (162 loc) · 3.26 KB
/
plugin.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"strings"
"github.com/drone/drone-template-lib/template"
"github.com/slack-go/slack"
)
type (
Repo struct {
Owner string
Name string
}
Build struct {
Tag string
Event string
Number int
Parent int
Commit string
Ref string
Branch string
Author Author
Pull string
Message Message
DeployTo string
Status string
Link string
Started int64
Created int64
}
Author struct {
Username string
Name string
Email string
Avatar string
}
Message struct {
msg string
Title string
Body string
}
Config struct {
Webhook string
Channel string
Recipient string
Username string
Template string
Fallback string
ImageURL string
IconURL string
IconEmoji string
Color string
LinkNames bool
}
Job struct {
Started int64
}
Plugin struct {
Repo Repo
Build Build
Config Config
Job Job
}
)
func (a Author) String() string {
return a.Username
}
func newCommitMessage(m string) Message {
// not checking the length here
// as split will always return at least one element
// check it if using more than the first element
splitMsg := strings.Split(m, "\n")
return Message{
msg: m,
Title: strings.TrimSpace(splitMsg[0]),
Body: strings.TrimSpace(strings.Join(splitMsg[1:], "\n")),
}
}
func (m Message) String() string {
return m.msg
}
func (p Plugin) Exec() error {
attachment := slack.Attachment{
Color: p.Config.Color,
ImageURL: p.Config.ImageURL,
AuthorName: "drone-slack",
MarkdownIn: []string{"text", "fallback"},
}
if p.Config.Color == "" {
attachment.Color = color(p.Build)
}
if p.Config.Fallback != "" {
f, err := templateMessage(p.Config.Fallback, p)
if err != nil {
return err
}
attachment.Fallback = f
} else {
attachment.Fallback = fallback(p.Repo, p.Build)
}
if p.Config.Template != "" {
var err error
f, err := templateMessage(p.Config.Template, p)
if err != nil {
return err
}
attachment.Text = f
} else {
attachment.Text = message(p.Repo, p.Build)
}
payload := slack.WebhookMessage{
Username: p.Config.Username,
Attachments: []slack.Attachment{attachment},
IconURL: p.Config.IconURL,
IconEmoji: p.Config.IconEmoji,
}
if p.Config.Recipient != "" {
payload.Channel = prepend("@", p.Config.Recipient)
} else if p.Config.Channel != "" {
payload.Channel = prepend("#", p.Config.Channel)
}
return slack.PostWebhook(p.Config.Webhook, &payload)
}
func templateMessage(t string, plugin Plugin) (string, error) {
return template.RenderTrim(t, plugin)
}
func message(repo Repo, build Build) string {
return fmt.Sprintf("*%s* <%s|%s/%s#%s> (%s) by %s",
build.Status,
build.Link,
repo.Owner,
repo.Name,
build.Commit[:8],
build.Branch,
build.Author,
)
}
func fallback(repo Repo, build Build) string {
return fmt.Sprintf("%s %s/%s#%s (%s) by %s",
build.Status,
repo.Owner,
repo.Name,
build.Commit[:8],
build.Branch,
build.Author,
)
}
func color(build Build) string {
switch build.Status {
case "success":
return "good"
case "failure", "error", "killed":
return "danger"
default:
return "warning"
}
}
func prepend(prefix, s string) string {
if !strings.HasPrefix(s, prefix) {
return prefix + s
}
return s
}