Skip to content

Commit

Permalink
edit_common: add file for edit* functions
Browse files Browse the repository at this point in the history
These functions were being used in different places(commands), thus having them
declared in other place (a common file) other than in a specific issue_ command
seems better.

Signed-off-by: Bruno Meneguele <[email protected]>
  • Loading branch information
bmeneg committed Jan 25, 2021
1 parent e38ac47 commit ee8227e
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 131 deletions.
68 changes: 68 additions & 0 deletions cmd/edit_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd

import (
"strings"

"github.com/zaquestion/lab/internal/git"
)

// editGetLabels returns a string slice of labels based on the current
// labels and flags from the command line, and a bool indicating whether
// the labels have changed
func editGetLabels(idLabels []string, labels []string, unlabels []string) ([]string, bool, error) {
// add the new labels to the current labels, then remove the "unlabels"
labels = difference(union(idLabels, labels), unlabels)

return labels, !same(idLabels, labels), nil
}

// GetUpdateAssignees returns an int slice of assignee IDs based on the
// current assignees and flags from the command line, and a bool
// indicating whether the assignees have changed
func getUpdateAssignees(currentAssignees []string, assignees []string, unassignees []string) ([]int, bool, error) {
// add the new assignees to the current assignees, then remove the "unassignees"
assignees = difference(union(currentAssignees, assignees), unassignees)
assigneesChanged := !same(currentAssignees, assignees)

// turn the new assignee list into a list of assignee IDs
var assigneeIDs []int
if assigneesChanged && len(assignees) == 0 {
// if we're removing all assignees, we have to use []int{0}
// see https://github.com/xanzy/go-gitlab/issues/427
assigneeIDs = []int{0}
} else {
assigneeIDs = make([]int, len(assignees))
for i, a := range assignees {
assigneeIDs[i] = *getAssigneeID(a)
}
}

return assigneeIDs, assigneesChanged, nil
}

// editGetTitleDescription returns a title and description based on the current
// issue title and description and various flags from the command line
func editGetTitleDescription(title string, body string, msgs []string, nFlag int) (string, string, error) {
if len(msgs) > 0 {
title = msgs[0]

if len(msgs) > 1 {
body = strings.Join(msgs[1:], "\n\n")
}

// we have everything we need
return title, body, nil
}

// if other flags were given (eg label), then skip the editor and return
// what we already have
if nFlag != 0 {
return title, body, nil
}

text, err := editText(title, body)
if err != nil {
return "", "", err
}
return git.Edit("EDIT", text)
}
78 changes: 78 additions & 0 deletions cmd/edit_common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cmd

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
gitlab "github.com/xanzy/go-gitlab"
)

func Test_editGetTitleAndDescription(t *testing.T) {
tests := []struct {
Name string
Issue *gitlab.Issue
Args []string
ExpectedTitle string
ExpectedDescription string
}{
{
Name: "Using messages",
Issue: &gitlab.Issue{
Title: "old title",
Description: "old body",
},
Args: []string{"new title", "new body 1", "new body 2"},
ExpectedTitle: "new title",
ExpectedDescription: "new body 1\n\nnew body 2",
},
{
Name: "Using a single message",
Issue: &gitlab.Issue{
Title: "old title",
Description: "old body",
},
Args: []string{"new title"},
ExpectedTitle: "new title",
ExpectedDescription: "old body",
},
{
Name: "From Editor",
Issue: &gitlab.Issue{
Title: "old title",
Description: "old body",
},
Args: nil,
ExpectedTitle: "old title",
ExpectedDescription: "old body",
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
test := test
t.Parallel()
title, body, err := editGetTitleDescription(test.Issue.Title, test.Issue.Description, test.Args, len(test.Args))
if err != nil {
t.Fatal(err)
}

assert.Equal(t, test.ExpectedTitle, title)
assert.Equal(t, test.ExpectedDescription, body)
})
}
}

func Test_editText(t *testing.T) {
t.Parallel()
text, err := editText("old title", "old body")
if err != nil {
t.Fatal(err)
}
require.Equal(t, `old title
old body
# Edit the title and/or description. The first block of text
# is the title and the rest is the description.`, text)

}
61 changes: 0 additions & 61 deletions cmd/issue_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,6 @@ lab issue edit <id>:<comment_id> # update a comment on MR`,
},
}

