Skip to content

Commit

Permalink
cmd: Rename Assignee functions to general User functions
Browse files Browse the repository at this point in the history
The Assignee ID functions are really User ID lookup functions and can be used
by other features to look up user IDs.

Signed-off-by: Prarit Bhargava <[email protected]>
  • Loading branch information
prarit committed Mar 24, 2021
1 parent 9beaf59 commit ac1fdb8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
34 changes: 17 additions & 17 deletions cmd/edit_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ func editGetLabels(idLabels []string, labels []string, unlabels []string) ([]str
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}
// GetUpdateUsers returns an int slice of user IDs based on the
// current users and flags from the command line, and a bool
// indicating whether the users have changed
func getUpdateUsers(currentUsers []string, users []string, remove []string) ([]int, bool, error) {
// add the new users to the current users, then remove the "remove" group
users = difference(union(currentUsers, users), remove)
usersChanged := !same(currentUsers, users)

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

return assigneeIDs, assigneesChanged, nil
return userIDs, usersChanged, nil
}

// editGetTitleDescription returns a title and description based on the
Expand Down
2 changes: 1 addition & 1 deletion cmd/issue_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ var issueCreateCmd = &cobra.Command{

assigneeIDs := make([]int, len(assignees))
for i, a := range assignees {
assigneeIDs[i] = *getAssigneeID(a)
assigneeIDs[i] = *getUserID(a)
}

issueURL, err := lab.IssueCreate(rn, &gitlab.CreateIssueOptions{
Expand Down
2 changes: 1 addition & 1 deletion cmd/issue_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ lab issue edit <id>:<comment_id> # update a comment on MR`,
}

currentAssignees := issueGetCurrentAssignees(issue)
assigneeIDs, assigneesChanged, err := getUpdateAssignees(currentAssignees, assignees, unassignees)
assigneeIDs, assigneesChanged, err := getUpdateUsers(currentAssignees, assignees, unassignees)
if err != nil {
log.Fatal(err)
}
Expand Down
27 changes: 13 additions & 14 deletions cmd/mr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,29 @@ func init() {
)
}

// getAssignee returns the assigneeID for use with other GitLab API calls.
// NOTE: It is also used by issue_create.go
func getAssigneeID(assignee string) *int {
if assignee == "" {
// getUser returns the userID for use with other GitLab API calls.
func getUserID(user string) *int {
if user == "" {
return nil
}
if assignee[0] == '@' {
assignee = assignee[1:]
if user[0] == '@' {
user = user[1:]
}
assigneeID, err := lab.UserIDFromUsername(assignee)
userID, err := lab.UserIDFromUsername(user)
if err != nil {
return nil
}
if assigneeID == -1 {
if userID == -1 {
return nil
}
return gitlab.Int(assigneeID)
return gitlab.Int(userID)
}

// getAssignees returns the assigneeIDs for use with other GitLab API calls.
func getAssigneeIDs(assignees []string) []int {
// getUsers returns the userIDs for use with other GitLab API calls.
func getUserIDs(users []string) []int {
var ids []int
for _, a := range assignees {
ids = append(ids, *getAssigneeID(a))
for _, a := range users {
ids = append(ids, *getUserID(a))
}
return ids
}
Expand Down Expand Up @@ -282,7 +281,7 @@ func runMRCreate(cmd *cobra.Command, args []string) {
TargetProjectID: &targetProject.ID,
Title: &title,
Description: &body,
AssigneeIDs: getAssigneeIDs(assignees),
AssigneeIDs: getUserIDs(assignees),
RemoveSourceBranch: &removeSourceBranch,
Squash: &squash,
AllowCollaboration: &allowCollaboration,
Expand Down
2 changes: 1 addition & 1 deletion cmd/mr_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ lab MR edit <id>:<comment_id> # update a comment on MR`,
}

currentAssignees := mrGetCurrentAssignees(mr)
assigneeIDs, assigneesChanged, err := getUpdateAssignees(currentAssignees, assignees, unassignees)
assigneeIDs, assigneesChanged, err := getUpdateUsers(currentAssignees, assignees, unassignees)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit ac1fdb8

Please sign in to comment.