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

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/MakeNowJust/heredoc/v2"
     6  	"github.com/rsteube/carapace"
     7  	"github.com/spf13/cobra"
     8  	"github.com/zaquestion/lab/internal/action"
     9  	lab "github.com/zaquestion/lab/internal/gitlab"
    10  	"os"
    11  )
    12  
    13  var mrApproveCmd = &cobra.Command{
    14  	Use:     "approve [remote] [<MR id or branch>]",
    15  	Aliases: []string{},
    16  	Short:   "Approve merge request",
    17  	Example: heredoc.Doc(`
    18  		lab mr approve origin
    19  		lab mr approve upstream -F test_file
    20  		lab mr approve upstream -m "A helpfull comment"
    21  		lab mr approve upstream --with-comment
    22  		lab mr approve upstream -m "A helpfull\nComment" --force-linebreak`),
    23  	PersistentPreRun: labPersistentPreRun,
    24  	Run: func(cmd *cobra.Command, args []string) {
    25  		rn, id, err := parseArgsWithGitBranchMR(args)
    26  		if err != nil {
    27  			log.Fatal(err)
    28  		}
    29  
    30  		approvalConfig, err := lab.GetMRApprovalsConfiguration(rn, int(id))
    31  		if err != nil {
    32  			log.Fatal(err)
    33  		}
    34  
    35  		for _, approvers := range approvalConfig.ApprovedBy {
    36  			if approvers.User.Username == lab.User() {
    37  				fmt.Printf("Merge Request !%d already approved\n", id)
    38  				os.Exit(1)
    39  			}
    40  		}
    41  
    42  		comment, err := cmd.Flags().GetBool("with-comment")
    43  		if err != nil {
    44  			log.Fatal(err)
    45  		}
    46  
    47  		msgs, err := cmd.Flags().GetStringArray("message")
    48  		if err != nil {
    49  			log.Fatal(err)
    50  		}
    51  
    52  		filename, err := cmd.Flags().GetString("file")
    53  		if err != nil {
    54  			log.Fatal(err)
    55  		}
    56  
    57  		note := comment || len(msgs) > 0 || filename != ""
    58  		linebreak := false
    59  		if note {
    60  			linebreak, err = cmd.Flags().GetBool("force-linebreak")
    61  			if err != nil {
    62  				log.Fatal(err)
    63  			}
    64  			if comment {
    65  				state := noteGetState(rn, true, int(id))
    66  				msg, _ := noteMsg(msgs, true, int(id), state, "", "")
    67  				msgs = append(msgs, msg)
    68  			}
    69  		}
    70  
    71  		msgs = append(msgs, "/approve")
    72  		createNote(rn, true, int(id), msgs, filename, linebreak, "", note)
    73  
    74  		fmt.Printf("Merge Request !%d approved\n", id)
    75  	},
    76  }
    77  
    78  func init() {
    79  	mrApproveCmd.Flags().Bool("with-comment", false, "Add a comment with the approval")
    80  	mrApproveCmd.Flags().StringArrayP("message", "m", []string{}, "use the given <msg>; multiple -m are concatenated as separate paragraphs")
    81  	mrApproveCmd.Flags().StringP("file", "F", "", "use the given file as the message")
    82  	mrApproveCmd.Flags().Bool("force-linebreak", false, "append 2 spaces to the end of each line to force markdown linebreaks")
    83  	mrCmd.AddCommand(mrApproveCmd)
    84  	carapace.Gen(mrApproveCmd).PositionalCompletion(
    85  		action.Remotes(),
    86  		action.MergeRequests(mrList),
    87  	)
    88  }