github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/step/create/pr/step_create_pr_make.go (about)

     1  package pr
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/olli-ai/jx/v2/pkg/cmd/opts/step"
     7  
     8  	"github.com/olli-ai/jx/v2/pkg/gits/operations"
     9  
    10  	"github.com/pkg/errors"
    11  
    12  	"github.com/jenkins-x/jx-logging/pkg/log"
    13  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
    14  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    15  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    16  	"github.com/olli-ai/jx/v2/pkg/util"
    17  	"github.com/spf13/cobra"
    18  )
    19  
    20  var (
    21  	createPullRequestMakeLong = templates.LongDesc(`
    22  		Creates a Pull Request updating a Makefile so that any variables defined as <name> := <value> will have the 
    23  		value replaced with the new version
    24  
    25  		Files named Makefile or Makefile.* will be updated
    26  `)
    27  
    28  	createPullRequestMakeExample = templates.Examples(`
    29  		jx step create pr make --name CHART_VERSION --version 1.2.3 --repo https://github.com/jenkins-x/cloud-environments.git
    30  					`)
    31  )
    32  
    33  // StepCreatePullRequestMakeOptions contains the command line flags
    34  type StepCreatePullRequestMakeOptions struct {
    35  	StepCreatePrOptions
    36  
    37  	Name string
    38  }
    39  
    40  // NewCmdStepCreatePullRequestMake Creates a new Command object
    41  func NewCmdStepCreatePullRequestMake(commonOpts *opts.CommonOptions) *cobra.Command {
    42  	options := &StepCreatePullRequestMakeOptions{
    43  		StepCreatePrOptions: StepCreatePrOptions{
    44  			StepCreateOptions: step.StepCreateOptions{
    45  				StepOptions: step.StepOptions{
    46  					CommonOptions: commonOpts,
    47  				},
    48  			},
    49  		},
    50  	}
    51  
    52  	cmd := &cobra.Command{
    53  		Use:     "make",
    54  		Short:   "Creates a Pull Request on a git repository, doing an update to a Makefile",
    55  		Long:    createPullRequestMakeLong,
    56  		Example: createPullRequestMakeExample,
    57  		Aliases: []string{"version pullrequest"},
    58  		Run: func(cmd *cobra.Command, args []string) {
    59  			options.Cmd = cmd
    60  			options.Args = args
    61  			err := options.Run()
    62  			helper.CheckErr(err)
    63  		},
    64  	}
    65  	AddStepCreatePrFlags(cmd, &options.StepCreatePrOptions)
    66  	cmd.Flags().StringVarP(&options.Name, "name", "", "", "The name of the variable to use when doing updates")
    67  	return cmd
    68  }
    69  
    70  // ValidateMakeOptions validates the common options for make pr steps
    71  func (o *StepCreatePullRequestMakeOptions) ValidateMakeOptions() error {
    72  	if err := o.ValidateOptions(false); err != nil {
    73  		return errors.WithStack(err)
    74  	}
    75  	if o.Name == "" {
    76  		return util.MissingOption("name")
    77  	}
    78  	if o.SrcGitURL == "" {
    79  		log.Logger().Warnf("srcRepo is not provided so generated PR will not be correctly linked in release notesPR")
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  // Run implements this command
    86  func (o *StepCreatePullRequestMakeOptions) Run() error {
    87  	if err := o.ValidateMakeOptions(); err != nil {
    88  		return errors.WithStack(err)
    89  	}
    90  	fn, err := operations.CreatePullRequestRegexFn(o.Version, fmt.Sprintf(`^%s\s*:=\s*(?P<version>.+)`, o.Name), "Makefile", "Makefile.*")
    91  	if err != nil {
    92  		return errors.WithStack(err)
    93  	}
    94  	err = o.CreatePullRequest("make", fn)
    95  	if err != nil {
    96  		return errors.WithStack(err)
    97  	}
    98  	return nil
    99  }