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

     1  package step
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/olli-ai/jx/v2/pkg/cmd/opts/step"
     8  
     9  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
    10  
    11  	"github.com/jenkins-x/jx-logging/pkg/log"
    12  	"github.com/olli-ai/jx/v2/pkg/buildnum"
    13  	"github.com/olli-ai/jx/v2/pkg/kube"
    14  	"github.com/olli-ai/jx/v2/pkg/util"
    15  
    16  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    17  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    18  	"github.com/spf13/cobra"
    19  )
    20  
    21  const (
    22  	optionBranch = "branch"
    23  	optionOwner  = "owner"
    24  )
    25  
    26  // StepNextBuildNumberOptions contains the command line flags
    27  type StepNextBuildNumberOptions struct {
    28  	step.StepOptions
    29  
    30  	Owner      string
    31  	Repository string
    32  	Branch     string
    33  }
    34  
    35  var (
    36  	StepNextBuildNumberLong = templates.LongDesc(`
    37  		Generates the next build unique number for a pipeline
    38  `)
    39  
    40  	StepNextBuildNumberExample = templates.Examples(`
    41  		jx step next-buildnumber 
    42  `)
    43  )
    44  
    45  func NewCmdStepNextBuildNumber(commonOpts *opts.CommonOptions) *cobra.Command {
    46  	options := StepNextBuildNumberOptions{
    47  		StepOptions: step.StepOptions{
    48  			CommonOptions: commonOpts,
    49  		},
    50  	}
    51  	cmd := &cobra.Command{
    52  		Use:     "next-buildnumber",
    53  		Short:   "Generates the next build unique number for a pipeline.",
    54  		Long:    StepNextBuildNumberLong,
    55  		Example: StepNextBuildNumberExample,
    56  		Aliases: []string{"next-buildno"},
    57  		Run: func(cmd *cobra.Command, args []string) {
    58  			options.Cmd = cmd
    59  			options.Args = args
    60  			err := options.Run()
    61  			helper.CheckErr(err)
    62  		},
    63  	}
    64  	cmd.Flags().StringVarP(&options.Owner, optionOwner, "o", "", "The Git repository owner")
    65  	cmd.Flags().StringVarP(&options.Repository, optionRepo, "r", "", "The Git repository name")
    66  	cmd.Flags().StringVarP(&options.Branch, optionBranch, "", "master", "The Git branch")
    67  	return cmd
    68  }
    69  
    70  func (o *StepNextBuildNumberOptions) Run() error {
    71  	if o.Owner == "" {
    72  		return util.MissingOption(optionOwner)
    73  	}
    74  	if o.Repository == "" {
    75  		return util.MissingOption(optionRepo)
    76  	}
    77  
    78  	jxClient, ns, err := o.JXClientAndDevNamespace()
    79  	if err != nil {
    80  		return err
    81  	}
    82  	buildNumGen := buildnum.NewCRDBuildNumGen(jxClient, ns)
    83  
    84  	pID := kube.NewPipelineID(o.Owner, o.Repository, o.Branch)
    85  
    86  	attempts := 100
    87  	for i := 0; i < attempts; i++ {
    88  		buildNum, err := buildNumGen.NextBuildNumber(pID)
    89  		if err == nil {
    90  			log.Logger().Infof("%s", buildNum)
    91  			return nil
    92  		}
    93  
    94  		time.Sleep(time.Second)
    95  	}
    96  
    97  	return fmt.Errorf("Failed after %d attempts to create a new build number for pipeline %s. "+
    98  		"The last error was: %s", attempts, pID.ID, err)
    99  }