-
Notifications
You must be signed in to change notification settings - Fork 785
/
step_create_pr_regex.go
141 lines (122 loc) · 4.41 KB
/
step_create_pr_regex.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
package pr
import (
"fmt"
"strings"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/gits"
"github.com/jenkins-x/jx/v2/pkg/gits/operations"
"github.com/jenkins-x/jx-logging/pkg/log"
"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/util"
"github.com/spf13/cobra"
)
var (
createPullRequestRegexLong = templates.LongDesc(`
Creates a Pull Request on a git repository updating files using a regex.
Any named capturing group called "version" will be replaced. If there are no named capturing groups, then the
all the capturing group will be used.
"
`)
createPullRequestRegexExample = templates.Examples(`
# Create a PR to change the value of release = <value> to $VERSION in the config.toml file
./build/linux/jx step create pr regex --regex "\s*release = \"(.*)\"" --version $VERSION --files config.toml \
--repo https://github.com/jenkins-x/jx-docs.git
# Create a PR to change the value of the ImageTag: <value> to ${VERSION} where the previous line is Image:
# "jenkinsxio/jenkinsx" in the jenkins-x-platform/values.yaml file
jx step create pr regex --regex "^(?m)\s+Image: \"jenkinsxio\/jenkinsx\"\s+ImageTag: \"(.*)\"$" \
--version ${VERSION} --files values.yaml --repo https://github.com/jenkins-x/jenkins-x-platform.git
# Create a PR to change the value of the named capture to $VERSION in the config.toml file
./build/linux/jx step create pr regex --regex "\s*release = \"(?P<version>.*)\"" --version $VERSION --files config.toml \
--repo https://github.com/jenkins-x/jx-docs.git
`)
)
// StepCreatePullRequestRegexOptions contains the command line flags
type StepCreatePullRequestRegexOptions struct {
StepCreatePrOptions
Regexps []string
Files []string
Kind string
}
// NewCmdStepCreatePullRequestRegex Creates a new Command object
func NewCmdStepCreatePullRequestRegex(commonOpts *opts.CommonOptions) *cobra.Command {
options := &StepCreatePullRequestRegexOptions{
StepCreatePrOptions: StepCreatePrOptions{
StepCreateOptions: step.StepCreateOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
},
},
}
cmd := &cobra.Command{
Use: "regex",
Short: "Creates a Pull Request on a git repository, doing an update using the provided regex",
Long: createPullRequestRegexLong,
Example: createPullRequestRegexExample,
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().StringArrayVarP(&options.Regexps, "regex", "", make([]string, 0), "The regex to use when doing updates")
cmd.Flags().StringArrayVarP(&options.Files, "files", "", make([]string, 0), "A glob describing the files to change")
return cmd
}
// ValidateRegexOptions validates the common options for regex pr steps
func (o *StepCreatePullRequestRegexOptions) ValidateRegexOptions() error {
if err := o.ValidateOptions(false); err != nil {
return errors.WithStack(err)
}
if len(o.Regexps) == 0 {
return util.MissingOption("regex")
}
for i, regex := range o.Regexps {
// ensure the regexp is multi-line
if !strings.HasPrefix(regex, "(?m") {
o.Regexps[i] = fmt.Sprintf("(?m)%s", regex)
}
}
if o.SrcGitURL == "" {
log.Logger().Warnf("srcRepo is not provided so generated PR will not be correctly linked in release notesPR")
}
if o.Kind == "" {
o.Kind = "regex"
}
return nil
}
// Run implements this command
func (o *StepCreatePullRequestRegexOptions) Run() error {
if err := o.ValidateRegexOptions(); err != nil {
return errors.WithStack(err)
}
modifyFns := make([]operations.ChangeFilesFn, 0)
for _, regex := range o.Regexps {
fn, err := operations.CreatePullRequestRegexFn(o.Version, regex, o.Files...)
if err != nil {
return errors.WithStack(err)
}
modifyFns = append(modifyFns, fn)
}
err := o.CreatePullRequest(o.Kind, func(dir string, gitInfo *gits.GitRepository) ([]string, error) {
var oldVersions []string
for _, fn := range modifyFns {
answer, err := fn(dir, gitInfo)
if err != nil {
return nil, errors.WithStack(err)
}
oldVersions = append(oldVersions, answer...)
}
return oldVersions, nil
})
if err != nil {
return errors.WithStack(err)
}
return nil
}