github.com/zaquestion/lab@v0.25.1/cmd/mr_merge.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/MakeNowJust/heredoc/v2"
     7  	"github.com/rsteube/carapace"
     8  	"github.com/spf13/cobra"
     9  	gitlab "github.com/xanzy/go-gitlab"
    10  	"github.com/zaquestion/lab/internal/action"
    11  	lab "github.com/zaquestion/lab/internal/gitlab"
    12  )
    13  
    14  var mergeImmediate bool
    15  
    16  var mrMergeCmd = &cobra.Command{
    17  	Use:   "merge [remote] [<MR id or branch>]",
    18  	Short: "Merge an open merge request",
    19  	Long: heredoc.Doc(`
    20  		Merges an open merge request. If the pipeline in the project is
    21  		enabled and is still running for that specific MR, by default,
    22  		this command will sets the merge to only happen when the pipeline
    23  		succeeds.`),
    24  	Example: heredoc.Doc(`
    25  		lab mr merge origin 10
    26  		lab mr merge upstream 11 -i`),
    27  	PersistentPreRun: labPersistentPreRun,
    28  	Run: func(cmd *cobra.Command, args []string) {
    29  		rn, id, err := parseArgsWithGitBranchMR(args)
    30  		if err != nil {
    31  			log.Fatal(err)
    32  		}
    33  
    34  		opts := gitlab.AcceptMergeRequestOptions{
    35  			MergeWhenPipelineSucceeds: gitlab.Bool(!mergeImmediate),
    36  		}
    37  
    38  		err = lab.MRMerge(rn, int(id), &opts)
    39  		if err != nil {
    40  			log.Fatal(err)
    41  		}
    42  		fmt.Printf("Merge Request !%d merged\n", id)
    43  	},
    44  }
    45  
    46  func init() {
    47  	mrMergeCmd.Flags().BoolVarP(&mergeImmediate, "immediate", "i", false, "merge immediately, regardless pipeline results")
    48  	mrCmd.AddCommand(mrMergeCmd)
    49  	carapace.Gen(mrMergeCmd).PositionalCompletion(
    50  		action.Remotes(),
    51  		action.MergeRequests(mrList),
    52  	)
    53  }