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

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