// editGetLabels returns a string slice of labels based on the current
// labels and flags from the command line, and a bool indicating whether
// the labels have changed
func editGetLabels(idLabels []string, labels []string, unlabels []string) ([]string, bool, error) {
// add the new labels to the current labels, then remove the "unlabels"
labels = difference(union(idLabels, labels), unlabels)

return labels, !same(idLabels, labels), nil
}

// issueGetCurrentAssignees returns a string slice of the current assignees'
// usernames
func issueGetCurrentAssignees(issue *gitlab.Issue) []string {
Expand All @@ -194,57 +184,6 @@ func issueGetCurrentAssignees(issue *gitlab.Issue) []string {
return currentAssignees
}

// GetUpdateAssignees returns an int slice of assignee IDs based on the
// current assignees and flags from the command line, and a bool
// indicating whether the assignees have changed
func getUpdateAssignees(currentAssignees []string, assignees []string, unassignees []string) ([]int, bool, error) {
// add the new assignees to the current assignees, then remove the "unassignees"
assignees = difference(union(currentAssignees, assignees), unassignees)
assigneesChanged := !same(currentAssignees, assignees)

// turn the new assignee list into a list of assignee IDs
var assigneeIDs []int
if assigneesChanged && len(assignees) == 0 {
// if we're removing all assignees, we have to use []int{0}
// see https://github.com/xanzy/go-gitlab/issues/427
assigneeIDs = []int{0}
} else {
assigneeIDs = make([]int, len(assignees))
for i, a := range assignees {
assigneeIDs[i] = *getAssigneeID(a)
}
}

return assigneeIDs, assigneesChanged, nil
}

// editGetTitleDescription returns a title and description based on the current
// issue title and description and various flags from the command line
func editGetTitleDescription(title string, body string, msgs []string, nFlag int) (string, string, error) {
if len(msgs) > 0 {
title = msgs[0]

if len(msgs) > 1 {
body = strings.Join(msgs[1:], "\n\n")
}

// we have everything we need
return title, body, nil
}

// if other flags were given (eg label), then skip the editor and return
// what we already have
if nFlag != 0 {
return title, body, nil
}

text, err := editText(title, body)
if err != nil {
return "", "", err
}
return git.Edit("EDIT", text)
}

// editText returns an issue editing template that is suitable for loading
// into an editor
func editText(title string, body string) (string, error) {
Expand Down
70 changes: 0 additions & 70 deletions cmd/issue_edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
gitlab "github.com/xanzy/go-gitlab"
)

// issueEditCmdTestCreateIssue creates an issue and returns the issue number
Expand Down Expand Up @@ -127,75 +126,6 @@ func Test_issueEditAssignees(t *testing.T) {
require.NotContains(t, issueShowOuput, "Assignees: lab-testing")
}

func Test_issueEditGetTitleAndDescription(t *testing.T) {
tests := []struct {
Name string
Issue *gitlab.Issue
Args []string
ExpectedTitle string
ExpectedDescription string
}{
{
Name: "Using messages",
Issue: &gitlab.Issue{
Title: "old title",
Description: "old body",
},
Args: []string{"new title", "new body 1", "new body 2"},
ExpectedTitle: "new title",
ExpectedDescription: "new body 1\n\nnew body 2",
},
{
Name: "Using a single message",
Issue: &gitlab.Issue{
Title: "old title",
Description: "old body",
},
Args: []string{"new title"},
ExpectedTitle: "new title",
ExpectedDescription: "old body",
},
{
Name: "From Editor",
Issue: &gitlab.Issue{
Title: "old title",
Description: "old body",
},
Args: nil,
ExpectedTitle: "old title",
ExpectedDescription: "old body",
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
test := test
t.Parallel()
title, body, err := editGetTitleDescription(test.Issue.Title, test.Issue.Description, test.Args, len(test.Args))
if err != nil {
t.Fatal(err)
}

assert.Equal(t, test.ExpectedTitle, title)
assert.Equal(t, test.ExpectedDescription, body)
})
}
}

func Test_issueEditText(t *testing.T) {
t.Parallel()
text, err := editText("old title", "old body")
if err != nil {
t.Fatal(err)
}
require.Equal(t, `old title
old body
# Edit the title and/or description. The first block of text
# is the title and the rest is the description.`, text)

}

func Test_issueEditSame(t *testing.T) {
t.Parallel()
assert.True(t, same([]string{}, []string{}))
Expand Down

0 comments on commit ee8227e

Please sign in to comment.