-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
edit_common: add file for edit* functions
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
Showing
4 changed files
with
146 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters