forked from jones2026/drone-flowdock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (61 loc) · 1.39 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
type flowMessage struct {
Event string `json:"event"`
Content string `json:"content"`
}
type inboxMessage struct {
Event string `json:"event"`
Title string `json:"title"`
}
func postMessage(raw []byte, err error, flowURL string) {
if err != nil {
log.Fatalln(err)
}
resp, err := http.Post(flowURL, "application/json", bytes.NewReader(raw))
if resp != nil {
fmt.Println(resp.Status)
resp.Body.Close()
}
if err != nil {
log.Fatalln(err)
}
}
func main() {
apiURL := "https://api.flowdock.com/messages?flow_token="
flowToken := os.Getenv("PLUGIN_FLOW_TOKEN")
if flowToken == "" {
log.Fatalln("Missing flow token")
}
flowURL := apiURL + flowToken
message := os.Getenv("PLUGIN_MESSAGE")
if message == "" {
repoName := os.Getenv("DRONE_REPO")
buildLink := os.Getenv("DRONE_BUILD_LINK")
buildStatus := os.Getenv("DRONE_BUILD_STATUS")
message = fmt.Sprintf("Status of build [%s](%s) is %s", repoName, buildLink, buildStatus)
}
messageType := os.Getenv("PLUGIN_MESSAGE_TYPE")
if messageType == "activity" {
msg := inboxMessage {
Event: "activity",
Title: message,
}
raw, err := json.Marshal(msg)
postMessage(raw, err, flowURL)
} else {
msg := flowMessage {
Event: "message",
Content: message,
}
raw, err := json.Marshal(msg)
postMessage(raw, err, flowURL)
}
}