From e893aced89ed78eec98d1da5686f8cc8216925a8 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Tue, 12 Dec 2017 09:23:08 +0200 Subject: [PATCH] Allow adding collaborators with (fullname) (#3103) (#3168) * Allow adding collaborators with (fullname) Signed-off-by: Sasha Varlamov * Refactor username suffix to utils pkg Signed-off-by: Sasha Varlamov --- routers/org/teams.go | 7 ++----- routers/repo/setting.go | 3 ++- routers/utils/utils.go | 17 +++++++++++++++++ routers/utils/utils_test.go | 17 +++++++++++++++++ 4 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 routers/utils/utils.go create mode 100644 routers/utils/utils_test.go diff --git a/routers/org/teams.go b/routers/org/teams.go index e9abfe791..7f8b828e8 100644 --- a/routers/org/teams.go +++ b/routers/org/teams.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/routers/utils" ) const ( @@ -76,11 +77,7 @@ func TeamsAction(ctx *context.Context) { ctx.Error(404) return } - uname := ctx.Query("uname") - // uname may be formatted as "username (fullname)" - if strings.Contains(uname, "(") && strings.HasSuffix(uname, ")") { - uname = strings.TrimSpace(strings.Split(uname, "(")[0]) - } + uname := utils.RemoveUsernameParameterSuffix(strings.ToLower(ctx.Query("uname"))) var u *models.User u, err = models.GetUserByName(uname) if err != nil { diff --git a/routers/repo/setting.go b/routers/repo/setting.go index ebe1c7cd9..5105c7ff2 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/routers/utils" ) const ( @@ -366,7 +367,7 @@ func Collaboration(ctx *context.Context) { // CollaborationPost response for actions for a collaboration of a repository func CollaborationPost(ctx *context.Context) { - name := strings.ToLower(ctx.Query("collaborator")) + name := utils.RemoveUsernameParameterSuffix(strings.ToLower(ctx.Query("collaborator"))) if len(name) == 0 || ctx.Repo.Owner.LowerName == name { ctx.Redirect(setting.AppSubURL + ctx.Req.URL.Path) return diff --git a/routers/utils/utils.go b/routers/utils/utils.go new file mode 100644 index 000000000..6ead7d60e --- /dev/null +++ b/routers/utils/utils.go @@ -0,0 +1,17 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package utils + +import ( + "strings" +) + +// RemoveUsernameParameterSuffix returns the username parameter without the (fullname) suffix - leaving just the username +func RemoveUsernameParameterSuffix(name string) string { + if index := strings.Index(name, " ("); index >= 0 { + name = name[:index] + } + return name +} diff --git a/routers/utils/utils_test.go b/routers/utils/utils_test.go new file mode 100644 index 000000000..fb56ac85c --- /dev/null +++ b/routers/utils/utils_test.go @@ -0,0 +1,17 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package utils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRemoveUsernameParameterSuffix(t *testing.T) { + assert.Equal(t, "foobar", RemoveUsernameParameterSuffix("foobar (Foo Bar)")) + assert.Equal(t, "foobar", RemoveUsernameParameterSuffix("foobar")) + assert.Equal(t, "", RemoveUsernameParameterSuffix("")) +}