-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.go
executable file
·50 lines (46 loc) · 1.11 KB
/
notify.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
package slackbot
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
cloudbuild "google.golang.org/api/cloudbuild/v1"
)
// Notify posts a notification to Slack that the build is complete.
func Notify(b *cloudbuild.Build, webhook string) {
url := fmt.Sprintf("https://console.cloud.google.com/cloud-build/builds/%s", b.Id)
var i string
switch b.Status {
case "SUCCESS":
i = ":white_check_mark:"
case "FAILURE", "CANCELLED":
i = ":x:"
case "STATUS_UNKNOWN", "INTERNAL_ERROR":
i = ":interrobang:"
default:
i = ":question:"
}
j := fmt.Sprintf(
`{"text": "Cloud Build %s complete: %s %s",
"attachments": [
{
"fallback": "Open build details at %s",
"actions": [
{
"type": "button",
"text": "Open details",
"url": "%s"
}
]
}
]}`, b.Id, i, b.Status, url, url)
r := strings.NewReader(j)
resp, err := http.Post(webhook, "application/json", r)
if err != nil {
log.Fatalf("Failed to post to Slack: %v", err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
log.Printf("Posted message to Slack: [%v], got response [%s]", j, body)
}