This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
forked from drone-plugins/drone-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
181 lines (158 loc) · 3.33 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
package main
import (
"fmt"
"io/ioutil"
"strings"
"github.com/bluele/slack"
"github.com/drone/drone-template-lib/template"
)
type (
Repo struct {
Owner string
Name string
}
Build struct {
Tag string
Event string
Number 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
AttachmentFile string
ImageURL string
IconURL string
IconEmoji 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{
Fallback: fallback(p.Repo, p.Build),
Color: color(p.Build),
MarkdownIn: []string{"text", "fallback"},
ImageURL: p.Config.ImageURL,
}
payload := slack.WebHookPostPayload{}
payload.Username = p.Config.Username
payload.Attachments = []*slack.Attachment{&attachment}
payload.IconUrl = p.Config.IconURL
payload.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)
}
if p.Config.LinkNames == true {
payload.LinkNames = "1"
}
if p.Config.Template != "" {
var err error
attachment.Text, err = templateMessage(p.Config.Template, p)
if err != nil {
return err
}
} else {
attachment.Text = message(p.Repo, p.Build)
}
if p.Config.AttachmentFile != "" {
dat, err := ioutil.ReadFile(p.Config.AttachmentFile)
if err != nil {
return err
}
attachment.Text = fmt.Sprintf("%s\n%s", attachment.Text, string(dat))
}
client := slack.NewWebHook(p.Config.Webhook)
return client.PostMessage(&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
}