generated from actions/container-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (103 loc) · 2.8 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/pkg/errors"
"github.com/senzing-factory/github-action-slack-notification/configuration"
)
type Message struct {
Username string `json:"username,omitempty"`
IconURL string `json:"icon_url,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
Channel string `json:"channel,omitempty"`
Text string `json:"text,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
}
type Attachment struct {
AuthorName string `json:"author_name,omitempty"`
AuthorLink string `json:"author_link,omitempty"`
AuthorIconURL string `json:"author_icon,omitempty"`
Color string `json:"color,omitempty"`
Title string `json:"title,omitempty"`
Fields []Field `json:"fields,omitempty"`
}
type Field struct {
Title string `json:"title,omitempty"`
Value string `json:"value,omitempty"`
Short bool `json:"short,omitempty"`
}
func (message *Message) Send(webhook string) error {
var err error
msg, err := json.Marshal(message)
if err != nil {
return err
}
msgBytes := bytes.NewBuffer(msg)
request, err := http.NewRequest(http.MethodPost, webhook, msgBytes)
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/json")
response, err := http.DefaultClient.Do(request)
if err != nil {
return err
}
if response.StatusCode >= 299 {
formatted := fmt.Sprintf("Exception: %s", response.Status)
err = fmt.Errorf("%s", formatted)
}
fmt.Println(response.Status)
return err
}
func main() {
var err error
var config *configuration.Config
var slackMessage *Message
// Init configuration from environment variables
config = new(configuration.Config)
err = config.Init()
if err != nil {
log.Fatalf("Exception: %v", err)
}
slackMessage = new(Message)
slackMessage.Username = config.SlackUsername
slackMessage.IconURL = config.SlackIconURL
slackMessage.Channel = config.SlackChannel
slackMessage.Attachments = []Attachment{
{
AuthorName: config.GithubActor,
AuthorLink: "http://github.com/" + config.GithubActor,
AuthorIconURL: "http://github.com/" + config.GithubActor + ".png?size=32",
Color: config.SlackColor,
Title: config.GithubEventName,
Fields: []Field{
{
Title: "Ref",
Value: config.GithubRef,
Short: true,
}, {
Title: "Event",
Value: config.GithubEventName,
Short: true,
},
{
Title: "Repo Action URL",
Value: "https://github.com/" + config.GithubRepository + "/actions",
Short: false,
},
{
Title: config.SlackTitle,
Value: config.SlackMessage,
Short: false,
},
},
},
}
err = slackMessage.Send(config.SlackWebhook)
if err != nil {
log.Printf("%+v", errors.Wrap(err, "Exception"))
}
}