-
Notifications
You must be signed in to change notification settings - Fork 786
/
step_create_pr_versions.go
341 lines (301 loc) · 11.7 KB
/
step_create_pr_versions.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
333
334
335
336
337
338
339
340
341
package pr
import (
"path/filepath"
"strings"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/config"
"github.com/jenkins-x/jx/v2/pkg/helm"
"github.com/jenkins-x/jx/v2/pkg/gits"
"github.com/jenkins-x/jx/v2/pkg/versionstream"
"github.com/jenkins-x/jx/v2/pkg/gits/operations"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cloud/gke"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
createVersionPullRequestLong = templates.LongDesc(`
Creates a Pull Request on the versions git repository for a new versionstream of a chart/package
`)
createVersionPullRequestExample = templates.Examples(`
# create a Pull Request to update a chart versionstream
jx step create pr versions -n jenkins-x/prow -v 1.2.3
# create a Pull Request to update a chart versionstream to the latest found in the helm repo
jx step create pr versions -n jenkins-x/prow
# create a Pull Request to update all charts matching a filter to the latest found in the helm repo
jx step create pr versions pr -f "*"
# create a Pull Request to update all charts in the 'jenkins-x' chart repository to the latest found in the helm repo
jx step create pr versions -f "jenkins-x/*"
# create a Pull Request to update all charts in the 'jenkins-x' chart repository and update the BDD test images
jx step create pr versions -f "jenkins-x/*" --images
`)
)
// StepCreatePullRequestVersionsOptions contains the command line flags
type StepCreatePullRequestVersionsOptions struct {
StepCreatePrOptions
Kinds []string
Name string
Includes []string
Excludes []string
UpdateTektonImages bool
}
// NewCmdStepCreatePullRequestVersion Creates a new Command object
func NewCmdStepCreatePullRequestVersion(commonOpts *opts.CommonOptions) *cobra.Command {
options := &StepCreatePullRequestVersionsOptions{
StepCreatePrOptions: StepCreatePrOptions{
StepCreateOptions: step.StepCreateOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
},
},
}
cmd := &cobra.Command{
Use: "versions",
Short: "Creates a Pull Request on the versions git repository for a new versionstream of a chart/package",
Long: createVersionPullRequestLong,
Example: createVersionPullRequestExample,
Aliases: []string{"versionstream pullrequest", "versionstream"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringArrayVarP(&options.Kinds, "kind", "k", []string{"charts", "git"}, "The kinds of versionstream. Possible values: "+strings.Join(versionstream.KindStrings, ", ")+".")
cmd.Flags().StringVarP(&options.Name, "name", "n", "", "The name of the versionstream to update. e.g. the name of the chart like 'jenkins-x/prow'")
cmd.Flags().StringArrayVarP(&options.Includes, "filter", "f", nil, "The name patterns to include - such as '*' for all names")
cmd.Flags().StringArrayVarP(&options.Excludes, "excludes", "x", nil, "The name patterns to exclude")
cmd.Flags().BoolVarP(&options.UpdateTektonImages, "images", "", false, "Update the tekton builder images for the Jenkins X Versions BDD tests")
AddStepCreatePrFlags(cmd, &options.StepCreatePrOptions)
return cmd
}
// ValidateVersionsOptions validates the common options for versionstream pr steps
func (o *StepCreatePullRequestVersionsOptions) ValidateVersionsOptions() error {
if len(o.GitURLs) == 0 {
// Default in the versions repo
o.GitURLs = []string{config.DefaultVersionsURL}
}
if len(o.Kinds) == 0 {
return util.MissingOption("kind")
}
for _, kind := range o.Kinds {
if util.StringArrayIndex(versionstream.KindStrings, kind) < 0 {
return util.InvalidOption("kind", kind, versionstream.KindStrings)
}
}
if o.UpdateTektonImages && o.Name == "" && len(o.Includes) == 0 && util.StringArraysEqual(o.Kinds, []string{"charts", "git"}) {
// Allow for just running jx step create pr versions --images
o.Kinds = make([]string, 0)
} else if len(o.Includes) == 0 && !o.UpdateTektonImages {
if o.Name == "" && !o.UpdateTektonImages {
return util.MissingOption("name")
}
}
return nil
}
// Run implements this command
func (o *StepCreatePullRequestVersionsOptions) Run() error {
if err := o.ValidateVersionsOptions(); err != nil {
return errors.WithStack(err)
}
modifyFns := make([]operations.ChangeFilesFn, 0)
if o.UpdateTektonImages {
builderImageVersion, err := findLatestBuilderImageVersion()
if err != nil {
return errors.WithStack(err)
}
log.Logger().Infof("the latest builder image version is %s\n", util.ColorInfo(builderImageVersion))
pro := operations.PullRequestOperation{
CommonOptions: o.CommonOptions,
GitURLs: o.GitURLs,
SrcGitURL: "https://github.com/jenkins-x/jenkins-x-builders.git",
Base: o.Base,
BranchName: o.BranchName,
Version: builderImageVersion,
DryRun: o.DryRun,
}
authorName, authorEmail, _ := gits.EnsureUserAndEmailSetup(o.Git())
if authorName != "" && authorEmail != "" {
pro.AuthorName = authorName
pro.AuthorEmail = authorEmail
}
fn, err := operations.CreatePullRequestRegexFn(builderImageVersion, "gcr.io/jenkinsxio/builder-(?:maven|go|terraform|go-nodejs):(?P<versions>.+)", "jenkins-x*.yml")
if err != nil {
return errors.WithStack(err)
}
modifyFns = append(modifyFns, pro.WrapChangeFilesWithCommitFn("versions", fn))
modifyFns = append(modifyFns, pro.WrapChangeFilesWithCommitFn("versions", operations.CreatePullRequestBuildersFn(builderImageVersion)))
// Update the pipeline files
fn, err = operations.CreatePullRequestRegexFn(builderImageVersion, `(?m)^\s*agent:\n\s*image: gcr.io/jenkinsxio/builder-.*:(?P<version>.*)$`, "jenkins-x-*.yml")
if err != nil {
return errors.WithStack(err)
}
modifyFns = append(modifyFns, pro.WrapChangeFilesWithCommitFn("versions", fn))
// Machine learning builders have to be handled separately
mlBuilderImageVersion, err := findLatestMLBuilderImageVersion()
if err != nil {
return errors.WithStack(err)
}
log.Logger().Infof("the latest machine learning builder image version is %s\n", util.ColorInfo(mlBuilderImageVersion))
mlPro := operations.PullRequestOperation{
CommonOptions: o.CommonOptions,
GitURLs: o.GitURLs,
SrcGitURL: "https://github.com/jenkins-x/jenkins-x-builders-ml.git",
Base: o.Base,
BranchName: o.BranchName,
Version: mlBuilderImageVersion,
DryRun: o.DryRun,
}
if authorName != "" && authorEmail != "" {
pro.AuthorName = authorName
pro.AuthorEmail = authorEmail
}
modifyFns = append(modifyFns, mlPro.WrapChangeFilesWithCommitFn("versions", operations.CreatePullRequestMLBuildersFn(mlBuilderImageVersion)))
}
if len(o.Includes) > 0 {
for _, kind := range o.Kinds {
modifyFns = append(modifyFns, o.CreatePullRequestUpdateVersionFilesFn(o.Includes, o.Excludes, kind, o.Helm()))
}
} else {
pro := operations.PullRequestOperation{
CommonOptions: o.CommonOptions,
GitURLs: o.GitURLs,
Base: o.Base,
BranchName: o.BranchName,
Version: o.Version,
DryRun: o.DryRun,
}
authorName, authorEmail, _ := gits.EnsureUserAndEmailSetup(o.Git())
if authorName != "" && authorEmail != "" {
pro.AuthorName = authorName
pro.AuthorEmail = authorEmail
}
vaultClient, err := o.SystemVaultClient("")
if err != nil {
vaultClient = nil
}
for _, kind := range o.Kinds {
switch kind {
case string(versionstream.KindChart):
modifyFns = append(modifyFns, pro.WrapChangeFilesWithCommitFn("versions", operations.CreateChartChangeFilesFn(o.Name, o.Version, kind, &pro, o.Helm(), vaultClient, o.GetIOFileHandles())))
}
}
}
o.SrcGitURL = "" // there is no src url for the overall PR
o.SkipCommit = true // As we've done all the commits already
return o.CreatePullRequest("versionstream", func(dir string, gitInfo *gits.GitRepository) ([]string, error) {
for _, kind := range o.Kinds {
if versionstream.VersionKind(kind) == versionstream.KindChart {
_, err := o.HelmInitDependency(dir, o.DefaultReleaseCharts())
if err != nil {
return nil, errors.Wrap(err, "failed to ensure the helm repositories were setup")
}
log.Logger().Info("updating helm repositories to find the latest chart versions...\n")
err = o.Helm().UpdateRepo()
if err != nil {
return nil, errors.Wrap(err, "failed to update helm repos")
}
}
}
for _, fn := range modifyFns {
// in a versions PR there is no overall "old version" - it's done commit by commit
_, err := fn(dir, gitInfo)
if err != nil {
return nil, errors.WithStack(err)
}
}
return nil, nil
})
}
func findLatestBuilderImageVersion() (string, error) {
return findLatestImageVersion("gcr.io/jenkinsxio/builder-maven")
}
func findLatestMLBuilderImageVersion() (string, error) {
return findLatestImageVersion("gcr.io/jenkinsxio/builder-machine-learning")
}
func findLatestImageVersion(image string) (string, error) {
cmd := util.Command{
Name: "gcloud",
Args: []string{
"container", "images", "list-tags", image, "--format", "json",
},
}
output, err := cmd.RunWithoutRetry()
if err != nil {
return "", errors.WithStack(err)
}
return gke.FindLatestImageTag(output)
}
//CreatePullRequestUpdateVersionFilesFn creates the ChangeFilesFn for directory tree of stable version files, applying the includes and excludes
func (o *StepCreatePullRequestVersionsOptions) CreatePullRequestUpdateVersionFilesFn(includes []string, excludes []string, kindStr string, helmer helm.Helmer) operations.ChangeFilesFn {
return func(dir string, gitInfo *gits.GitRepository) (i []string, e error) {
answer := make([]string, 0)
kindDir := filepath.Join(dir, kindStr)
glob := filepath.Join(kindDir, "*", "*.yml")
paths, err := filepath.Glob(glob)
if err != nil {
return nil, errors.Wrapf(err, "bad glob pattern %s", glob)
}
glob = filepath.Join(kindDir, "*", "*", "*.yml")
morePaths, err := filepath.Glob(glob)
if err != nil {
return nil, errors.Wrapf(err, "bad glob pattern %s", glob)
}
paths = append(paths, morePaths...)
for _, path := range paths {
name, err := versionstream.NameFromPath(kindDir, path)
if err != nil {
return nil, errors.WithStack(err)
}
if !util.StringMatchesAny(name, includes, excludes) {
continue
} else {
pro := operations.PullRequestOperation{
CommonOptions: o.CommonOptions,
GitURLs: o.GitURLs,
Base: o.Base,
BranchName: o.BranchName,
DryRun: o.DryRun,
}
authorName, authorEmail, err := gits.EnsureUserAndEmailSetup(o.Git())
if err != nil {
pro.AuthorName = authorName
pro.AuthorEmail = authorEmail
}
var cff operations.ChangeFilesFn
switch kindStr {
case string(versionstream.KindChart):
vaultClient, err := o.SystemVaultClient("")
if err != nil {
vaultClient = nil
}
cff = pro.WrapChangeFilesWithCommitFn(kindStr, operations.CreateChartChangeFilesFn(name, "", kindStr, &pro, o.Helm(), vaultClient, o.GetIOFileHandles()))
case string(versionstream.KindGit):
cff = pro.WrapChangeFilesWithCommitFn(kindStr, pro.CreatePullRequestGitReleasesFn(name))
}
a, err := cff(dir, gitInfo)
if err != nil {
if isFailedToFindLatest(err) {
log.Logger().Warnf("Failed to find latest for %s", util.ColorInfo(name))
} else {
return nil, errors.WithStack(err)
}
}
answer = append(answer, a...)
}
}
return answer, nil
}
}
func isFailedToFindLatest(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), "failed to find latest version for ")
}