Skip to content

Commit

Permalink
mr: add delete command
Browse files Browse the repository at this point in the history
This patch adds the ability to delete an MR.

Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
  • Loading branch information
bmeneg committed Mar 20, 2021
1 parent 78a28ab commit 9a36b5f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
1 change: 0 additions & 1 deletion cmd/mr_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

var mrCloseCmd = &cobra.Command{
Use: "close [remote] <id>",
Aliases: []string{"delete"},
Short: "Close merge request",
Long: ``,
PersistentPreRun: LabPersistentPreRun,
Expand Down
36 changes: 36 additions & 0 deletions cmd/mr_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"github.com/spf13/cobra"
lab "github.com/zaquestion/lab/internal/gitlab"

"fmt"
"log"
)

var mrDeleteCmd = &cobra.Command{
Use: "delete [remote] <id>",
Aliases: []string{"del"},
Short: "Delete a merge request on GitLab",
Long: `Delete a merge request (default: MR created on default branch of origin)`,
Args: cobra.MaximumNArgs(2),
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
remote, id, err := parseArgsWithGitBranchMR(args)
if err != nil {
log.Fatal(err)
}
mrNum := int(id)

err = lab.MRDelete(remote, mrNum)
if err != nil {
log.Fatal(err)
}

fmt.Printf("Merge request #%d deleted\n", mrNum)
},
}

func init() {
mrCmd.AddCommand(mrDeleteCmd)
}
17 changes: 17 additions & 0 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,23 @@ func MRUpdate(project string, mrNum int, opts *gitlab.UpdateMergeRequestOptions)
return issue.WebURL, nil
}

// MRDelete deletes an merge request on a GitLab project
func MRDelete(project string, mrNum int) error {
p, err := FindProject(project)
if err != nil {
return err
}
resp, err := lab.MergeRequests.DeleteMergeRequest(p.ID, mrNum)
if resp != nil && resp.StatusCode == http.StatusForbidden {
return ErrStatusForbidden
}
if err != nil {
return err
}

return nil
}

// MRCreateNote adds a note to a merge request on GitLab
func MRCreateNote(project string, mrNum int, opts *gitlab.CreateMergeRequestNoteOptions) (string, error) {
p, err := FindProject(project)
Expand Down

0 comments on commit 9a36b5f

Please sign in to comment.