forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hipchat.go
124 lines (109 loc) · 3.36 KB
/
hipchat.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
package notify
import (
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/drone/drone/shared/model"
)
const (
startedMessage = "Building %s (%s) by %s <br> - %s"
successMessage = "Success %s (%s) by %s"
failureMessage = "Failed %s (%s) by %s"
defaultServer = "api.hipchat.com"
)
type Hipchat struct {
Server string `yaml:"server,omitempty"`
Room string `yaml:"room,omitempty"`
Token string `yaml:"token,omitempty"`
Started bool `yaml:"on_started,omitempty"`
Success bool `yaml:"on_success,omitempty"`
Failure bool `yaml:"on_failure,omitempty"`
}
func (h *Hipchat) Send(context *model.Request) error {
client := new(HipchatSimpleHTTPClient)
return h.SendWithClient(client, context)
}
func (h *Hipchat) SendWithClient(client HipchatClient, context *model.Request) error {
switch {
case context.Commit.Status == "Started" && h.Started:
return h.sendStarted(client, context)
case context.Commit.Status == "Success" && h.Success:
return h.sendSuccess(client, context)
case context.Commit.Status == "Failure" && h.Failure:
return h.sendFailure(client, context)
}
return nil
}
func (h *Hipchat) buildLink(context *model.Request) string {
repoName := context.Repo.Owner + "/" + context.Repo.Name
url := context.Host + "/" + context.Repo.Host + "/" + repoName + "/" + context.Commit.Branch + "/" + context.Commit.Sha
return fmt.Sprintf("<a href=\"%s\">%s#%s</a>", url, repoName, context.Commit.ShaShort())
}
func (h *Hipchat) sendStarted(client HipchatClient, context *model.Request) error {
msg := fmt.Sprintf(startedMessage, h.buildLink(context), context.Commit.Branch, context.Commit.Author, context.Commit.Message)
req := HipchatMessageRequest{
Server: h.Server,
RoomId: h.Room,
AuthToken: h.Token,
Color: "yellow",
Message: msg,
Notify: false,
}
return client.PostMessage(req)
}
func (h *Hipchat) sendFailure(client HipchatClient, context *model.Request) error {
msg := fmt.Sprintf(failureMessage, h.buildLink(context), context.Commit.Branch, context.Commit.Author)
req := HipchatMessageRequest{
Server: h.Server,
RoomId: h.Room,
AuthToken: h.Token,
Color: "red",
Message: msg,
Notify: true,
}
return client.PostMessage(req)
}
func (h *Hipchat) sendSuccess(client HipchatClient, context *model.Request) error {
msg := fmt.Sprintf(successMessage, h.buildLink(context), context.Commit.Branch, context.Commit.Author)
req := HipchatMessageRequest{
Server: h.Server,
RoomId: h.Room,
AuthToken: h.Token,
Color: "green",
Message: msg,
Notify: false,
}
return client.PostMessage(req)
}
// HipChat client
type HipchatClient interface {
PostMessage(req HipchatMessageRequest) error
}
type HipchatMessageRequest struct {
Server string
RoomId string
Color string
Message string
Notify bool
AuthToken string
}
type HipchatSimpleHTTPClient struct{}
func (*HipchatSimpleHTTPClient) PostMessage(req HipchatMessageRequest) error {
var server string
if len(req.Server) > 0 {
server = req.Server
} else {
server = defaultServer
}
hipchat_uri := fmt.Sprintf("https://%s/v2/room/%s/notification", server, req.RoomId)
_, err := http.PostForm(hipchat_uri,
url.Values{
"color": {req.Color},
"message": {req.Message},
"notify": {strconv.FormatBool(req.Notify)},
"message_format": {"html"},
"auth_token": {req.AuthToken},
})
return err
}