Use UpdateTeamOptions instead of multiple parameters of function

This commit is contained in:
Lunny Xiao 2024-10-23 11:03:53 -07:00
parent df785f6200
commit 8e1cac1185
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
3 changed files with 37 additions and 26 deletions

View File

@ -320,14 +320,14 @@ func EditTeam(ctx *context.APIContext) {
} }
} }
if err := org_service.UpdateTeam(ctx, team, if err := org_service.UpdateTeam(ctx, team, org_service.UpdateTeamOptions{
teamName, TeamName: teamName,
description, Description: description,
form.Permission == "admin", IsAdmin: form.Permission == "admin",
includeAllRepos, IncludesAllRepositories: includeAllRepos,
canCreateOrgRepo, CanCreateOrgRepo: canCreateOrgRepo,
unitPerms, UnitPerms: unitPerms,
); err != nil { }); err != nil {
ctx.Error(http.StatusInternalServerError, "EditTeam", err) ctx.Error(http.StatusInternalServerError, "EditTeam", err)
return return
} }

View File

@ -494,12 +494,14 @@ func EditTeamPost(ctx *context.Context) {
return return
} }
if err := org_service.UpdateTeam(ctx, t, form.TeamName, form.Description, if err := org_service.UpdateTeam(ctx, t, org_service.UpdateTeamOptions{
form.Permission == "admin", TeamName: form.TeamName,
form.RepoAccess == "all", Description: form.Description,
form.CanCreateOrgRepo, IsAdmin: form.Permission == "admin",
unitPerms, IncludesAllRepositories: form.RepoAccess == "all",
); err != nil { CanCreateOrgRepo: form.CanCreateOrgRepo,
UnitPerms: unitPerms,
}); err != nil {
ctx.Data["Err_TeamName"] = true ctx.Data["Err_TeamName"] = true
switch { switch {
case org_model.IsErrTeamAlreadyExist(err): case org_model.IsErrTeamAlreadyExist(err):

View File

@ -12,31 +12,40 @@ import (
unit_model "code.gitea.io/gitea/models/unit" unit_model "code.gitea.io/gitea/models/unit"
) )
func UpdateTeam(ctx context.Context, team *org_model.Team, teamName, description string, isAdmin, includesAllRepositories, canCreateOrgRepo bool, unitPerms map[unit_model.Type]perm.AccessMode) error { type UpdateTeamOptions struct {
TeamName string
Description string
IsAdmin bool
IncludesAllRepositories bool
CanCreateOrgRepo bool
UnitPerms map[unit_model.Type]perm.AccessMode
}
func UpdateTeam(ctx context.Context, team *org_model.Team, opts UpdateTeamOptions) error {
var changedCols []string var changedCols []string
newAccessMode := perm.AccessModeRead newAccessMode := perm.AccessModeRead
if isAdmin { if opts.IsAdmin {
newAccessMode = perm.AccessModeAdmin newAccessMode = perm.AccessModeAdmin
} else { } else {
// if newAccessMode is less than admin accessmode, then it should be general accessmode, // if newAccessMode is less than admin accessmode, then it should be general accessmode,
// so we should calculate the minial accessmode from units accessmodes. // so we should calculate the minial accessmode from units accessmodes.
newAccessMode = unit_model.MinUnitAccessMode(unitPerms) newAccessMode = unit_model.MinUnitAccessMode(opts.UnitPerms)
} }
if !team.IsOwnerTeam() { if !team.IsOwnerTeam() {
team.Name = teamName team.Name = opts.TeamName
if team.AccessMode != newAccessMode { if team.AccessMode != newAccessMode {
team.AccessMode = newAccessMode team.AccessMode = newAccessMode
changedCols = append(changedCols, "authorize") changedCols = append(changedCols, "authorize")
} }
if team.IncludesAllRepositories != includesAllRepositories { if team.IncludesAllRepositories != opts.IncludesAllRepositories {
team.IncludesAllRepositories = includesAllRepositories team.IncludesAllRepositories = opts.IncludesAllRepositories
changedCols = append(changedCols, "includes_all_repositories") changedCols = append(changedCols, "includes_all_repositories")
} }
units := make([]*org_model.TeamUnit, 0, len(unitPerms)) units := make([]*org_model.TeamUnit, 0, len(opts.UnitPerms))
for tp, perm := range unitPerms { for tp, perm := range opts.UnitPerms {
units = append(units, &org_model.TeamUnit{ units = append(units, &org_model.TeamUnit{
OrgID: team.OrgID, OrgID: team.OrgID,
TeamID: team.ID, TeamID: team.ID,
@ -46,8 +55,8 @@ func UpdateTeam(ctx context.Context, team *org_model.Team, teamName, description
} }
team.Units = units team.Units = units
changedCols = append(changedCols, "units") changedCols = append(changedCols, "units")
if team.CanCreateOrgRepo != canCreateOrgRepo { if team.CanCreateOrgRepo != opts.CanCreateOrgRepo {
team.CanCreateOrgRepo = canCreateOrgRepo team.CanCreateOrgRepo = opts.CanCreateOrgRepo
changedCols = append(changedCols, "can_create_org_repo") changedCols = append(changedCols, "can_create_org_repo")
} }
} else { } else {
@ -56,9 +65,9 @@ func UpdateTeam(ctx context.Context, team *org_model.Team, teamName, description
changedCols = append(changedCols, "can_create_org_repo", "includes_all_repositories") changedCols = append(changedCols, "can_create_org_repo", "includes_all_repositories")
} }
if team.Description != description { if team.Description != opts.Description {
changedCols = append(changedCols, "description") changedCols = append(changedCols, "description")
team.Description = description team.Description = opts.Description
} }
return models.UpdateTeam(ctx, team, changedCols...) return models.UpdateTeam(ctx, team, changedCols...)