This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathplugin.go
332 lines (270 loc) · 8.99 KB
/
plugin.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"encoding/json"
"errors"
"strings"
log "github.com/sirupsen/logrus"
)
const pluginName = "github.com/monebag/monorepo-diff"
// Plugin buildkite monorepo diff plugin structure
type Plugin struct {
Diff string
Wait bool
LogLevel string `json:"log_level"`
Interpolation bool
Hooks []HookConfig
Watch []WatchConfig
RawEnv interface{} `json:"env"`
Env map[string]string
RawNotify []map[string]interface{} `json:"notify" yaml:",omitempty"`
Notify []PluginNotify `yaml:"notify,omitempty"`
}
// HookConfig Plugin hook configuration
type HookConfig struct {
Command string
}
// WatchConfig Plugin watch configuration
type WatchConfig struct {
RawPath interface{} `json:"path"`
Paths []string
Step Step `json:"config"`
}
type Group struct {
Label string `yaml:"group"`
Steps []Step `yaml:"steps"`
}
// GithubStatusNotification is notification config for github_commit_status
type GithubStatusNotification struct {
Context string `yaml:"context,omitempty"`
}
// PluginNotify is notify configuration for pipeline
type PluginNotify struct {
Slack string `yaml:"slack,omitempty"`
Email string `yaml:"email,omitempty"`
PagerDuty string `yaml:"pagerduty_change_event,omitempty"`
Webhook string `yaml:"webhook,omitempty"`
Basecamp string `yaml:"basecamp_campfire,omitempty"`
GithubStatus GithubStatusNotification `yaml:"github_commit_status,omitempty"`
Condition string `yaml:"if,omitempty"`
}
// Notify is Buildkite notification definition
type StepNotify struct {
Slack string `yaml:"slack,omitempty"`
Basecamp string `yaml:"basecamp_campfire,omitempty"`
GithubStatus GithubStatusNotification `yaml:"github_commit_status,omitempty"`
Condition string `yaml:"if,omitempty"`
}
// Step is buildkite pipeline definition
type Step struct {
Group string `yaml:"group,omitempty"`
Trigger string `yaml:"trigger,omitempty"`
Label string `yaml:"label,omitempty"`
Build Build `yaml:"build,omitempty"`
Command interface{} `yaml:"command,omitempty"`
Commands interface{} `yaml:"commands,omitempty"`
Agents Agent `yaml:"agents,omitempty"`
Artifacts []string `yaml:"artifacts,omitempty"`
RawEnv interface{} `json:"env" yaml:",omitempty"`
Env map[string]string `yaml:"env,omitempty"`
Async bool `yaml:"async,omitempty"`
SoftFail interface{} `json:"soft_fail" yaml:"soft_fail,omitempty"`
RawNotify []map[string]interface{} `json:"notify" yaml:",omitempty"`
Notify []StepNotify `yaml:"notify,omitempty"`
}
// Agent is Buildkite agent definition
type Agent map[string]string
// Build is buildkite build definition
type Build struct {
Message string `yaml:"message,omitempty"`
Branch string `yaml:"branch,omitempty"`
Commit string `yaml:"commit,omitempty"`
RawEnv interface{} `json:"env" yaml:",omitempty"`
Env map[string]string `yaml:"env,omitempty"`
MetaData map[string]string `json:"meta_data,omitempty" yaml:"meta_data,omitempty"`
// Notify []Notify `yaml:"notify,omitempty"`
}
// UnmarshalJSON set defaults properties
func (plugin *Plugin) UnmarshalJSON(data []byte) error {
type plain Plugin
def := &plain{
Diff: "git diff --name-only HEAD~1",
Wait: false,
LogLevel: "info",
Interpolation: true,
}
_ = json.Unmarshal(data, def)
*plugin = Plugin(*def)
parseResult, err := parseEnv(plugin.RawEnv)
if err != nil {
return errors.New("failed to parse plugin configuration")
}
plugin.Env = parseResult
plugin.RawEnv = nil
setPluginNotify(&plugin.Notify, &plugin.RawNotify)
// Path can be string or an array of strings,
// handle both cases and create an array of paths.
for i, p := range plugin.Watch {
switch p.RawPath.(type) {
case string:
plugin.Watch[i].Paths = []string{plugin.Watch[i].RawPath.(string)}
case []interface{}:
for _, v := range plugin.Watch[i].RawPath.([]interface{}) {
plugin.Watch[i].Paths = append(plugin.Watch[i].Paths, v.(string))
}
}
if plugin.Watch[i].Step.Trigger != "" {
setBuild(&plugin.Watch[i].Step.Build)
}
if plugin.Watch[i].Step.RawNotify != nil {
setNotify(&plugin.Watch[i].Step.Notify, &plugin.Watch[i].Step.RawNotify)
}
appendEnv(&plugin.Watch[i], plugin.Env)
p.RawPath = nil
}
return nil
}
func initializePlugin(data string) (Plugin, error) {
log.Debugf("parsing plugin config: %v", data)
var pluginConfigs []map[string]json.RawMessage
if err := json.Unmarshal([]byte(data), &pluginConfigs); err != nil {
log.Debug(err)
return Plugin{}, errors.New("failed to parse plugin configuration")
}
for _, p := range pluginConfigs {
for key, pluginConfig := range p {
if strings.HasPrefix(key, pluginName) {
var plugin Plugin
if err := json.Unmarshal(pluginConfig, &plugin); err != nil {
log.Debug(err)
return Plugin{}, errors.New("failed to parse plugin configuration")
}
return plugin, nil
}
}
}
return Plugin{}, errors.New("could not initialize plugin")
}
func setPluginNotify(notifications *[]PluginNotify, rawNotify *[]map[string]interface{}) {
for _, v := range *rawNotify {
var notify PluginNotify
if condition, ok := isString(v["if"]); ok {
notify.Condition = condition
}
if email, ok := isString(v["email"]); ok {
notify.Email = email
*notifications = append(*notifications, notify)
continue
}
if basecamp, ok := isString(v["basecamp_campfire"]); ok {
notify.Basecamp = basecamp
*notifications = append(*notifications, notify)
continue
}
if webhook, ok := isString(v["webhook"]); ok {
notify.Webhook = webhook
*notifications = append(*notifications, notify)
continue
}
if pagerduty, ok := isString(v["pagerduty_change_event"]); ok {
notify.PagerDuty = pagerduty
*notifications = append(*notifications, notify)
continue
}
if slack, ok := isString(v["slack"]); ok {
notify.Slack = slack
*notifications = append(*notifications, notify)
continue
}
if github, ok := v["github_commit_status"].(map[string]interface{}); ok {
if context, ok := isString(github["context"]); ok {
notify.GithubStatus = GithubStatusNotification{Context: context}
*notifications = append(*notifications, notify)
}
continue
}
}
*rawNotify = nil
}
func setNotify(notifications *[]StepNotify, rawNotify *[]map[string]interface{}) {
for _, v := range *rawNotify {
var notify StepNotify
if condition, ok := isString(v["if"]); ok {
notify.Condition = condition
}
if basecamp, ok := isString(v["basecamp_campfire"]); ok {
notify.Basecamp = basecamp
*notifications = append(*notifications, notify)
continue
}
if slack, ok := isString(v["slack"]); ok {
notify.Slack = slack
*notifications = append(*notifications, notify)
continue
}
if github, ok := v["github_commit_status"].(map[string]interface{}); ok {
if context, ok := isString(github["context"]); ok {
notify.GithubStatus = GithubStatusNotification{Context: context}
*notifications = append(*notifications, notify)
}
continue
}
}
*rawNotify = nil
}
func setBuild(build *Build) {
if build.Message == "" {
build.Message = env("BUILDKITE_MESSAGE", "")
}
if build.Branch == "" {
build.Branch = env("BUILDKITE_BRANCH", "")
}
if build.Commit == "" {
build.Commit = env("BUILDKITE_COMMIT", "")
}
}
// appends top level env to Step.Env and Step.Build.Env
func appendEnv(watch *WatchConfig, env map[string]string) {
watch.Step.Env, _ = parseEnv(watch.Step.RawEnv)
watch.Step.Build.Env, _ = parseEnv(watch.Step.Build.RawEnv)
for key, value := range env {
if watch.Step.Command != nil || watch.Step.Commands != nil {
if watch.Step.Env == nil {
watch.Step.Env = make(map[string]string)
}
watch.Step.Env[key] = value
continue
}
if watch.Step.Trigger != "" {
if watch.Step.Build.Env == nil {
watch.Step.Build.Env = make(map[string]string)
}
watch.Step.Build.Env[key] = value
continue
}
}
watch.Step.RawEnv = nil
watch.Step.Build.RawEnv = nil
watch.RawPath = nil
}
// parse env in format from env=env-value to map[env] = env-value
func parseEnv(raw interface{}) (map[string]string, error) {
if raw == nil {
return nil, nil
}
if _, ok := raw.([]interface{}); ok != true {
return nil, errors.New("failed to parse plugin configuration")
}
result := make(map[string]string)
for _, v := range raw.([]interface{}) {
split := strings.Split(v.(string), "=")
key, value := strings.TrimSpace(split[0]), split[1:]
// only key exists. set value from env
if len(key) > 0 && len(value) == 0 {
result[key] = env(key, "")
}
if len(value) > 0 {
result[key] = strings.TrimSpace(value[0])
}
}
return result, nil
}