forked from adikari/monorepo-diff-buildkite-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
171 lines (137 loc) · 3.59 KB
/
pipeline.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
package main
import (
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
"github.com/bmatcuk/doublestar/v2"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
// Pipeline is Buildkite pipeline definition
type Pipeline struct {
Steps []Step
}
// PipelineGenerator generates pipeline file
type PipelineGenerator func(steps []Step, plugin Plugin) (*os.File, error)
func uploadPipeline(plugin Plugin, generatePipeline PipelineGenerator) (string, []string, error) {
diffOutput, err := diff(plugin.Diff)
if err != nil {
log.Fatal(err)
return "", []string{}, err
}
if len(diffOutput) < 1 {
log.Info("No changes detected. Skipping pipeline upload.")
return "", []string{}, nil
}
log.Debug("Output from diff: \n" + strings.Join(diffOutput, "\n"))
steps, err := stepsToTrigger(diffOutput, plugin.Watch)
if err != nil {
return "", []string{}, err
}
pipeline, err := generatePipeline(steps, plugin)
defer os.Remove(pipeline.Name())
if err != nil {
log.Error(err)
return "", []string{}, err
}
cmd := "buildkite-agent"
args := []string{"pipeline", "upload", pipeline.Name()}
if !plugin.Interpolation {
args = append(args, "--no-interpolation")
}
_, err = executeCommand("buildkite-agent", args)
return cmd, args, err
}
func diff(command string) ([]string, error) {
log.Infof("Running diff command: %s", command)
split := strings.Split(command, " ")
cmd, args := split[0], split[1:]
output, err := executeCommand(cmd, args)
if err != nil {
return nil, fmt.Errorf("diff command failed: %v", err)
}
f := func(c rune) bool {
return c == '\n'
}
return strings.FieldsFunc(strings.TrimSpace(output), f), nil
}
func stepsToTrigger(files []string, watch []WatchConfig) ([]Step, error) {
steps := []Step{}
for _, w := range watch {
for _, p := range w.Paths {
for _, f := range files {
match, err := matchPath(p, f)
if err != nil {
return nil, err
}
if match {
steps = append(steps, w.Step)
break
}
}
}
}
return dedupSteps(steps), nil
}
// matchPath checks if the file f matches the path p.
func matchPath(p string, f string) (bool, error) {
// If the path contains a glob, the `doublestar.Match`
// method is used to determine the match,
// otherwise `strings.HasPrefix` is used.
if strings.Contains(p, "*") {
match, err := doublestar.Match(p, f)
if err != nil {
return false, fmt.Errorf("path matching failed: %v", err)
}
if match {
return true, nil
}
}
if strings.HasPrefix(f, p) {
return true, nil
}
return false, nil
}
func dedupSteps(steps []Step) []Step {
unique := []Step{}
for _, p := range steps {
duplicate := false
for _, t := range unique {
if reflect.DeepEqual(p, t) {
duplicate = true
break
}
}
if !duplicate {
unique = append(unique, p)
}
}
return unique
}
func generatePipeline(steps []Step, plugin Plugin) (*os.File, error) {
tmp, err := ioutil.TempFile(os.TempDir(), "bmrd-")
pipeline := Pipeline{Steps: steps}
if err != nil {
return nil, fmt.Errorf("could not create temporary pipeline file: %v", err)
}
data, err := yaml.Marshal(&pipeline)
if err != nil {
return nil, fmt.Errorf("could not serialize the pipeline: %v", err)
}
if plugin.Wait {
data = append(data, "- wait"...)
}
for _, cmd := range plugin.Hooks {
data = append(data, "\n- command: "+cmd.Command...)
}
// Disable logging in context of go tests.
if env("TEST_MODE", "") != "true" {
fmt.Printf("Generated Pipeline:\n%s\n", string(data))
}
if err = ioutil.WriteFile(tmp.Name(), data, 0644); err != nil {
return nil, fmt.Errorf("could not write step to temporary file: %v", err)
}
return tmp, nil
}