Skip to content

Commit

Permalink
mr_reopen: add command for reopening a closed MR
Browse files Browse the repository at this point in the history
MRs can be closed from `lab` cli, it should also be possible to reopen them
as needed. This patch enables `lab mr reopen` command, allowing the user to
reopen the desired MR.

Signed-off-by: Bruno Meneguele <[email protected]>
  • Loading branch information
bmeneg committed Dec 10, 2020
1 parent 1b34b39 commit 6e4a87a
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cmd/mr_reopen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"fmt"
"log"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
)

var mrReopenCmd = &cobra.Command{
Use: "reopen [remote] <id>",
Short: "Reopen a closed merge request",
Long: ``,
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, id, err := parseArgsWithGitBranchMR(args)
if err != nil {
log.Fatal(err)
}

p, err := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
}

err = lab.MRReopen(p.ID, int(id))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Merge Request #%d reopened\n", id)
},
}

func init() {
mrCmd.AddCommand(mrReopenCmd)
carapace.Gen(mrReopenCmd).PositionalCompletion(
action.Remotes(),
action.MergeRequests(mrList),
)
}
54 changes: 54 additions & 0 deletions cmd/mr_reopen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"os/exec"
"testing"

"github.com/stretchr/testify/require"
)

func Test_mrCloseReopen(t *testing.T) {
tests := []struct {
desc string
opt string
expected string
}{
{
desc: "reopen-open",
opt: "reopen",
expected: "mr not closed",
},
{
desc: "close-open",
opt: "close",
expected: "Merge Request #19 closed",
},
{
desc: "close-closed",
opt: "close",
expected: "mr already closed",
},
{
desc: "reopen-closed",
opt: "reopen",
expected: "Merge Request #19 reopened",
},
}

repo := copyTestRepo(t)
for _, test := range tests {
test := test
t.Run(test.desc, func(t *testing.T) {
cmd := exec.Command(labBinaryPath, "mr", test.opt, "19")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
}

out := string(b)
require.Contains(t, out, test.expected)
})
}
}
18 changes: 18 additions & 0 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,24 @@ func MRClose(pid interface{}, id int) error {
return nil
}

// MRReopen reopen an already close mr on a GitLab project
func MRReopen(pid interface{}, id int) error {
mr, _, err := lab.MergeRequests.GetMergeRequest(pid, id, nil)
if err != nil {
return err
}
if mr.State == "opened" {
return fmt.Errorf("mr not closed")
}
_, _, err = lab.MergeRequests.UpdateMergeRequest(pid, int(id), &gitlab.UpdateMergeRequestOptions{
StateEvent: gitlab.String("reopen"),
})
if err != nil {
return err
}
return nil
}

// MRListDiscussions retrieves the discussions (aka notes & comments) for a merge request
func MRListDiscussions(project string, mrNum int) ([]*gitlab.Discussion, error) {
p, err := FindProject(project)
Expand Down

0 comments on commit 6e4a87a

Please sign in to comment.