Skip to content

Commit 311c83a

Browse files
svarlamovlunny
authored andcommitted
Allow adding collaborators with (fullname) (#3103)
* Allow adding collaborators with (fullname) Signed-off-by: Sasha Varlamov <[email protected]> * Refactor username suffix to utils pkg Signed-off-by: Sasha Varlamov <[email protected]>
1 parent 7ec6cdd commit 311c83a

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

routers/org/teams.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/modules/base"
1616
"code.gitea.io/gitea/modules/context"
1717
"code.gitea.io/gitea/modules/log"
18+
"code.gitea.io/gitea/routers/utils"
1819
)
1920

2021
const (
@@ -76,11 +77,7 @@ func TeamsAction(ctx *context.Context) {
7677
ctx.Error(404)
7778
return
7879
}
79-
uname := ctx.Query("uname")
80-
// uname may be formatted as "username (fullname)"
81-
if strings.Contains(uname, "(") && strings.HasSuffix(uname, ")") {
82-
uname = strings.TrimSpace(strings.Split(uname, "(")[0])
83-
}
80+
uname := utils.RemoveUsernameParameterSuffix(strings.ToLower(ctx.Query("uname")))
8481
var u *models.User
8582
u, err = models.GetUserByName(uname)
8683
if err != nil {

routers/repo/setting.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/context"
1717
"code.gitea.io/gitea/modules/log"
1818
"code.gitea.io/gitea/modules/setting"
19+
"code.gitea.io/gitea/routers/utils"
1920
)
2021

2122
const (
@@ -366,7 +367,7 @@ func Collaboration(ctx *context.Context) {
366367

367368
// CollaborationPost response for actions for a collaboration of a repository
368369
func CollaborationPost(ctx *context.Context) {
369-
name := strings.ToLower(ctx.Query("collaborator"))
370+
name := utils.RemoveUsernameParameterSuffix(strings.ToLower(ctx.Query("collaborator")))
370371
if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
371372
ctx.Redirect(setting.AppSubURL + ctx.Req.URL.Path)
372373
return

routers/utils/utils.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package utils
6+
7+
import (
8+
"strings"
9+
)
10+
11+
// RemoveUsernameParameterSuffix returns the username parameter without the (fullname) suffix - leaving just the username
12+
func RemoveUsernameParameterSuffix(name string) string {
13+
if index := strings.Index(name, " ("); index >= 0 {
14+
name = name[:index]
15+
}
16+
return name
17+
}

routers/utils/utils_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package utils
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestRemoveUsernameParameterSuffix(t *testing.T) {
14+
assert.Equal(t, "foobar", RemoveUsernameParameterSuffix("foobar (Foo Bar)"))
15+
assert.Equal(t, "foobar", RemoveUsernameParameterSuffix("foobar"))
16+
assert.Equal(t, "", RemoveUsernameParameterSuffix(""))
17+
}

0 commit comments

Comments
 (0)