-
Notifications
You must be signed in to change notification settings - Fork 785
/
step_create_version_pr.go
378 lines (331 loc) · 11.1 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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package cmd
import (
"fmt"
"io/ioutil"
"strings"
"github.com/jenkins-x/jx/pkg/gits"
"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"
pipelineapi "github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/uuid"
)
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/*"
`)
)
// StepCreateVersionPullRequestOptions contains the command line flags
type StepCreateVersionPullRequestOptions struct {
StepOptions
Kind string
Name string
Includes []string
Excludes []string
VersionsRepository string
VersionsBranch string
Version string
updatedHelmRepo bool
branchNameText string
title string
message string
}
// 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.VersionsRepository, "repo", "r", DefaultVersionsURL, "Jenkins X versions Git repo")
cmd.Flags().StringVarP(&options.VersionsBranch, "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")
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)
}
if o.VersionsRepository == "" {
return util.MissingOption("repo")
}
if o.VersionsBranch == "" {
o.VersionsBranch = "master"
}
dir, err := ioutil.TempDir("", "create-version-pr")
if err != nil {
return err
}
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.branchNameText = strings.Replace("upgrade-"+o.Name+"-"+o.Version, "/", "-", -1)
o.branchNameText = strings.Replace(o.branchNameText, ".", "-", -1)
o.title = fmt.Sprintf("%s version upgrade of %s", o.Kind, o.Name)
o.message = fmt.Sprintf("change %s to version %s", o.Name, o.Version)
} else {
o.branchNameText = "upgrade-chart-versions"
o.title = "upgrade chart versions"
}
gitInfo, err := gits.ParseGitURL(o.VersionsRepository)
if err != nil {
return err
}
provider, err := o.gitProviderForURL(o.VersionsRepository, "versions repository")
if err != nil {
return err
}
username := provider.CurrentUsername()
if username == "" {
return fmt.Errorf("no git user name found")
}
originalOrg := gitInfo.Organisation
originalRepo := gitInfo.Name
repo, err := provider.GetRepository(username, originalRepo)
if err != nil {
if originalOrg == username {
return err
}
// lets try create a fork - using a blank organisation to force a user specific fork
repo, err = provider.ForkRepository(originalOrg, originalRepo, "")
if err != nil {
return errors.Wrapf(err, "failed to fork GitHub repo %s/%s to user %s", originalOrg, originalRepo, username)
}
log.Infof("Forked Git repository to %s\n\n", util.ColorInfo(repo.HTMLURL))
}
originalGitURL := o.VersionsRepository
git := o.Git()
err = git.Clone(repo.CloneURL, dir)
if err != nil {
return errors.Wrapf(err, "cloning the version repository %q", repo.CloneURL)
}
log.Infof("cloned fork of version repository %s to %s\n", util.ColorInfo(repo.HTMLURL), util.ColorInfo(dir))
err = git.SetRemoteURL(dir, "upstream", originalGitURL)
if err != nil {
return errors.Wrapf(err, "setting remote upstream %q in forked environment repo", originalGitURL)
}
err = git.PullUpstream(dir)
if err != nil {
return errors.Wrap(err, "pulling upstream of forked versions repository")
}
branchName := o.Git().ConvertToValidBranchName(o.branchNameText)
branchNames, err := o.Git().RemoteBranchNames(dir, "remotes/origin/")
if err != nil {
return errors.Wrapf(err, "failed to load remote branch names")
}
if util.StringArrayIndex(branchNames, branchName) >= 0 {
// lets append a UUID as the branch name already exists
branchName += "-" + string(uuid.NewUUID())
}
err = git.CreateBranch(dir, branchName)
if err != nil {
return err
}
err = git.Checkout(dir, branchName)
if err != nil {
return err
}
err = o.modifyFiles(dir)
if err != nil {
return err
}
err = o.Git().Add(dir, "*", "*/*")
if err != nil {
return err
}
changes, err := git.HasChanges(dir)
if err != nil {
return err
}
if !changes {
log.Infof("No source changes so not generating a Pull Request\n")
return nil
}
err = git.CommitDir(dir, o.title)
if err != nil {
return err
}
// lets find a previous PR so we can force push to its branch
prs, err := provider.ListOpenPullRequests(gitInfo.Organisation, gitInfo.Name)
if err != nil {
return errors.Wrapf(err, "failed to list open pull requests on %s", gitInfo.HTMLURL)
}
for _, pr := range prs {
author := pr.Author
if pr.Title == o.title && author != nil && author.Login == username {
log.Infof("found existing PullRequest: %s\n", util.ColorInfo(pr.URL))
head := pr.HeadRef
if head == nil {
log.Warnf("No head value!\n")
} else {
headText := *head
remoteBranch := headText
paths := strings.SplitN(headText, ":", 2)
if len(paths) > 1 {
remoteBranch = paths[1]
}
log.Infof("force pushing to remote branch %s\n", util.ColorInfo(remoteBranch))
err := git.ForcePushBranch(dir, branchName, remoteBranch)
if err != nil {
return errors.Wrapf(err, "failed to force push to remote branch %s", remoteBranch)
}
pr.Body = o.message
log.Infof("force pushed new pull request change to: %s\n", util.ColorInfo(pr.URL))
err = provider.AddPRComment(pr, o.message)
if err != nil {
return errors.Wrapf(err, "failed to add message to PR %s", pr.URL)
}
return nil
}
}
}
err = git.Push(dir)
if err != nil {
return errors.Wrapf(err, "pushing forked environment dir %q", dir)
}
base := o.VersionsBranch
gha := &gits.GitPullRequestArguments{
GitRepository: gitInfo,
Title: o.title,
Body: o.message,
Base: base,
Head: username + ":" + branchName,
}
pr, err := provider.CreatePullRequest(gha)
if err != nil {
return err
}
log.Infof("Created Pull Request: %s\n\n", util.ColorInfo(pr.URL))
return nil
}
func (o *StepCreateVersionPullRequestOptions) modifyFiles(dir string) error {
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.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
}