-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
97 lines (77 loc) · 2.24 KB
/
main.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
package main
import (
"io/ioutil"
"os"
"strings"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
const pluginPrefix = "BUILDKITE_PLUGIN_BUILDPIPE_"
type Config struct {
Projects []Project `yaml:"projects"`
Steps []interface{} `yaml:"steps"`
Env map[string]string `yaml:"env"`
Notify []interface{} `yaml:"notify"`
}
func NewConfig(filename string) *Config {
config := Config{}
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalf("Error reading file %s: %s\n", filename, err)
}
if err = yaml.Unmarshal(yamlFile, &config); err != nil {
log.Fatalf("Error unmarshalling: %s\n", err)
}
return &config
}
func getAffectedProjects(projects []Project, changedFiles []string) []Project {
affectedProjects := make([]Project, 0)
for _, project := range projects {
if project.checkAffected(changedFiles) {
affectedProjects = append(affectedProjects, project)
}
}
return affectedProjects
}
func projectsFromBuildProjects(buildProjects string, projects []Project) []Project {
if buildProjects == "*" {
return projects
}
projectNames := strings.Split(buildProjects, ",")
affectedProjects := make([]Project, 0)
for _, projectName := range projectNames {
for _, configProject := range projects {
if projectName == configProject.Label {
affectedProjects = append(affectedProjects, configProject)
}
}
}
return affectedProjects
}
func main() {
logLevel := getEnv(pluginPrefix+"LOG_LEVEL", "info")
ll, err := log.ParseLevel(logLevel)
if err != nil {
ll = log.InfoLevel
}
log.SetLevel(ll)
config := NewConfig(os.Getenv(pluginPrefix + "DYNAMIC_PIPELINE"))
buildProjects := os.Getenv(pluginPrefix + "BUILD_PROJECTS")
var affectedProjects []Project
if len(buildProjects) > 0 {
affectedProjects = projectsFromBuildProjects(buildProjects, config.Projects)
} else {
changedFiles := getChangedFiles()
if len(changedFiles) == 0 {
log.Info("No files were changed")
os.Exit(0)
}
affectedProjects = getAffectedProjects(config.Projects, changedFiles)
if len(affectedProjects) == 0 {
log.Info("No project was affected from git changes")
os.Exit(0)
}
}
pipeline := generatePipeline(config.Steps, config.Notify, config.Env, affectedProjects)
uploadPipeline(*pipeline)
}