-
Notifications
You must be signed in to change notification settings - Fork 10
/
handler_codeship_out.go
138 lines (119 loc) · 3.79 KB
/
handler_codeship_out.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
package codeship
import (
"encoding/json"
"fmt"
cc "github.com/grokify/commonchat"
"github.com/grokify/chathooks/pkg/config"
"github.com/grokify/chathooks/pkg/handlers"
"github.com/grokify/chathooks/pkg/models"
)
const (
DisplayName = "Codeship"
HandlerKey = "codeship"
MessageDirection = "out"
DocumentationURL = "https://documentation.codeship.com/basic/getting-started/webhooks/"
MessageBodyType = models.JSON
)
func NewHandler() handlers.Handler {
return handlers.Handler{MessageBodyType: MessageBodyType, Normalize: Normalize}
}
func Normalize(cfg config.Configuration, hReq handlers.HandlerRequest) (cc.Message, error) {
ccMsg := cc.NewMessage()
iconURL, err := cfg.GetAppIconURL(HandlerKey)
if err == nil {
ccMsg.IconURL = iconURL.String()
}
src, err := CodeshipOutMessageFromBytes(hReq.Body)
if err != nil {
return ccMsg, err
}
build := src.Build
status := build.Status
if status == "infrastructure_failure" {
status = "failed due to infrastructure error"
}
ccMsg.Activity = fmt.Sprintf("Build %v", status)
ccMsg.Title = fmt.Sprintf("[Build #%v](%s) for **%s** %s ([%s](%s))",
build.BuildID,
build.BuildURL,
build.ProjectName,
status,
build.ShortCommitID,
build.CommitURL)
attachment := cc.NewAttachment()
if len(build.Message) > 0 {
if len(build.CommitURL) > 0 {
attachment.AddField(cc.Field{
Title: "Message",
Value: fmt.Sprintf("[%v](%v)", build.Message, build.CommitURL)})
} else {
attachment.AddField(cc.Field{
Title: "Message",
Value: build.Message})
}
}
if len(build.Branch) > 0 {
attachment.AddField(cc.Field{
Title: "Branch",
Value: build.Branch,
Short: true})
}
if len(build.Committer) > 0 {
attachment.AddField(cc.Field{
Title: "Committer",
Value: build.Committer,
Short: true})
}
ccMsg.AddAttachment(attachment)
return ccMsg, nil
}
type CodeshipOutMessage struct {
Build CodeshipOutBuild `json:"build,omitempty"`
}
type CodeshipOutBuild struct {
BuildURL string `json:"build_url,omitempty"`
CommitURL string `json:"commit_url,omitempty"`
ProjectID int64 `json:"project_id,omitempty"`
BuildID int64 `json:"build_id,omitempty"`
Status string `json:"status,omitempty"`
ProjectFullName string `json:"project_full_name,omitempty"`
ProjectName string `json:"project_name,omitempty"`
CommitID string `json:"commit_id,omitempty"`
ShortCommitID string `json:"short_commit_id,omitempty"`
Message string `json:"message,omitempty"`
Committer string `json:"committer,omitempty"`
Branch string `json:"branch,omitempty"`
}
func CodeshipOutMessageFromBytes(bytes []byte) (CodeshipOutMessage, error) {
var msg CodeshipOutMessage
return msg, json.Unmarshal(bytes, &msg)
}
/*
{
"build": {
"build_url":"https://www.codeship.com/projects/10213/builds/973711",
"commit_url":"https://github.com/codeship/docs/
commit/96943dc5269634c211b6fbb18896ecdcbd40a047",
"project_id":10213,
"build_id":973711,
"status":"testing",
# PROJECT_FULL_NAME IS DEPRECATED AND WILL BE REMOVED IN THE FUTURE
"project_full_name":"codeship/docs",
"project_name":"codeship/docs",
"commit_id":"96943dc5269634c211b6fbb18896ecdcbd40a047",
"short_commit_id":"96943",
"message":"Merge pull request #34 from codeship/feature/shallow-clone",
"committer":"beanieboi",
"branch":"master"
}
}
The status field can have one of the following values:
testing for newly started build
error for failed builds
success for passed builds
stopped for stopped builds
waiting for waiting builds
ignored for builds ignored because the account is over the monthly build limit
blocked for builds blocked because of excessive resource consumption
infrastructure_failure for builds which failed because of an internal error on the build VM
*/