forked from drone-plugins/drone-slack
/
main.go
126 lines (109 loc) · 2.82 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
package main
import (
"bytes"
"fmt"
"os"
"strings"
"text/template"
"time"
"github.com/drone/drone-plugin-go/plugin"
)
type Slack struct {
Webhook string `json:"webhook_url"`
Channel string `json:"channel"`
Recipient string `json:"recipient"`
Username string `json:"username"`
Template string `json:"template"`
}
// create the Slack client
var client ClientImpl = &Client{}
func main() {
repo := plugin.Repo{}
build := plugin.Build{}
system := plugin.System{}
vargs := Slack{}
plugin.Param("build", &build)
plugin.Param("repo", &repo)
plugin.Param("system", &system)
plugin.Param("vargs", &vargs)
// parse the parameters
if err := plugin.Parse(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
client.SetUrl(vargs.Webhook)
// generate the Slack message
msg := Message{}
msg.Username = vargs.Username
msg.Channel = vargs.Recipient
if len(vargs.Recipient) != 0 {
msg.Channel = Prepend("@", vargs.Recipient)
} else {
msg.Channel = Prepend("#", vargs.Channel)
}
attach := msg.NewAttachment()
attach.Text = GetMessage(repo, build, system, vargs)
attach.Fallback = GetFallback(&repo, &build)
attach.Color = GetColor(&build)
attach.MrkdwnIn = []string{"text", "fallback"}
// sends the message
if err := client.SendMessage(&msg); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func Prepend(prefix, s string) string {
if !strings.HasPrefix(s, prefix) {
return prefix + s
}
return s
}
var defaultTemplate = "*{{.Build.Status}}* <{{.System.Link}}/{{.Repo.FullName}}/{{.Build.Number}}|{{.Repo.FullName}}#{{ShortCommit .Build}}> ({{.Build.Branch}}) by {{.Build.Author}}"
func GetMessage(repo plugin.Repo, build plugin.Build, system plugin.System, vargs Slack) string {
templateString := defaultTemplate
if len(vargs.Template) != 0 {
templateString = vargs.Template
}
t := template.New("_").Funcs(template.FuncMap{
"Duration": func(build plugin.Build) time.Duration {
return time.Duration(build.Finished-build.Started) * time.Second
},
"ShortCommit": func(build plugin.Build) string { return build.Commit[:8] },
})
t, err := t.Parse(templateString)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
data := struct {
Build plugin.Build
System plugin.System
Repo plugin.Repo
}{build, system, repo}
var textBuf bytes.Buffer
if err := t.Execute(&textBuf, &data); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
return textBuf.String()
}
func GetFallback(repo *plugin.Repo, build *plugin.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 GetColor(build *plugin.Build) string {
switch build.Status {
case plugin.StateSuccess:
return "good"
case plugin.StateFailure, plugin.StateError, plugin.StateKilled:
return "danger"
default:
return "warning"
}
}