-
Notifications
You must be signed in to change notification settings - Fork 785
/
step_create_pr_make.go
99 lines (84 loc) · 2.79 KB
/
step_create_pr_make.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
package pr
import (
"fmt"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/gits/operations"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"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/log"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/spf13/cobra"
)
var (
createPullRequestMakeLong = templates.LongDesc(`
Creates a Pull Request updating a Makefile so that any variables defined as <name> := <value> will have the
value replaced with the new version
Files named Makefile or Makefile.* will be updated
`)
createPullRequestMakeExample = templates.Examples(`
jx step create pr make --name CHART_VERSION --version 1.2.3 --repo https://github.com/jenkins-x/cloud-environments.git
`)
)
// StepCreatePullRequestMakeOptions contains the command line flags
type StepCreatePullRequestMakeOptions struct {
StepCreatePrOptions
Name string
}
// NewCmdStepCreatePullRequestMake Creates a new Command object
func NewCmdStepCreatePullRequestMake(commonOpts *opts.CommonOptions) *cobra.Command {
options := &StepCreatePullRequestMakeOptions{
StepCreatePrOptions: StepCreatePrOptions{
StepCreateOptions: step.StepCreateOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
},
},
}
cmd := &cobra.Command{
Use: "make",
Short: "Creates a Pull Request on a git repository, doing an update to a Makefile",
Long: createPullRequestMakeLong,
Example: createPullRequestMakeExample,
Aliases: []string{"version pullrequest"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
AddStepCreatePrFlags(cmd, &options.StepCreatePrOptions)
cmd.Flags().StringVarP(&options.Name, "name", "", "", "The name of the variable to use when doing updates")
return cmd
}
// ValidateMakeOptions validates the common options for make pr steps
func (o *StepCreatePullRequestMakeOptions) ValidateMakeOptions() error {
if err := o.ValidateOptions(false); err != nil {
return errors.WithStack(err)
}
if o.Name == "" {
return util.MissingOption("name")
}
if o.SrcGitURL == "" {
log.Logger().Warnf("srcRepo is not provided so generated PR will not be correctly linked in release notesPR")
}
return nil
}
// Run implements this command
func (o *StepCreatePullRequestMakeOptions) Run() error {
if err := o.ValidateMakeOptions(); err != nil {
return errors.WithStack(err)
}
fn, err := operations.CreatePullRequestRegexFn(o.Version, fmt.Sprintf(`^%s\s*:=\s*(?P<version>.+)`, o.Name), "Makefile", "Makefile.*")
if err != nil {
return errors.WithStack(err)
}
err = o.CreatePullRequest("make", fn)
if err != nil {
return errors.WithStack(err)
}
return nil
}