Skip to content

Commit

Permalink
issue_reopen: Add subcommand to reopen closed issues
Browse files Browse the repository at this point in the history
Commit 6e4a87a added such a command for merge requests;
the same functionality is useful for issues too, so add the
subcommand there as well.
  • Loading branch information
fmuellner committed Dec 19, 2020
1 parent 894625e commit f945aca
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cmd/issue_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 issueReopenCmd = &cobra.Command{
Use: "reopen [remote] <id>",
Short: "Reopen a closed issue",
Long: ``,
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, id, err := parseArgsRemoteAndID(args)
if err != nil {
log.Fatal(err)
}

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

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

func init() {
issueCmd.AddCommand(issueReopenCmd)
carapace.Gen(mrReopenCmd).PositionalCompletion(
action.Remotes(),
action.Issues(issueList),
)
}
54 changes: 54 additions & 0 deletions cmd/issue_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_issueCloseReopen(t *testing.T) {
tests := []struct {
desc string
opt string
expected string
}{
{
desc: "reopen-open",
opt: "reopen",
expected: "issue not closed",
},
{
desc: "close-open",
opt: "close",
expected: "Issue #1 closed",
},
{
desc: "close-closed",
opt: "close",
expected: "issue already closed",
},
{
desc: "reopen-closed",
opt: "reopen",
expected: "Issue #1 reopened",
},
}

repo := copyTestRepo(t)
for _, test := range tests {
test := test
t.Run(test.desc, func(t *testing.T) {
cmd := exec.Command(labBinaryPath, "issue", test.opt, "1")
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 @@ -604,6 +604,24 @@ func IssueClose(pid interface{}, id int) error {
return nil
}

// IssueReopen reopens a closed issue
func IssueReopen(pid interface{}, id int) error {
issue, _, err := lab.Issues.GetIssue(pid, id)
if err != nil {
return err
}
if issue.State == "opened" {
return fmt.Errorf("issue not closed")
}
_, _, err = lab.Issues.UpdateIssue(pid, id, &gitlab.UpdateIssueOptions{
StateEvent: gitlab.String("reopen"),
})
if err != nil {
return err
}
return nil
}

// IssueListDiscussions retrieves the discussions (aka notes & comments) for an issue
func IssueListDiscussions(project string, issueNum int) ([]*gitlab.Discussion, error) {
p, err := FindProject(project)
Expand Down

0 comments on commit f945aca

Please sign in to comment.