diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-03-29 14:29:02 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-29 14:29:02 +0800 |
commit | b06b9a056c0af751e576978f6ef3c914ee959b9c (patch) | |
tree | aa0d11413038baa5d47af65fd435665c698fe456 /routers/api/v1/org/team.go | |
parent | d4c789dfc1c341413b77a2f21fe7339982102bed (diff) | |
download | gitea-b06b9a056c0af751e576978f6ef3c914ee959b9c.tar.gz gitea-b06b9a056c0af751e576978f6ef3c914ee959b9c.zip |
Move organization related structs into sub package (#18518)
* Move organization related structs into sub package
* Fix test
* Fix lint
* Move more functions into sub packages
* Fix bug
* Fix test
* Update models/organization/team_repo.go
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
* Apply suggestions from code review
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
* Fix fmt
* Follow suggestion from @Gusted
* Fix test
* Fix test
* Fix bug
* Use ctx but db.DefaultContext on routers
* Fix bug
* Fix bug
* fix bug
* Update models/organization/team_user.go
* Fix bug
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'routers/api/v1/org/team.go')
-rw-r--r-- | routers/api/v1/org/team.go | 60 |
1 files changed, 34 insertions, 26 deletions
diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index ca8cda269a..206012530f 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -10,6 +10,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" @@ -47,7 +48,7 @@ func ListTeams(ctx *context.APIContext) { // "200": // "$ref": "#/responses/TeamList" - teams, count, err := models.SearchOrgTeams(&models.SearchOrgTeamOptions{ + teams, count, err := organization.SearchTeam(&organization.SearchTeamOptions{ ListOptions: utils.GetListOptions(ctx), OrgID: ctx.Org.Organization.ID, }) @@ -90,7 +91,7 @@ func ListUserTeams(ctx *context.APIContext) { // "200": // "$ref": "#/responses/TeamList" - teams, count, err := models.GetUserTeams(&models.GetUserTeamOptions{ + teams, count, err := organization.SearchTeam(&organization.SearchTeamOptions{ ListOptions: utils.GetListOptions(ctx), UserID: ctx.Doer.ID, }) @@ -104,7 +105,7 @@ func ListUserTeams(ctx *context.APIContext) { for i := range teams { apiOrg, ok := cache[teams[i].OrgID] if !ok { - org, err := models.GetOrgByID(teams[i].OrgID) + org, err := organization.GetOrgByID(teams[i].OrgID) if err != nil { ctx.Error(http.StatusInternalServerError, "GetUserByID", err) return @@ -150,11 +151,11 @@ func GetTeam(ctx *context.APIContext) { ctx.JSON(http.StatusOK, convert.ToTeam(ctx.Org.Team)) } -func attachTeamUnits(team *models.Team, units []string) { +func attachTeamUnits(team *organization.Team, units []string) { unitTypes := unit_model.FindUnitTypes(units...) - team.Units = make([]*models.TeamUnit, 0, len(units)) + team.Units = make([]*organization.TeamUnit, 0, len(units)) for _, tp := range unitTypes { - team.Units = append(team.Units, &models.TeamUnit{ + team.Units = append(team.Units, &organization.TeamUnit{ OrgID: team.OrgID, Type: tp, AccessMode: team.AccessMode, @@ -170,10 +171,10 @@ func convertUnitsMap(unitsMap map[string]string) map[unit_model.Type]perm.Access return res } -func attachTeamUnitsMap(team *models.Team, unitsMap map[string]string) { - team.Units = make([]*models.TeamUnit, 0, len(unitsMap)) +func attachTeamUnitsMap(team *organization.Team, unitsMap map[string]string) { + team.Units = make([]*organization.TeamUnit, 0, len(unitsMap)) for unitKey, p := range unitsMap { - team.Units = append(team.Units, &models.TeamUnit{ + team.Units = append(team.Units, &organization.TeamUnit{ OrgID: team.OrgID, Type: unit_model.TypeFromKey(unitKey), AccessMode: perm.ParseAccessMode(p), @@ -210,7 +211,7 @@ func CreateTeam(ctx *context.APIContext) { if p < perm.AccessModeAdmin && len(form.UnitsMap) > 0 { p = unit_model.MinUnitAccessMode(convertUnitsMap(form.UnitsMap)) } - team := &models.Team{ + team := &organization.Team{ OrgID: ctx.Org.Organization.ID, Name: form.Name, Description: form.Description, @@ -231,7 +232,7 @@ func CreateTeam(ctx *context.APIContext) { } if err := models.NewTeam(team); err != nil { - if models.IsErrTeamAlreadyExist(err) { + if organization.IsErrTeamAlreadyExist(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { ctx.Error(http.StatusInternalServerError, "NewTeam", err) @@ -368,7 +369,7 @@ func GetTeamMembers(ctx *context.APIContext) { // "200": // "$ref": "#/responses/UserList" - isMember, err := models.IsOrganizationMember(ctx.Org.Team.OrgID, ctx.Doer.ID) + isMember, err := organization.IsOrganizationMember(ctx, ctx.Org.Team.OrgID, ctx.Doer.ID) if err != nil { ctx.Error(http.StatusInternalServerError, "IsOrganizationMember", err) return @@ -377,14 +378,17 @@ func GetTeamMembers(ctx *context.APIContext) { return } - if err := ctx.Org.Team.GetMembers(&models.SearchMembersOptions{ + teamMembers, err := organization.GetTeamMembers(ctx, &organization.SearchMembersOptions{ ListOptions: utils.GetListOptions(ctx), - }); err != nil { + TeamID: ctx.Org.Team.ID, + }) + if err != nil { ctx.Error(http.StatusInternalServerError, "GetTeamMembers", err) return } + members := make([]*api.User, len(ctx.Org.Team.Members)) - for i, member := range ctx.Org.Team.Members { + for i, member := range teamMembers { members[i] = convert.ToUser(member, ctx.Doer) } @@ -422,7 +426,7 @@ func GetTeamMember(ctx *context.APIContext) { return } teamID := ctx.ParamsInt64("teamid") - isTeamMember, err := models.IsUserInTeams(u.ID, []int64{teamID}) + isTeamMember, err := organization.IsUserInTeams(ctx, u.ID, []int64{teamID}) if err != nil { ctx.Error(http.StatusInternalServerError, "IsUserInTeams", err) return @@ -462,7 +466,7 @@ func AddTeamMember(ctx *context.APIContext) { if ctx.Written() { return } - if err := ctx.Org.Team.AddMember(u.ID); err != nil { + if err := models.AddTeamMember(ctx.Org.Team, u.ID); err != nil { ctx.Error(http.StatusInternalServerError, "AddMember", err) return } @@ -499,8 +503,8 @@ func RemoveTeamMember(ctx *context.APIContext) { return } - if err := ctx.Org.Team.RemoveMember(u.ID); err != nil { - ctx.Error(http.StatusInternalServerError, "RemoveMember", err) + if err := models.RemoveTeamMember(ctx.Org.Team, u.ID); err != nil { + ctx.Error(http.StatusInternalServerError, "RemoveTeamMember", err) return } ctx.Status(http.StatusNoContent) @@ -533,13 +537,16 @@ func GetTeamRepos(ctx *context.APIContext) { // "$ref": "#/responses/RepositoryList" team := ctx.Org.Team - if err := team.GetRepositories(&models.SearchOrgTeamOptions{ + teamRepos, err := organization.GetTeamRepositories(ctx, &organization.SearchTeamRepoOptions{ ListOptions: utils.GetListOptions(ctx), - }); err != nil { + TeamID: team.ID, + }) + if err != nil { ctx.Error(http.StatusInternalServerError, "GetTeamRepos", err) + return } repos := make([]*api.Repository, len(team.Repos)) - for i, repo := range team.Repos { + for i, repo := range teamRepos { access, err := models.AccessLevel(ctx.Doer, repo) if err != nil { ctx.Error(http.StatusInternalServerError, "GetTeamRepos", err) @@ -606,7 +613,7 @@ func AddTeamRepository(ctx *context.APIContext) { ctx.Error(http.StatusForbidden, "", "Must have admin-level access to the repository") return } - if err := ctx.Org.Team.AddRepository(repo); err != nil { + if err := models.AddRepository(ctx.Org.Team, repo); err != nil { ctx.Error(http.StatusInternalServerError, "AddRepository", err) return } @@ -656,7 +663,7 @@ func RemoveTeamRepository(ctx *context.APIContext) { ctx.Error(http.StatusForbidden, "", "Must have admin-level access to the repository") return } - if err := ctx.Org.Team.RemoveRepository(repo.ID); err != nil { + if err := models.RemoveRepository(ctx.Org.Team, repo.ID); err != nil { ctx.Error(http.StatusInternalServerError, "RemoveRepository", err) return } @@ -707,14 +714,15 @@ func SearchTeam(ctx *context.APIContext) { listOptions := utils.GetListOptions(ctx) - opts := &models.SearchOrgTeamOptions{ + 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, } - teams, maxResults, err := models.SearchOrgTeams(opts) + teams, maxResults, err := organization.SearchTeam(opts) if err != nil { log.Error("SearchTeam failed: %v", err) ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ |