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

     1  package pr
     2  
     3  import (
     4  	"github.com/olli-ai/jx/v2/pkg/brew"
     5  	"github.com/olli-ai/jx/v2/pkg/cmd/opts/step"
     6  	"github.com/olli-ai/jx/v2/pkg/gits"
     7  	"github.com/pkg/errors"
     8  
     9  	"github.com/jenkins-x/jx-logging/pkg/log"
    10  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
    11  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    12  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    13  	"github.com/olli-ai/jx/v2/pkg/util"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  var (
    18  	createPullRequestBrewLong = templates.LongDesc(`
    19  		Creates a Pull Request on a git repository updating any lines in the Dockerfile that start with FROM, ENV or ARG=
    20  `)
    21  
    22  	createPullRequestBrewExample = templates.Examples(`
    23  					`)
    24  )
    25  
    26  // StepCreatePullRequestBrewOptions contains the command line flags
    27  type StepCreatePullRequestBrewOptions struct {
    28  	StepCreatePrOptions
    29  	Sha string
    30  }
    31  
    32  // NewCmdStepCreatePullRequestBrew Creates a new Command object
    33  func NewCmdStepCreatePullRequestBrew(commonOpts *opts.CommonOptions) *cobra.Command {
    34  	options := &StepCreatePullRequestBrewOptions{
    35  		StepCreatePrOptions: StepCreatePrOptions{
    36  			StepCreateOptions: step.StepCreateOptions{
    37  				StepOptions: step.StepOptions{
    38  					CommonOptions: commonOpts,
    39  				},
    40  			},
    41  		},
    42  	}
    43  
    44  	cmd := &cobra.Command{
    45  		Use:     "brew",
    46  		Short:   "Creates a Pull Request on a git repository updating the homebrew file",
    47  		Long:    createPullRequestBrewLong,
    48  		Example: createPullRequestBrewExample,
    49  		Aliases: []string{"version pullrequest"},
    50  		Run: func(cmd *cobra.Command, args []string) {
    51  			options.Cmd = cmd
    52  			options.Args = args
    53  			err := options.Run()
    54  			helper.CheckErr(err)
    55  		},
    56  	}
    57  	AddStepCreatePrFlags(cmd, &options.StepCreatePrOptions)
    58  	cmd.Flags().StringVarP(&options.Sha, "sha", "", "", "The sha of the brew archive to update")
    59  	return cmd
    60  }
    61  
    62  // ValidateOptions validates the common options for brew pr steps
    63  func (o *StepCreatePullRequestBrewOptions) ValidateBrewOptions() error {
    64  	if err := o.ValidateOptions(false); err != nil {
    65  		return errors.WithStack(err)
    66  	}
    67  	if o.Sha == "" {
    68  		return util.MissingOption("sha")
    69  	}
    70  	if o.SrcGitURL == "" {
    71  		log.Logger().Warnf("srcRepo is not provided so generated PR will not be correctly linked in release notesPR")
    72  	}
    73  
    74  	return nil
    75  }
    76  
    77  // Run implements this command
    78  func (o *StepCreatePullRequestBrewOptions) Run() error {
    79  	if err := o.ValidateBrewOptions(); err != nil {
    80  		return errors.WithStack(err)
    81  	}
    82  	err := o.CreatePullRequest("brew",
    83  		func(dir string, gitInfo *gits.GitRepository) ([]string, error) {
    84  			oldVersions, _, err := brew.UpdateVersionAndSha(dir, o.Version, o.Sha)
    85  			if err != nil {
    86  				return nil, errors.Wrapf(err, "updating version to %s and sha to %s", o.Version, o.Sha)
    87  			}
    88  			return oldVersions, nil
    89  		})
    90  	if err != nil {
    91  		return errors.WithStack(err)
    92  	}
    93  	return nil
    94  }