github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/step/create/pr/step_create_pr_regex.go (about) 1 package pr 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/olli-ai/jx/v2/pkg/cmd/opts/step" 8 "github.com/olli-ai/jx/v2/pkg/gits" 9 "github.com/olli-ai/jx/v2/pkg/gits/operations" 10 11 "github.com/jenkins-x/jx-logging/pkg/log" 12 "github.com/pkg/errors" 13 14 "github.com/olli-ai/jx/v2/pkg/cmd/helper" 15 16 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 17 "github.com/olli-ai/jx/v2/pkg/cmd/templates" 18 "github.com/olli-ai/jx/v2/pkg/util" 19 "github.com/spf13/cobra" 20 ) 21 22 var ( 23 createPullRequestRegexLong = templates.LongDesc(` 24 Creates a Pull Request on a git repository updating files using a regex. 25 26 Any named capturing group called "version" will be replaced. If there are no named capturing groups, then the 27 all the capturing group will be used. 28 " 29 `) 30 31 createPullRequestRegexExample = templates.Examples(` 32 # Create a PR to change the value of release = <value> to $VERSION in the config.toml file 33 ./build/linux/jx step create pr regex --regex "\s*release = \"(.*)\"" --version $VERSION --files config.toml \ 34 --repo https://github.com/jenkins-x/jx-docs.git 35 36 # Create a PR to change the value of the ImageTag: <value> to ${VERSION} where the previous line is Image: 37 # "jenkinsxio/jenkinsx" in the jenkins-x-platform/values.yaml file 38 jx step create pr regex --regex "^(?m)\s+Image: \"jenkinsxio\/jenkinsx\"\s+ImageTag: \"(.*)\"$" \ 39 --version ${VERSION} --files values.yaml --repo https://github.com/jenkins-x/jenkins-x-platform.git 40 41 # Create a PR to change the value of the named capture to $VERSION in the config.toml file 42 ./build/linux/jx step create pr regex --regex "\s*release = \"(?P<version>.*)\"" --version $VERSION --files config.toml \ 43 --repo https://github.com/jenkins-x/jx-docs.git 44 45 `) 46 ) 47 48 // StepCreatePullRequestRegexOptions contains the command line flags 49 type StepCreatePullRequestRegexOptions struct { 50 StepCreatePrOptions 51 52 Regexps []string 53 Files []string 54 Kind string 55 } 56 57 // NewCmdStepCreatePullRequestRegex Creates a new Command object 58 func NewCmdStepCreatePullRequestRegex(commonOpts *opts.CommonOptions) *cobra.Command { 59 options := &StepCreatePullRequestRegexOptions{ 60 StepCreatePrOptions: StepCreatePrOptions{ 61 StepCreateOptions: step.StepCreateOptions{ 62 StepOptions: step.StepOptions{ 63 CommonOptions: commonOpts, 64 }, 65 }, 66 }, 67 } 68 69 cmd := &cobra.Command{ 70 Use: "regex", 71 Short: "Creates a Pull Request on a git repository, doing an update using the provided regex", 72 Long: createPullRequestRegexLong, 73 Example: createPullRequestRegexExample, 74 Aliases: []string{"version pullrequest"}, 75 Run: func(cmd *cobra.Command, args []string) { 76 options.Cmd = cmd 77 options.Args = args 78 err := options.Run() 79 helper.CheckErr(err) 80 }, 81 } 82 AddStepCreatePrFlags(cmd, &options.StepCreatePrOptions) 83 cmd.Flags().StringArrayVarP(&options.Regexps, "regex", "", make([]string, 0), "The regex to use when doing updates") 84 cmd.Flags().StringArrayVarP(&options.Files, "files", "", make([]string, 0), "A glob describing the files to change") 85 return cmd 86 } 87 88 // ValidateRegexOptions validates the common options for regex pr steps 89 func (o *StepCreatePullRequestRegexOptions) ValidateRegexOptions() error { 90 if err := o.ValidateOptions(false); err != nil { 91 return errors.WithStack(err) 92 } 93 if len(o.Regexps) == 0 { 94 return util.MissingOption("regex") 95 } 96 97 for i, regex := range o.Regexps { 98 // ensure the regexp is multi-line 99 if !strings.HasPrefix(regex, "(?m") { 100 o.Regexps[i] = fmt.Sprintf("(?m)%s", regex) 101 } 102 } 103 if o.SrcGitURL == "" { 104 log.Logger().Warnf("srcRepo is not provided so generated PR will not be correctly linked in release notesPR") 105 } 106 if o.Kind == "" { 107 o.Kind = "regex" 108 } 109 110 return nil 111 } 112 113 // Run implements this command 114 func (o *StepCreatePullRequestRegexOptions) Run() error { 115 if err := o.ValidateRegexOptions(); err != nil { 116 return errors.WithStack(err) 117 } 118 modifyFns := make([]operations.ChangeFilesFn, 0) 119 for _, regex := range o.Regexps { 120 fn, err := operations.CreatePullRequestRegexFn(o.Version, regex, o.Files...) 121 if err != nil { 122 return errors.WithStack(err) 123 } 124 modifyFns = append(modifyFns, fn) 125 } 126 err := o.CreatePullRequest(o.Kind, func(dir string, gitInfo *gits.GitRepository) ([]string, error) { 127 var oldVersions []string 128 for _, fn := range modifyFns { 129 answer, err := fn(dir, gitInfo) 130 if err != nil { 131 return nil, errors.WithStack(err) 132 } 133 oldVersions = append(oldVersions, answer...) 134 } 135 return oldVersions, nil 136 }) 137 if err != nil { 138 return errors.WithStack(err) 139 } 140 return nil 141 }