Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix API Bug (fail on empty assignees) #8873

Merged
merged 23 commits into from
Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions models/issue_assignees.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package models
import (
"fmt"

"code.gitea.io/gitea/modules/util"

"xorm.io/xorm"
)

Expand Down Expand Up @@ -171,25 +173,20 @@ func toggleUserAssignee(e *xorm.Session, issue *Issue, assigneeID int64) (remove
// MakeIDsFromAPIAssigneesToAdd returns an array with all assignee IDs
func MakeIDsFromAPIAssigneesToAdd(oneAssignee string, multipleAssignees []string) (assigneeIDs []int64, err error) {

var requestAssignees []string

// Keeping the old assigning method for compatibility reasons
if oneAssignee != "" {

// Prevent double adding assignees
var isDouble bool
for _, assignee := range multipleAssignees {
if assignee == oneAssignee {
isDouble = true
break
}
}
if oneAssignee != "" && !util.IsStringInSlice(oneAssignee, multipleAssignees) {
requestAssignees = append(requestAssignees, oneAssignee)
}

if !isDouble {
multipleAssignees = append(multipleAssignees, oneAssignee)
}
//Prevent empty assignees
if len(multipleAssignees) > 0 && multipleAssignees[0] != "" {
requestAssignees = append(requestAssignees, multipleAssignees...)
}

// Get the IDs of all assignees
assigneeIDs, err = GetUserIDsByNames(multipleAssignees, false)
assigneeIDs, err = GetUserIDsByNames(requestAssignees, false)

return
}
21 changes: 21 additions & 0 deletions models/issue_assignees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,24 @@ func TestUpdateAssignee(t *testing.T) {
assert.NoError(t, err)
assert.False(t, isAssigned)
}

func TestMakeIDsFromAPIAssigneesToAdd(t *testing.T) {
IDs, err := MakeIDsFromAPIAssigneesToAdd("", []string{""})
assert.NoError(t, err)
assert.Equal(t, []int64{}, IDs)

IDs, err = MakeIDsFromAPIAssigneesToAdd("", []string{"none_existing_user"})
assert.Error(t, err)

IDs, err = MakeIDsFromAPIAssigneesToAdd("user1", []string{"user1"})
assert.NoError(t, err)
assert.Equal(t, []int64{1}, IDs)

IDs, err = MakeIDsFromAPIAssigneesToAdd("user2", []string{""})
assert.NoError(t, err)
assert.Equal(t, []int64{2}, IDs)

IDs, err = MakeIDsFromAPIAssigneesToAdd("", []string{"user1", "user2"})
assert.NoError(t, err)
assert.Equal(t, []int64{1, 2}, IDs)
}
13 changes: 13 additions & 0 deletions models/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,16 @@ func TestCreateUser_Issue5882(t *testing.T) {
assert.NoError(t, DeleteUser(v.user))
}
}

func TestGetUserIDsByNames(t *testing.T) {

//ignore non existing
IDs, err := GetUserIDsByNames([]string{"user1", "user2", "none_existing_user"}, true)
assert.NoError(t, err)
assert.Equal(t, []int64{1, 2}, IDs)

//ignore non existing
IDs, err = GetUserIDsByNames([]string{"user1", "do_not_exist"}, false)
assert.Error(t, err)
assert.Equal(t, []int64(nil), IDs)
}