This repository has been archived by the owner on Oct 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnotify.go
145 lines (119 loc) · 4.31 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package grim
import (
"bytes"
"errors"
"fmt"
"log"
"text/template"
"time"
"github.com/google/go-github/github"
)
// 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 localConfig) (string, string, error)
}
type standardGrimNotification struct {
githubState refStatusState
getHipchatColor func(localConfig) string
getTemplate func(localConfig) string
}
//GrimPending is the notification used for pending builds.
var GrimPending = &standardGrimNotification{
RSPending,
func(c localConfig) string { return c.pendingColor() },
func(c localConfig) string { return c.pendingTemplate() },
}
//GrimError is the notification used for builds that cannot be run correctly.
var GrimError = &standardGrimNotification{
RSError,
func(c localConfig) string { return c.errorColor() },
func(c localConfig) string { return c.errorTemplate() },
}
//GrimFailure is the notification used when builds fail.
var GrimFailure = &standardGrimNotification{
RSFailure,
func(c localConfig) string { return c.failureColor() },
func(c localConfig) string { return c.failureTemplate() },
}
//GrimSuccess is the notification used when builds succeed.
var GrimSuccess = &standardGrimNotification{
RSSuccess,
func(c localConfig) string { return c.successColor() },
func(c localConfig) string { return c.successTemplate() },
}
func (s *standardGrimNotification) GithubRefStatus() refStatusState {
return s.githubState
}
func (s *standardGrimNotification) HipchatNotification(context *grimNotificationContext, config localConfig) (string, string, error) {
message, err := context.render(s.getTemplate(config))
return message, s.getHipchatColor(config), 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 localConfig, hook hookEvent, ws, logDir string, notification grimNotification, logger *log.Logger) error {
if hook.EventName != "push" && hook.EventName != "pull_request" {
return nil
}
repoStatus := createGithubRepoStatus(config.grimServerID(), notification.GithubRefStatus(), logDir)
ghErr := setRefStatus(config.gitHubToken(), hook.Owner, hook.Repo, hook.StatusRef, repoStatus)
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
}
switch config.hipChatVersion() {
case 1:
err = sendMessageToRoom(config.hipChatToken(), config.hipChatRoom(), config.grimServerID(), message, color)
case 2:
err = sendMessageToRoom2(config.hipChatToken(), config.hipChatRoom(), config.grimServerID(), message, color)
default:
err = errors.New("invalid or unsupported hipchat version")
}
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
}
func createGithubRepoStatus(serverID string, state refStatusState, logDir string) *github.RepoStatus {
stateStr := string(state)
description := fmt.Sprintf("%v - %v", logDir, time.Now().Format(time.RFC822))
if len(description) >= 1024 {
description = fmt.Sprintf("...%v", description[10:])
}
return &github.RepoStatus{
State: &stateStr,
Description: &description,
Context: &serverID,
}
}