From afade2f0c74b5fe77656640e361c573135561d96 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Oct 2024 10:59:08 -0700 Subject: [PATCH] Fix test --- models/org_team_test.go | 4 ++-- modules/structs/org_team.go | 2 +- services/forms/org.go | 2 +- services/org/team.go | 32 ++++++++++++++++++++------------ 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/models/org_team_test.go b/models/org_team_test.go index 7881d8f4a2..8e9d91dbfd 100644 --- a/models/org_team_test.go +++ b/models/org_team_test.go @@ -84,7 +84,7 @@ func TestUpdateTeam(t *testing.T) { team.Name = "newName" team.Description = strings.Repeat("A long description!", 100) team.AccessMode = perm.AccessModeAdmin - assert.NoError(t, UpdateTeam(db.DefaultContext, team, "authorize")) + assert.NoError(t, UpdateTeam(db.DefaultContext, team, "name", "lower_name", "description", "authorize")) team = unittest.AssertExistsAndLoadBean(t, &organization.Team{Name: "newName"}) assert.True(t, strings.HasPrefix(team.Description, "A long description!")) @@ -103,7 +103,7 @@ func TestUpdateTeam2(t *testing.T) { team.LowerName = "owners" team.Name = "Owners" team.Description = strings.Repeat("A long description!", 100) - err := UpdateTeam(db.DefaultContext, team, "authorize") + err := UpdateTeam(db.DefaultContext, team, "name", "lower_name", "description", "authorize") assert.True(t, organization.IsErrTeamAlreadyExist(err)) unittest.CheckConsistencyFor(t, &organization.Team{ID: team.ID}) diff --git a/modules/structs/org_team.go b/modules/structs/org_team.go index e906a51ba3..f8899b236b 100644 --- a/modules/structs/org_team.go +++ b/modules/structs/org_team.go @@ -44,7 +44,7 @@ type EditTeamOption struct { Description *string `json:"description" binding:"MaxSize(255)"` IncludesAllRepositories *bool `json:"includes_all_repositories"` // enum: read,write,admin - Permission string `json:"permission" binding:"Required;In(read,write,admin)"` + Permission string `json:"permission"` // example: ["repo.code","repo.issues","repo.ext_issues","repo.wiki","repo.pulls","repo.releases","repo.projects","repo.ext_wiki"] // Deprecated: This variable should be replaced by UnitsMap and will be dropped in later versions. Units []string `json:"units"` diff --git a/services/forms/org.go b/services/forms/org.go index a6df8aadee..4d85897e90 100644 --- a/services/forms/org.go +++ b/services/forms/org.go @@ -57,7 +57,7 @@ func (f *UpdateOrgSettingForm) Validate(req *http.Request, errs binding.Errors) type CreateTeamForm struct { TeamName string `binding:"Required;AlphaDashDot;MaxSize(255)"` Description string `binding:"MaxSize(255)"` - Permission string `binding:"Required;In(read,write,admin)"` + Permission string `binding:"Required;In(,read,write,admin)"` RepoAccess string `binding:"Required;In(specified, all)"` CanCreateOrgRepo bool } diff --git a/services/org/team.go b/services/org/team.go index f4b0d76324..26389bcf69 100644 --- a/services/org/team.go +++ b/services/org/team.go @@ -5,6 +5,7 @@ package org import ( "context" + "strings" "code.gitea.io/gitea/models" org_model "code.gitea.io/gitea/models/organization" @@ -34,7 +35,12 @@ func UpdateTeam(ctx context.Context, team *org_model.Team, opts UpdateTeamOption } if !team.IsOwnerTeam() { - team.Name = opts.TeamName + if team.Name != opts.TeamName { + team.Name = opts.TeamName + team.LowerName = strings.ToLower(opts.TeamName) + changedCols = append(changedCols, "name", "lower_name") + } + if team.AccessMode != newAccessMode { team.AccessMode = newAccessMode changedCols = append(changedCols, "authorize") @@ -44,22 +50,24 @@ func UpdateTeam(ctx context.Context, team *org_model.Team, opts UpdateTeamOption team.IncludesAllRepositories = opts.IncludesAllRepositories changedCols = append(changedCols, "includes_all_repositories") } - units := make([]*org_model.TeamUnit, 0, len(opts.UnitPerms)) - for tp, perm := range opts.UnitPerms { - units = append(units, &org_model.TeamUnit{ - OrgID: team.OrgID, - TeamID: team.ID, - Type: tp, - AccessMode: perm, - }) + if len(opts.UnitPerms) > 0 { + units := make([]*org_model.TeamUnit, 0, len(opts.UnitPerms)) + for tp, perm := range opts.UnitPerms { + units = append(units, &org_model.TeamUnit{ + OrgID: team.OrgID, + TeamID: team.ID, + Type: tp, + AccessMode: perm, + }) + } + team.Units = units + changedCols = append(changedCols, "units") } - team.Units = units - changedCols = append(changedCols, "units") if team.CanCreateOrgRepo != opts.CanCreateOrgRepo { team.CanCreateOrgRepo = opts.CanCreateOrgRepo changedCols = append(changedCols, "can_create_org_repo") } - } else { + } else { // make the possible legacy data correct, we force to update these fields team.CanCreateOrgRepo = true team.IncludesAllRepositories = true changedCols = append(changedCols, "can_create_org_repo", "includes_all_repositories")