forked from adikari/monorepo-diff-buildkite-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
191 lines (152 loc) · 4.4 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
package main
import (
"encoding/json"
"errors"
"strings"
log "github.com/sirupsen/logrus"
)
const pluginName = "github.com/chronotc/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
}
// 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"`
}
// Step is buildkite pipeline definition
type Step struct {
Trigger string `yaml:"trigger,omitempty"`
Label string `yaml:"label,omitempty"`
Build Build `yaml:"build,omitempty"`
Command string `yaml:"command,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"`
}
// Agent is Buildkite agent definition
type Agent struct {
Queue string `yaml:"queue,omitempty"`
}
// 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"`
}
func initializePlugin(data string) (Plugin, error) {
var plugins []map[string]Plugin
err := json.Unmarshal([]byte(data), &plugins)
if err != nil {
log.Debug(err)
return Plugin{}, errors.New("failed to parse plugin configuration")
}
for _, p := range plugins {
for key, plugin := range p {
if strings.HasPrefix(key, pluginName) {
return plugin, nil
}
}
}
return Plugin{}, errors.New("could not initialize plugin")
}
// 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)
plugin.Env = parseEnv(plugin.RawEnv)
plugin.RawEnv = nil
// 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)
}
appendEnv(&plugin.Watch[i], plugin.Env)
p.RawPath = nil
}
return 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.Env == nil {
watch.Step.Env = make(map[string]string)
}
watch.Step.Env[key] = value
if watch.Step.Trigger == "" {
continue
}
if watch.Step.Build.Env == nil {
watch.Step.Build.Env = make(map[string]string)
}
watch.Step.Build.Env[key] = value
}
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 {
if raw == nil {
return nil
}
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
}