-
Notifications
You must be signed in to change notification settings - Fork 787
/
step_create_version_pr.go
285 lines (250 loc) · 9.29 KB
/
step_create_version_pr.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
package cmd
import (
"fmt"
"github.com/jenkins-x/jx/pkg/cloud/gke"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/jenkins-x/jx/pkg/version"
"github.com/pkg/errors"
"github.com/spf13/cobra"
pipelineapi "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
)
var (
createVersionPullRequestLong = templates.LongDesc(`
Creates a Pull Request on the versions git repository for a new version of a chart/package
`)
createVersionPullRequestExample = templates.Examples(`
# create a Pull Request to update a chart version
jx step create version pr -n jenkins-x/prow -v 1.2.3
# create a Pull Request to update a chart version to the latest found in the helm repo
jx step create version pr -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 version 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 version pr -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 version pr -f "jenkins-x/*" --images
`)
)
// StepCreateVersionPullRequestOptions contains the command line flags
type StepCreateVersionPullRequestOptions struct {
StepOptions
Kind string
Name string
Includes []string
Excludes []string
Version string
UpdateTektonImages bool
updatedHelmRepo bool
builderImageVersion string
PullRequestDetails PullRequestDetails
}
// StepCreateVersionPullRequestResults stores the generated results
type StepCreateVersionPullRequestResults struct {
Pipeline *pipelineapi.Pipeline
Task *pipelineapi.Task
PipelineRun *pipelineapi.PipelineRun
}
// NewCmdStepCreateVersionPullRequest Creates a new Command object
func NewCmdStepCreateVersionPullRequest(commonOpts *CommonOptions) *cobra.Command {
options := &StepCreateVersionPullRequestOptions{
StepOptions: StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "version pr",
Short: "Creates a Pull Request on the versions git repository for a new version of a chart/package",
Long: createVersionPullRequestLong,
Example: createVersionPullRequestExample,
Aliases: []string{"version pullrequest"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.PullRequestDetails.RepositoryGitURL, "repo", "r", DefaultVersionsURL, "Jenkins X versions Git repo")
cmd.Flags().StringVarP(&options.PullRequestDetails.RepositoryBranch, "branch", "", "master", "the versions git repository branch to clone and generate a pull request from")
cmd.Flags().StringVarP(&options.Kind, "kind", "k", "charts", "The kind of version. Possible values: "+strings.Join(version.KindStrings, ", "))
cmd.Flags().StringVarP(&options.Name, "name", "n", "", "The name of the version to update. e.g. the name of the chart like 'jenkins-x/prow'")
cmd.Flags().StringVarP(&options.Version, "version", "v", "", "The version to change. If no version is supplied the latest version is found")
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")
return cmd
}
// Run implements this command
func (o *StepCreateVersionPullRequestOptions) Run() error {
if o.Kind == "" {
return util.MissingOption("kind")
}
if util.StringArrayIndex(version.KindStrings, o.Kind) < 0 {
return util.InvalidOption("kind", o.Kind, version.KindStrings)
}
opts := &o.PullRequestDetails
if opts.RepositoryGitURL == "" {
return util.MissingOption("repo")
}
dir, err := ioutil.TempDir("", "create-version-pr")
if err != nil {
return err
}
if o.UpdateTektonImages {
o.builderImageVersion, err = o.findLatestBuilderImageVersion()
if err != nil {
return err
}
log.Infof("the latest builder image version is %s\n", util.ColorInfo(o.builderImageVersion))
}
if len(o.Includes) == 0 {
if o.Name == "" {
return util.MissingOption("name")
}
if o.Version == "" && o.Kind == string(version.KindChart) {
o.Version, err = o.findLatestChartVersion(o.Name)
if err != nil {
return errors.Wrapf(err, "failed to find latest chart version for %s", o.Name)
}
log.Infof("found latest version %s for chart %s\n", util.ColorInfo(o.Version), util.ColorInfo(o.Name))
}
if o.Version == "" {
return util.MissingOption("version")
}
o.PullRequestDetails.BranchNameText = strings.Replace("upgrade-"+o.Name+"-"+o.Version, "/", "-", -1)
o.PullRequestDetails.BranchNameText = strings.Replace(o.PullRequestDetails.BranchNameText, ".", "-", -1)
o.PullRequestDetails.Title = fmt.Sprintf("%s version upgrade of %s", o.Kind, o.Name)
o.PullRequestDetails.Message = fmt.Sprintf("change %s to version %s", o.Name, o.Version)
} else {
o.PullRequestDetails.BranchNameText = "upgrade-chart-versions"
o.PullRequestDetails.Title = "upgrade chart versions"
}
opts.Dir = dir
opts.RepositoryMessage = "versions repository"
fn := func() error {
return o.modifyFiles(dir)
}
return o.CreatePullRequest(&o.PullRequestDetails, fn)
}
func (o *StepCreateVersionPullRequestOptions) modifyFiles(dir string) error {
if o.builderImageVersion != "" {
err := o.modifyRegex(filepath.Join(dir, "jenkins-x-*.yml"), "gcr.io/jenkinsxio/builder-go-maven:(.+)", "gcr.io/jenkinsxio/builder-go-maven:"+o.builderImageVersion)
if err != nil {
return err
}
}
if len(o.Includes) > 0 {
switch version.VersionKind(o.Kind) {
case version.KindChart:
return o.findLatestChartVersions(dir)
default:
return fmt.Errorf("We do not yet support finding the latest version of kind %s", o.Kind)
}
}
kind := version.VersionKind(o.Kind)
data, err := version.LoadStableVersion(dir, kind, o.Name)
if err != nil {
return err
}
if data.Version == o.Version {
return nil
}
data.Version = o.Version
err = version.SaveStableVersion(dir, kind, o.Name, data)
if err != nil {
return errors.Wrapf(err, "failed to save version file")
}
return nil
}
func (o *StepCreateVersionPullRequestOptions) findLatestChartVersions(dir string) error {
callback := func(kind version.VersionKind, name string, stableVersion *version.StableVersion) (bool, error) {
if !util.StringMatchesAny(name, o.Includes, o.Excludes) {
return true, nil
}
v, err := o.findLatestChartVersion(name)
if err != nil {
log.Warnf("failed to find latest version of %s: %s\n", name, err.Error())
return true, nil
}
if v != stableVersion.Version {
stableVersion.Version = v
err = version.SaveStableVersion(dir, kind, name, stableVersion)
if err != nil {
return false, err
}
o.PullRequestDetails.Message += fmt.Sprintf("change `%s` to version `%s`\n", name, v)
}
return true, nil
}
err := version.ForEachKindVersion(dir, version.VersionKind(o.Kind), callback)
return err
}
func (o *StepCreateVersionPullRequestOptions) findLatestChartVersion(name string) (string, error) {
err := o.updateHelmRepo()
if err != nil {
return "", err
}
info, err := o.Helm().SearchChartVersions(name)
if err != nil {
return "", err
}
if len(info) == 0 {
return "", fmt.Errorf("no version found for chart %s", name)
}
if o.Verbose {
log.Infof("found %d versions:\n", len(info))
for _, v := range info {
log.Infof(" %s:\n", v)
}
}
return info[0], nil
}
// updateHelmRepo updates the helm repos if required
func (o *StepCreateVersionPullRequestOptions) updateHelmRepo() error {
if o.updatedHelmRepo {
return nil
}
log.Info("updating helm repositories to find the latest chart versions...\n")
err := o.Helm().UpdateRepo()
if err != nil {
return errors.Wrap(err, "failed to update helm repos")
}
o.updatedHelmRepo = true
return nil
}
func (o *StepCreateVersionPullRequestOptions) findLatestBuilderImageVersion() (string, error) {
output, err := o.getCommandOutput("", "gcloud", "container", "images", "list-tags", "gcr.io/jenkinsxio/builder-maven", "--format", "json")
if err != nil {
return "", err
}
return gke.FindLatestImageTag(output)
}
// modifyRegex performs a search and replace of the given regular expression with the replacement in the given set of globPattern files
func (o *StepCreateVersionPullRequestOptions) modifyRegex(globPattern string, regexPattern string, replacement string) error {
files, err := filepath.Glob(globPattern)
if err != nil {
return errors.Wrapf(err, "failed to find glob pattern %s", globPattern)
}
re, err := regexp.Compile(regexPattern)
if err != nil {
return errors.Wrapf(err, "failed to parse regex %s", regexPattern)
}
for _, file := range files {
data, err := ioutil.ReadFile(file)
if err != nil {
return errors.Wrapf(err, "failed to load file %s", file)
}
data = re.ReplaceAll(data, []byte(replacement))
err = ioutil.WriteFile(file, data, util.DefaultWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to save file %s", file)
}
}
return nil
}