forked from MediaMath/grim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.go
101 lines (80 loc) · 3.35 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
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
package grim
import (
"bytes"
"fmt"
"log"
"text/template"
)
// Copyright 2015 MediaMath <http://www.mediamath.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
type grimNotification interface {
GithubRefStatus() refStatusState
HipchatNotification(context *grimNotificationContext, config *effectiveConfig) (string, messageColor, error)
}
type standardGrimNotification struct {
githubState refStatusState
hipchatColor messageColor
getTemplate func(*effectiveConfig) string
}
//GrimPending is the notification used for pending builds.
var GrimPending = &standardGrimNotification{RSPending, ColorYellow, func(c *effectiveConfig) string { return c.pendingTemplate }}
//GrimError is the notification used for builds that cannot be run correctly.
var GrimError = &standardGrimNotification{RSError, ColorGray, func(c *effectiveConfig) string { return c.errorTemplate }}
//GrimFailure is the notification used when builds fail.
var GrimFailure = &standardGrimNotification{RSFailure, ColorRed, func(c *effectiveConfig) string { return c.failureTemplate }}
//GrimSuccess is the notification used when builds succeed.
var GrimSuccess = &standardGrimNotification{RSSuccess, ColorGreen, func(c *effectiveConfig) string { return c.successTemplate }}
func (s *standardGrimNotification) GithubRefStatus() refStatusState {
return s.githubState
}
func (s *standardGrimNotification) HipchatNotification(context *grimNotificationContext, config *effectiveConfig) (string, messageColor, error) {
message, err := context.render(s.getTemplate(config))
return message, s.hipchatColor, err
}
type grimNotificationContext struct {
Owner string
Repo string
EventName string
Target string
UserName string
Workspace string
LogDir string
}
func (c *grimNotificationContext) render(templateString string) (string, error) {
template, tempErr := template.New("msg").Parse(templateString)
if tempErr != nil {
return "", fmt.Errorf("Error parsing notification template: %v", tempErr)
}
var doc bytes.Buffer
if tempErr = template.Execute(&doc, c); tempErr != nil {
return "", fmt.Errorf("Error applying template: %v", tempErr)
}
return doc.String(), nil
}
func buildContext(hook hookEvent, ws, logDir string) *grimNotificationContext {
return &grimNotificationContext{hook.Owner, hook.Repo, hook.EventName, hook.Target, hook.UserName, ws, logDir}
}
func notify(config *effectiveConfig, hook hookEvent, ws, logDir string, notification grimNotification, logger *log.Logger) error {
if hook.EventName != "push" && hook.EventName != "pull_request" {
return nil
}
ghErr := setRefStatus(config.gitHubToken, hook.Owner, hook.Repo, hook.StatusRef, notification.GithubRefStatus(), "", "")
context := buildContext(hook, ws, logDir)
message, color, err := notification.HipchatNotification(context, config)
logger.Print(message)
if config.hipChatToken != "" && config.hipChatRoom != "" {
if err != nil {
logger.Printf("Hipchat: Error while rendering message: %v", err)
return err
}
err = sendMessageToRoom(config.hipChatToken, config.hipChatRoom, config.grimServerID, message, color)
if err != nil {
logger.Printf("Hipchat: Error while sending message to room: %v", err)
return err
}
} else {
logger.Print("HipChat: config.hipChatToken and config.hitChatRoom not set")
}
return ghErr
}