-
Notifications
You must be signed in to change notification settings - Fork 2
/
sender.go
64 lines (52 loc) · 1.26 KB
/
sender.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
package main
import (
"fmt"
"log"
"strings"
"github.com/mchmarny/gcputil/env"
"github.com/nlopes/slack"
)
var (
sendStatus = []string{"WORKING", "SUCCESS", "FAILURE", "INTERNAL_ERROR", "TIMEOUT"}
token = env.MustGetEnvVar("SLACK_API_TOKEN", "")
channel = env.MustGetEnvVar("SLACK_BUILD_STATUS_CHANNEL", "")
)
func send(msg *CloudBuildNotification) error {
if msg == nil {
return fmt.Errorf("Null message on send: %v", msg)
}
// check if status is to be sent
if !isStatusForSend(msg.Status) {
log.Printf("Status not for send: %s != [%s]", msg.Status, strings.Join(sendStatus, ","))
return nil
}
api := slack.New(token)
a1 := slack.Attachment{
Title: "Trigger: ",
TitleLink: msg.LogURL,
}
a1.Fields = []slack.AttachmentField{
slack.AttachmentField{
Title: "Tag",
Value: fmt.Sprintf("Git repo *%s* was tagged: *%s*",
msg.Source.RepoSource.RepoName,
msg.Source.RepoSource.TagName),
},
slack.AttachmentField{
Title: "Status",
Value: msg.Status,
},
}
_, _, err := api.PostMessage(channel,
slack.MsgOptionText("Cloud Build Status", false),
slack.MsgOptionAttachments(a1))
return err
}
func isStatusForSend(a string) bool {
for _, b := range sendStatus {
if b == a {
return true
}
}
return false
}