-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
148 lines (124 loc) · 4.52 KB
/
app.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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/slack-go/slack"
)
// Client represents Slack client
type Client interface {
PostMessageContext(context.Context, string, ...slack.MsgOption) (string, string, error)
UpdateMessageContext(ctx context.Context, channelID, timestamp string, options ...slack.MsgOption) (string, string, string, error)
}
// Slack represents app config
type Slack struct {
Channel string
Context context.Context
Timestamp string
}
// GetTemplate returns default Slack Message Template
func GetTemplate(status string, failed bool, additions []slack.AttachmentField) slack.Attachment {
var color string
var failureColor string = "#fd0000"
if strings.ToLower(status) == "running" || strings.ToLower(status) == "started" || strings.ToLower(status) == "building" || strings.ToLower(status) == "initializing" {
color = "#fbf000"
} else if strings.ToLower(status) == "deploying" || strings.ToLower(status) == "uploading" || strings.ToLower(status) == "publishing" || strings.ToLower(status) == "creating" {
color = "#fda100"
} else if strings.ToLower(status) == "finished" || strings.ToLower(status) == "succeeded" || strings.ToLower(status) == "passed" || strings.ToLower(status) == "built" || strings.ToLower(status) == "released" {
color = "#0ce823"
} else if strings.ToLower(status) == "failed" || strings.ToLower(status) == "aborted" || strings.ToLower(status) == "canceled" || strings.ToLower(status) == "terminated" {
color = failureColor
} else {
color = "#777777"
}
s := status
if failed {
color = failureColor
s = "failed"
}
fields := []slack.AttachmentField{
{
Title: "Repository",
Value: fmt.Sprintf("<https://github.com/%s|%s>", os.Getenv("GITHUB_REPOSITORY"), strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[1]),
Short: true,
},
{
Title: "Workflow",
Value: fmt.Sprintf("<https://github.com/%s/actions?query=workflow", os.Getenv("GITHUB_REPOSITORY")) + "%3A" + fmt.Sprintf("%s|%s>", os.Getenv("GITHUB_WORKFLOW"), os.Getenv("GITHUB_WORKFLOW")),
Short: true,
},
{
Title: "Initiator",
Value: fmt.Sprintf("<https://github.com/%s|%s>", os.Getenv("GITHUB_ACTOR"), os.Getenv("GITHUB_ACTOR")),
Short: true,
},
{
Title: "Status",
Value: fmt.Sprintf("<https://github.com/%s/actions/runs/%s|%s>", os.Getenv("GITHUB_REPOSITORY"), os.Getenv("GITHUB_RUN_ID"), strings.ToUpper(s)),
Short: true,
},
}
if len(additions) > 0 {
fields = append(fields, additions...)
}
msg := slack.Attachment{
Color: color,
Fields: fields,
Footer: "<https://github.com/ReasonSoftware/action-notify-slack|ReasonSoftware/action-notify-slack>",
FooterIcon: "https://cdn.reasonsecurity.com/images/logo.png",
Ts: json.Number(strconv.FormatInt(time.Now().Unix(), 10)),
}
return msg
}
// SendTemplate sends a template message
func (s *Slack) SendTemplate(cli Client, fields []slack.AttachmentField) (string, error) {
failure := false
if os.Getenv("FAIL") != "" {
var err error
failure, err = strconv.ParseBool(os.Getenv("FAIL"))
if err != nil {
return "", errors.Wrap(err, "error parsing env.var 'FAIL'")
}
}
t := GetTemplate(os.Getenv("STATUS"), failure, fields)
return s.send(cli, slack.MsgOptionAttachments(t))
}
// SendAttachmentFromFile sends an attachment provided via json file
func (s *Slack) SendAttachmentFromFile(cli Client, filename string, fields []slack.AttachmentField) (string, error) {
file, err := os.ReadFile(filename)
if err != nil {
return "", errors.Wrap(err, fmt.Sprintf("error reading file '%s'", filename))
}
var slice []slack.Attachment
if err := json.Unmarshal(file, &slice); err != nil {
var single slack.Attachment
if err := json.Unmarshal(file, &single); err != nil {
return "", errors.Wrap(err, fmt.Sprintf("invalid JSON file '%s'", filename))
}
single.Fields = append(single.Fields, fields...)
return s.send(cli, slack.MsgOptionAttachments(single))
}
for _, a := range slice {
a.Fields = append(a.Fields, fields...)
}
return s.send(cli, slack.MsgOptionAttachments(slice...))
}
func (s *Slack) send(cli Client, options ...slack.MsgOption) (string, error) {
if s.Timestamp != "" {
_, ts, _, err := cli.UpdateMessageContext(s.Context, s.Channel, s.Timestamp, options...)
if err != nil {
return "", errors.Wrap(err, "error updating message")
}
return ts, nil
}
_, ts, err := cli.PostMessageContext(s.Context, s.Channel, options...)
if err != nil {
return "", errors.Wrap(err, "error sending message")
}
return ts, nil
}