From 3677273a1d6c029eea0709651326b39f738b7aac Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 18 Sep 2022 18:52:04 +0200 Subject: [PATCH 1/8] refactor --- models/organization/team.go | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/models/organization/team.go b/models/organization/team.go index 2d5ee172724bb..9556dee6ef6a6 100644 --- a/models/organization/team.go +++ b/models/organization/team.go @@ -123,35 +123,20 @@ func (opts *SearchTeamOptions) toCond() builder.Cond { func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) { sess := db.GetEngine(db.DefaultContext) - opts.SetDefaultValues() cond := opts.toCond() if opts.UserID > 0 { sess = sess.Join("INNER", "team_user", "team_user.team_id = team.id") } - count, err := sess. - Where(cond). - Count(new(Team)) - if err != nil { - return nil, 0, err - } - - if opts.UserID > 0 { - sess = sess.Join("INNER", "team_user", "team_user.team_id = team.id") - } - - if opts.PageSize == -1 { - opts.PageSize = int(count) - } else { - sess = sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize) + if opts.PageSize != -1 { + opts.SetDefaultValues() + sess = db.SetSessionPagination(sess, opts) } teams := make([]*Team, 0, opts.PageSize) - if err = sess. - Where(cond). - OrderBy("lower_name"). - Find(&teams); err != nil { + count, err := sess.Where(cond).OrderBy("lower_name").FindAndCount(&teams) + if err != nil { return nil, 0, err } From 8bd5089b3045d276ab357aa832d275879fcb831c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 18 Sep 2022 19:17:54 +0200 Subject: [PATCH 2/8] Add TestAPIOrgSearchEmptyTeam --- tests/integration/api_org_test.go | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/integration/api_org_test.go b/tests/integration/api_org_test.go index 70bb17bee2e10..13033b97c6a9a 100644 --- a/tests/integration/api_org_test.go +++ b/tests/integration/api_org_test.go @@ -5,6 +5,7 @@ package integration import ( + "fmt" "net/http" "net/url" "strings" @@ -151,3 +152,37 @@ func TestAPIGetAll(t *testing.T) { assert.Equal(t, "org25", apiOrgList[0].FullName) assert.Equal(t, "public", apiOrgList[0].Visibility) } + +func TestAPIOrgSearchEmptyTeam(t *testing.T) { + onGiteaRun(t, func(*testing.T, *url.URL) { + token := getUserToken(t, "user1") + orgName := "org_with_empty_team" + + // create org + req := NewRequestWithJSON(t, "POST", "/api/v1/orgs?token="+token, &api.CreateOrgOption{ + UserName: orgName, + }) + MakeRequest(t, req, http.StatusCreated) + + // create team with no member + req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/orgs/%s/teams?token=%s", orgName, token), &api.CreateTeamOption{ + Name: "Lehr", + IncludesAllRepositories: true, + Permission: "read", + }) + MakeRequest(t, req, http.StatusCreated) + + // search for team with no member + req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/orgs/%s/teams/search?q=%s&token=%s", orgName, "lehr", token)) + resp := MakeRequest(t, req, http.StatusOK) + data := struct { + Ok bool + Data []*api.Team + }{} + DecodeJSON(t, resp, &data) + assert.True(t, data.Ok) + if assert.Len(t, data.Data, 1) { + assert.EqualValues(t, "Lehr", data.Data[0].Name) + } + }) +} From c5916081f01a0f7b1aaf7b52c39526f1edad0b77 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 18 Sep 2022 19:22:22 +0200 Subject: [PATCH 3/8] fix --- routers/api/v1/org/team.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index c891d0e122ccb..f3e7834a4931c 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -759,13 +759,17 @@ func SearchTeam(ctx *context.APIContext) { listOptions := utils.GetListOptions(ctx) opts := &organization.SearchTeamOptions{ - UserID: ctx.Doer.ID, Keyword: ctx.FormTrim("q"), OrgID: ctx.Org.Organization.ID, IncludeDesc: ctx.FormString("include_desc") == "" || ctx.FormBool("include_desc"), ListOptions: listOptions, } + // Only admin is allowd to search for all teams + if !ctx.Doer.IsAdmin { + opts.UserID = ctx.Doer.ID + } + teams, maxResults, err := organization.SearchTeam(opts) if err != nil { log.Error("SearchTeam failed: %v", err) From 2f82d3a2480feb0f8c38cd490868439588a27626 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 18 Sep 2022 19:23:03 +0200 Subject: [PATCH 4/8] fmt --- build/generate-go-licenses.go | 4 ++-- modules/markup/markdown/meta.go | 1 + modules/markup/markdown/renderconfig.go | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/build/generate-go-licenses.go b/build/generate-go-licenses.go index fedfdc315e43a..87c773ed8b0cd 100644 --- a/build/generate-go-licenses.go +++ b/build/generate-go-licenses.go @@ -64,8 +64,8 @@ func main() { } entries = append(entries, LicenseEntry{ - Name: name, - Path: path, + Name: name, + Path: path, LicenseText: string(licenseText), }) } diff --git a/modules/markup/markdown/meta.go b/modules/markup/markdown/meta.go index 28913fd68457f..b08121e8680c2 100644 --- a/modules/markup/markdown/meta.go +++ b/modules/markup/markdown/meta.go @@ -11,6 +11,7 @@ import ( "unicode/utf8" "code.gitea.io/gitea/modules/log" + "gopkg.in/yaml.v3" ) diff --git a/modules/markup/markdown/renderconfig.go b/modules/markup/markdown/renderconfig.go index 6a3b3a1bde8b4..003579115fc0c 100644 --- a/modules/markup/markdown/renderconfig.go +++ b/modules/markup/markdown/renderconfig.go @@ -8,6 +8,7 @@ import ( "strings" "code.gitea.io/gitea/modules/log" + "github.com/yuin/goldmark/ast" "gopkg.in/yaml.v3" ) From 28c4ab001ad6a1873d74ca8115a033b5cd92ce24 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 18 Sep 2022 19:30:46 +0200 Subject: [PATCH 5/8] fix test --- tests/integration/api_org_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/api_org_test.go b/tests/integration/api_org_test.go index 13033b97c6a9a..d9f4a99ee7ce5 100644 --- a/tests/integration/api_org_test.go +++ b/tests/integration/api_org_test.go @@ -169,6 +169,7 @@ func TestAPIOrgSearchEmptyTeam(t *testing.T) { Name: "Lehr", IncludesAllRepositories: true, Permission: "read", + Units: []string{"repo.code", "repo.issues", "repo.ext_issues", "repo.wiki", "repo.pulls"}, }) MakeRequest(t, req, http.StatusCreated) From 654c60a2e38723bfba6f56b54e13594502aee630 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 19 Sep 2022 12:29:49 +0200 Subject: [PATCH 6/8] =?UTF-8?q?~~=F0=9F=A5=9A~~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/integration/api_org_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/integration/api_org_test.go b/tests/integration/api_org_test.go index d9f4a99ee7ce5..c225b1952b5fa 100644 --- a/tests/integration/api_org_test.go +++ b/tests/integration/api_org_test.go @@ -166,7 +166,7 @@ func TestAPIOrgSearchEmptyTeam(t *testing.T) { // create team with no member req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/orgs/%s/teams?token=%s", orgName, token), &api.CreateTeamOption{ - Name: "Lehr", + Name: "Empty", IncludesAllRepositories: true, Permission: "read", Units: []string{"repo.code", "repo.issues", "repo.ext_issues", "repo.wiki", "repo.pulls"}, @@ -174,7 +174,7 @@ func TestAPIOrgSearchEmptyTeam(t *testing.T) { MakeRequest(t, req, http.StatusCreated) // search for team with no member - req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/orgs/%s/teams/search?q=%s&token=%s", orgName, "lehr", token)) + req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/orgs/%s/teams/search?q=%s&token=%s", orgName, "empty", token)) resp := MakeRequest(t, req, http.StatusOK) data := struct { Ok bool @@ -183,7 +183,7 @@ func TestAPIOrgSearchEmptyTeam(t *testing.T) { DecodeJSON(t, resp, &data) assert.True(t, data.Ok) if assert.Len(t, data.Data, 1) { - assert.EqualValues(t, "Lehr", data.Data[0].Name) + assert.EqualValues(t, "Empty", data.Data[0].Name) } }) } From 92c5f273bbb1c73d9d4e3ca09fb54d8fb399a739 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 19 Sep 2022 12:32:54 +0200 Subject: [PATCH 7/8] Update tests/integration/api_org_test.go --- tests/integration/api_org_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/api_org_test.go b/tests/integration/api_org_test.go index c225b1952b5fa..4b8c5c97a8a56 100644 --- a/tests/integration/api_org_test.go +++ b/tests/integration/api_org_test.go @@ -173,7 +173,7 @@ func TestAPIOrgSearchEmptyTeam(t *testing.T) { }) MakeRequest(t, req, http.StatusCreated) - // search for team with no member + // case-insensitive search for teams that have no members req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/orgs/%s/teams/search?q=%s&token=%s", orgName, "empty", token)) resp := MakeRequest(t, req, http.StatusOK) data := struct { From cc87add70fade34e93c25616f8fb424d0bfc23c4 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 19 Sep 2022 13:34:48 +0200 Subject: [PATCH 8/8] enforce pagination --- models/organization/team.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/models/organization/team.go b/models/organization/team.go index 9556dee6ef6a6..bd80b1a8c7888 100644 --- a/models/organization/team.go +++ b/models/organization/team.go @@ -123,16 +123,13 @@ func (opts *SearchTeamOptions) toCond() builder.Cond { func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) { sess := db.GetEngine(db.DefaultContext) + opts.SetDefaultValues() cond := opts.toCond() if opts.UserID > 0 { sess = sess.Join("INNER", "team_user", "team_user.team_id = team.id") } - - if opts.PageSize != -1 { - opts.SetDefaultValues() - sess = db.SetSessionPagination(sess, opts) - } + sess = db.SetSessionPagination(sess, opts) teams := make([]*Team, 0, opts.PageSize) count, err := sess.Where(cond).OrderBy("lower_name").FindAndCount(&teams)