github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/step/update/release/step_update_release_github.go (about)

     1  package release
     2  
     3  import (
     4  	"os"
     5  
     6  	v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
     7  	"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
     8  	"github.com/jenkins-x/jx/v2/pkg/gits"
     9  	"github.com/pkg/errors"
    10  
    11  	"github.com/jenkins-x/jx-logging/pkg/log"
    12  	"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
    13  	"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
    14  	"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  var (
    19  	updateReleaseGithubLong = templates.LongDesc(`
    20  		updates the status of a release to be either a prerelease or a release
    21  	`)
    22  
    23  	updateReleaseGitHubExample = templates.Examples(`
    24          jx step update release github -o jenkins-x -r jx -v 1.2.3 -p=false
    25  	`)
    26  )
    27  
    28  // StepUpdateReleaseGitHubOptions contains the command line flags
    29  type StepUpdateReleaseGitHubOptions struct {
    30  	StepUpdateReleaseOptions
    31  	PreRelease bool
    32  
    33  	State StepUpdateReleaseStatusState
    34  }
    35  
    36  // StepUpdateReleaseStatusState contains the state information
    37  type StepUpdateReleaseStatusState struct {
    38  	GitInfo     *gits.GitRepository
    39  	GitProvider gits.GitProvider
    40  	Release     *v1.Release
    41  }
    42  
    43  // NewCmdStepUpdateReleaseGitHub Creates a new Command object
    44  func NewCmdStepUpdateReleaseGitHub(commonOpts *opts.CommonOptions) *cobra.Command {
    45  	options := &StepUpdateReleaseGitHubOptions{
    46  		StepUpdateReleaseOptions: StepUpdateReleaseOptions{
    47  			StepUpdateOptions: step.StepUpdateOptions{
    48  				StepOptions: step.StepOptions{
    49  					CommonOptions: commonOpts,
    50  				},
    51  			},
    52  		},
    53  	}
    54  
    55  	cmd := &cobra.Command{
    56  		Use:     "github",
    57  		Short:   "Updates a release to either a prerelease or a release",
    58  		Long:    updateReleaseGithubLong,
    59  		Example: updateReleaseGitHubExample,
    60  		Aliases: []string{"gh"},
    61  		Run: func(cmd *cobra.Command, args []string) {
    62  			options.Cmd = cmd
    63  			options.Args = args
    64  			err := options.Run()
    65  			helper.CheckErr(err)
    66  		},
    67  	}
    68  	AddStepUpdateReleaseFlags(cmd, &options.StepUpdateReleaseOptions)
    69  	cmd.Flags().BoolVarP(&options.PreRelease, "prerelease", "p", false, "The release state of that version release of the repository")
    70  	return cmd
    71  }
    72  
    73  // ValidateGitHubOptions validates the common options for brew pr steps
    74  func (o *StepUpdateReleaseGitHubOptions) ValidateGitHubOptions() error {
    75  	if err := o.ValidateOptions(); err != nil {
    76  		return errors.WithStack(err)
    77  	}
    78  
    79  	return nil
    80  }
    81  
    82  // Run implements this command
    83  func (o *StepUpdateReleaseGitHubOptions) Run() error {
    84  	if err := o.ValidateGitHubOptions(); err != nil {
    85  		return errors.WithStack(err)
    86  	}
    87  
    88  	releaseInfo := &gits.GitRelease{
    89  		PreRelease: o.PreRelease,
    90  	}
    91  	dir, err := os.Getwd()
    92  	if err != nil {
    93  		return err
    94  	}
    95  	authConfigSvc, err := o.GitAuthConfigService()
    96  	if err != nil {
    97  		return err
    98  	}
    99  	gitDir, gitConfDir, err := o.Git().FindGitConfigDir(dir)
   100  	if err != nil {
   101  		return err
   102  	}
   103  	if gitDir == "" || gitConfDir == "" {
   104  		log.Logger().Warnf("No git directory could be found from dir %s", dir)
   105  		return nil
   106  	}
   107  	gitURL, err := o.Git().DiscoverUpstreamGitURL(gitConfDir)
   108  	if err != nil {
   109  		return err
   110  	}
   111  	gitInfo, err := gits.ParseGitURL(gitURL)
   112  	if err != nil {
   113  		return err
   114  	}
   115  	o.State.GitInfo = gitInfo
   116  
   117  	gitKind, err := o.GitServerKind(gitInfo)
   118  	if err != nil {
   119  		return err
   120  	}
   121  	ghOwner, err := o.GetGitHubAppOwner(gitInfo)
   122  	if err != nil {
   123  		return err
   124  	}
   125  
   126  	gitProvider, err := o.State.GitInfo.CreateProvider(o.InCluster(), authConfigSvc, gitKind, ghOwner, o.Git(), false, o.GetIOFileHandles())
   127  	if err != nil {
   128  		return errors.Wrap(err, "Could not create GitProvider, unable to update the release state %s")
   129  	}
   130  	o.State.GitProvider = gitProvider
   131  	err = gitProvider.UpdateReleaseStatus(o.Owner, o.Repository, o.Version, releaseInfo)
   132  	if err != nil {
   133  		return err
   134  	}
   135  	return nil
   136  }