-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplugin.go
executable file
·106 lines (92 loc) · 1.71 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
package main
import (
"fmt"
"github.com/drone/drone-template-lib/template"
"github.com/josmo/drone-google-chat/google-chat"
"strings"
)
type (
Repo struct {
Owner string
Name string
}
Build struct {
Tag string
Event string
Number int
Commit string
Ref string
Branch string
Author string
Message string
Status string
Link string
Started int64
Created int64
}
Config struct {
Webhook string
Template string
Token string
Key string
ConversationKey string
}
Job struct {
Started int64
}
Plugin struct {
Repo Repo
Build Build
Config Config
Job Job
}
)
func (p Plugin) Exec() error {
text := message(p.Repo, p.Build)
if p.Config.Template != "" {
txt, err := template.RenderTrim(p.Config.Template, p)
if err != nil {
return err
}
text = txt
}
client := google_chat.NewClient(p.Config.Webhook, p.Config.Key, p.Config.Token, p.Config.ConversationKey)
return client.SendMessage(&google_chat.Message{text})
}
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
}