-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
129 lines (108 loc) · 3 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-tools/go-steputils/stepconf"
)
// Config ...
type Config struct {
Debug bool `env:"is_debug_mode,opt[yes,no]"`
// Message
WebhookURL stepconf.Secret `env:"webhook_url,required"`
AppTitle string `env:"app_title"`
ImageURL string `env:"image_url"`
ImageURLOnError string `env:"image_url_on_error"`
Title string `env:"title"`
TitleOnError string `env:"title_on_error"`
QRCodeURL string `env:"qr_image_url"`
Fields string `env:"fields"`
Buttons string `env:"buttons"`
}
// success is true if the build is successful, false otherwise.
var success = os.Getenv("BITRISE_BUILD_STATUS") == "0"
// selectValue chooses the right value based on the result of the build.
func selectValue(ifSuccess, ifFailed string) string {
if success || ifFailed == "" {
return ifSuccess
}
return ifFailed
}
// ensureNewlines replaces all \n substrings with newline characters.
func ensureNewlines(s string) string {
return strings.Replace(s, "\\n", "\n", -1)
}
func newCard(c Config) Card {
sections := []Section{}
fields := parseFields(c.Fields)
if len(fields) > 0 {
sections = append(sections, Section{Widgets: fields})
}
if len(c.QRCodeURL) > 0 {
widget := Widget{Image: Image{URL: c.QRCodeURL}}
sections = append(sections, Section{Widgets: []Widget{widget}})
}
buttons := parseButtons(c.Buttons)
if len(buttons) > 0 {
sections = append(sections, Section{Widgets: buttons})
}
card := Card{
Header: Header{
Title: selectValue(c.Title, c.TitleOnError),
SubTitle: c.AppTitle,
IconURL: selectValue(c.ImageURL, c.ImageURLOnError),
},
Sections: sections,
}
return card
}
func newMessage(c Config) Message {
msg := Message{
Cards: []Card{newCard(c)},
}
return msg
}
func postMessage(webhookURL string, msg Message) error {
b, err := json.Marshal(msg)
if err != nil {
return err
}
log.Debugf("Request to Google Chat: %s\n", b)
resp, err := http.Post(webhookURL, "application/json", bytes.NewReader(b))
if err != nil {
return fmt.Errorf("failed to send the request: %s", err)
}
defer func() {
if cerr := resp.Body.Close(); err == nil {
err = cerr
}
}()
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("server error: %s, failed to read response: %s", resp.Status, err)
}
return fmt.Errorf("server error: %s, response: %s", resp.Status, body)
}
return nil
}
func main() {
var conf Config
if err := stepconf.Parse(&conf); err != nil {
log.Errorf("Error: %s\n", err)
os.Exit(1)
}
stepconf.Print(conf)
log.SetEnableDebugLog(conf.Debug)
msg := newMessage(conf)
if err := postMessage(string(conf.WebhookURL), msg); err != nil {
log.Errorf("Error: %s", err)
os.Exit(1)
}
log.Donef("\nGoogle Chat message successfully sent! 🚀\n")
}