forked from drone-plugins/drone-slack-blame
/
main.go
159 lines (129 loc) · 3.57 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
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
package main
import (
"fmt"
"math/rand"
"os"
"strings"
"time"
"github.com/drone/drone-plugin-go/plugin"
"github.com/nlopes/slack"
)
type Slack struct {
Token string `json:"token"`
Channel string `json:"channel"`
Success MessageOptions `json:"success"`
Failure MessageOptions `json:"failure"`
}
type MessageOptions struct {
Icon string `json:"icon"`
Username string `json:"username"`
Message string `json:"message"`
ImageAttachments []string `json:"image_attachments"`
}
var (
buildDate string
)
func main() {
fmt.Printf("Drone Slack Blame Plugin built at %s\n", buildDate)
repo := plugin.Repo{}
build := plugin.Build{}
system := plugin.System{}
vargs := Slack{}
plugin.Param("build", &build)
plugin.Param("system", &system)
plugin.Param("repo", &repo)
plugin.Param("vargs", &vargs)
// parse the parameters
if err := plugin.Parse(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
// setup the message
buildLink := fmt.Sprintf("%s/%s/%d", system.Link, repo.FullName, build.Number)
var messageOptions MessageOptions
var color string
var messageText string
var channelText string
// Determine if the build was a success
if build.Status == "success" {
messageOptions = vargs.Success
color = "good"
messageText = fmt.Sprintf("Build succeeded at %s", buildLink)
channelText = "Thanks"
} else {
messageOptions = vargs.Failure
color = "danger"
messageText = fmt.Sprintf("Build failed at %s", buildLink)
channelText = "Blame"
}
// set default values
if len(messageOptions.Username) == 0 {
messageOptions.Username = "drone"
}
if len(messageOptions.Icon) == 0 {
messageOptions.Icon = ":drone:"
}
if len(messageOptions.ImageAttachments) == 0 {
messageOptions.ImageAttachments = []string{""}
}
// setup the message
messageParams := slack.PostMessageParameters{
Username: messageOptions.Username,
IconEmoji: messageOptions.Icon,
}
imageCount := len(messageOptions.ImageAttachments)
rand.Seed(time.Now().UTC().UnixNano())
attachment := slack.Attachment{
Color: color,
Text: messageText,
ImageURL: messageOptions.ImageAttachments[rand.Intn(imageCount)],
}
messageParams.Attachments = []slack.Attachment{attachment}
// get the commit author
commitAuthor := build.Email
// create the slack api
api := slack.New(vargs.Token)
// get the users
//
// Slack doesn't let you search by email so just need to get
// everything and find the user in question
var blameUser *slack.User
users, _ := api.GetUsers()
for _, user := range users {
if user.Profile.Email == commitAuthor {
fmt.Printf("%s\n", user.Name)
fmt.Printf("%s\n", user.Profile.Email)
blameUser = &user
break
}
}
// notify the user if possible
var userAt string
if blameUser != nil {
userAt = fmt.Sprintf("@%s", blameUser.Name)
// send the message to the user's channel
//
// this will appear through slackbot
_, _, err := api.PostMessage(userAt, messageOptions.Message, messageParams)
if err == nil {
fmt.Printf("User %s notified\n", userAt)
} else {
fmt.Printf("Could not notify user %s!\n", userAt)
}
} else {
userAt = build.Author
fmt.Print("User could not be found")
}
// notify the channel if requested
if len(vargs.Channel) != 0 {
if !strings.HasPrefix(vargs.Channel, "#") {
vargs.Channel = "#" + vargs.Channel
}
_, _, err := api.PostMessage(vargs.Channel, fmt.Sprintf("%s %s %s", messageOptions.Message, channelText, userAt), messageParams)
if err == nil {
fmt.Printf("Channel notified\n")
} else {
fmt.Printf("Could not notify channel!\n")
}
}
}