-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
215 lines (177 loc) · 5.31 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
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
package main
import (
"bufio"
"bytes"
"embed"
"fmt"
"log"
"os"
"os/exec"
"strings"
"text/template"
"github.com/digitalocean/gta"
yaml "gopkg.in/yaml.v3"
)
const expectedPathParts = 5
//go:embed _template/config.yml
var configTemplate embed.FS
type Metadata struct {
Name string `yaml:"name"`
Staging bool `yaml:"staging"`
Team string `yaml:"team"`
Domain string `yaml:"domain"`
KubescoreEnabled bool `yaml:"kubescoreEnabled"`
CDEnabled bool `yaml:"cdEnabled"`
Services []MetadataService `yaml:"services"`
ArgoAppNamesProduction string `yaml:"argoAppNamesProduction"`
ArgoAppNamesStaging string `yaml:"argoAppNamesStaging"`
Deploy Deploy `yaml:"deploy"`
GoVersion string `yaml:"goVersion,omitempty"`
AlpineVersion string `yaml:"alpineVersion,omitempty"`
TZDataVersion string `yaml:"tzDataVersion,omitempty"`
CaCertVersion string `yaml:"caCertVersion,omitempty"`
DisableWhitesource bool `yaml:"disableWhitesource"`
ChangedServices []MetadataService
}
type Deploy struct {
Platform string `yaml:"platform"`
Product string `yaml:"product"`
}
type MetadataService struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
CIEnabled bool `yaml:"ciEnabled"`
Dockerfile string `yaml:"dockerfile,omitempty"`
}
//nolint:gocritic
func (m Metadata) HasGRPC() bool {
for _, service := range m.Services {
if service.Type == "http-grpc" {
return true
}
}
return false
}
//nolint:gocritic
func (m Metadata) NeedsApproval() bool {
return m.Staging || !m.CDEnabled
}
//nolint:gocritic
func (m Metadata) OverrideGoVersion() string {
out := ""
if m.GoVersion != "" {
out = fmt.Sprintf(" --build-arg=\"GO_VERSION=%s\"", strings.TrimSpace(m.GoVersion))
}
return out
}
//nolint:gocritic
func (m Metadata) OverrideAlpineVersions() string {
out := ""
if m.AlpineVersion != "" {
out = fmt.Sprintf(" --build-arg=\"ALPINE_VERSION=%s\"", strings.TrimSpace(m.AlpineVersion))
}
return out
}
//nolint:gocritic
func (m Metadata) OverrideAlpinePackagesVersions() string {
out := ""
if m.TZDataVersion != "" {
out = fmt.Sprintf(" --build-arg=\"TZDATA_VERSION=%s\"", strings.TrimSpace(m.TZDataVersion))
}
if m.CaCertVersion != "" {
out = fmt.Sprintf(" --build-arg=\"CA_CERTIFICATE_VERSION=%s\"", strings.TrimSpace(m.CaCertVersion))
}
return out
}
//nolint:gocritic
func (m Metadata) ArgOverrides() string {
return strings.TrimSpace(m.OverrideGoVersion() + m.OverrideAlpineVersions() + m.OverrideAlpinePackagesVersions())
}
func (ms MetadataService) NameUnderscored() string {
return strings.ReplaceAll(ms.Name, "-", "_")
}
//nolint:funlen
func main() {
metadataByte, err := os.ReadFile(".metadata.yml")
Fatal(err)
var metadata Metadata
err = yaml.Unmarshal(metadataByte, &metadata)
Fatal(err)
if os.Getenv("CIRCLE_BRANCH") == "" {
log.Fatal("CIRCLE_BRANCH envvar must be set")
}
// Check for inconsistent vendoring
cmdString := "go list -m -f {{.Dir}}"
cmd := exec.Command("bash", "-c", cmdString)
var out strings.Builder
cmd.Stderr = &out
_, err = cmd.Output()
if _, ok := err.(*exec.ExitError); ok {
log.Fatal(out.String())
}
// Compare to last commit on current branch
gitDifferOptions := []gta.GitDifferOption{
gta.SetBaseBranch(os.Getenv("CIRCLE_BRANCH") + "~1"),
}
options := []gta.Option{
gta.SetDiffer(gta.NewGitDiffer(gitDifferOptions...)),
// gta.SetPrefixes(parseStringSlice(*flagInclude)...),
// gta.SetTags(tags...),
}
gt, err := gta.New(options...)
if err != nil {
log.Fatalf("can't prepare gta: %v", err)
}
packages, err := gt.ChangedPackages()
if err != nil {
log.Fatalf("can't list dirty packages: %v", err)
}
for _, changedPackage := range packages.AllChanges {
pathParts := strings.Split(changedPackage.ImportPath, "/")
// branch if there are no changes to specific services ie only vendored dependencies
if len(pathParts) < expectedPathParts {
str := "a service, the go.mod and go.sum or another root level file may have changed"
log.Printf("skipping no specific code changes to %s %s", pathParts, str)
continue
}
if pathParts[1] == os.Getenv("CIRCLE_PROJECT_USERNAME") &&
pathParts[2] == os.Getenv("CIRCLE_PROJECT_REPONAME") &&
pathParts[3] == "cmd" {
for _, service := range metadata.Services {
if service.Name == pathParts[4] {
metadata.ChangedServices = append(metadata.ChangedServices, service)
}
}
}
}
if metadata.GoVersion == "" {
cmdString := "go mod edit -json | jq -r .Go"
out, err := exec.Command("bash", "-c", cmdString).Output()
if err != nil {
Fatal(err)
}
if string(out) == "1.21.3" {
metadata.GoVersion = "1.21"
return
}
metadata.GoVersion = string(out)
}
f, err := os.Create(".circleci/generated-config.yml")
Fatal(err)
byteBuf := bytes.NewBuffer([]byte{})
t, err := template.ParseFS(configTemplate, "_template/config.yml")
Fatal(err)
w := bufio.NewWriter(byteBuf)
err = t.Execute(w, metadata)
Fatal(err)
err = w.Flush()
Fatal(err)
_, err = f.Write(byteBuf.Bytes())
Fatal(err)
fmt.Println(byteBuf.String())
}
func Fatal(err error) {
if err != nil {
log.Fatal(err)
}
}