diff options
author | 6543 <6543@obermui.de> | 2019-12-20 18:07:12 +0100 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2019-12-20 19:07:12 +0200 |
commit | 2848c5eb8f7333b6791afd296b12d21751d0516b (patch) | |
tree | 67ff6244026174116edbff1b4c4cdb5934401968 /routers/api/v1/org/team.go | |
parent | 050a8af4243d7f5fff0a2f492b9166f4dfdf0ecf (diff) | |
download | gitea-2848c5eb8f7333b6791afd296b12d21751d0516b.tar.gz gitea-2848c5eb8f7333b6791afd296b12d21751d0516b.zip |
Swagger info corrections (#9441)
* use numbers and not http.Status___ enum
* fix test
* add many missing swagger responses
* code format
* Deletion Sould return 204 ...
* error handling improvements
* if special error type ... then add it to swagger too
* one smal nit
* invalidTopicsError is []string
* valid swagger specification 2.0
- if you add responses swagger can tell you if you do it right :+1:
* use ctx.InternalServerError
* Revert "use numbers and not http.Status___ enum"
This reverts commit b1ff386e2418ed6a7f183e756b13277d701278ef.
* use http.Status* enum everywhere
Diffstat (limited to 'routers/api/v1/org/team.go')
-rw-r--r-- | routers/api/v1/org/team.go | 103 |
1 files changed, 65 insertions, 38 deletions
diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index c14742e3a4..73714e6a66 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -6,6 +6,7 @@ package org import ( + "net/http" "strings" "code.gitea.io/gitea/models" @@ -32,22 +33,23 @@ func ListTeams(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/TeamList" + org := ctx.Org.Organization if err := org.GetTeams(); err != nil { - ctx.Error(500, "GetTeams", err) + ctx.Error(http.StatusInternalServerError, "GetTeams", err) return } apiTeams := make([]*api.Team, len(org.Teams)) for i := range org.Teams { if err := org.Teams[i].GetUnits(); err != nil { - ctx.Error(500, "GetUnits", err) + ctx.Error(http.StatusInternalServerError, "GetUnits", err) return } apiTeams[i] = convert.ToTeam(org.Teams[i]) } - ctx.JSON(200, apiTeams) + ctx.JSON(http.StatusOK, apiTeams) } // ListUserTeams list all the teams a user belongs to @@ -60,9 +62,10 @@ func ListUserTeams(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/TeamList" + teams, err := models.GetUserTeams(ctx.User.ID) if err != nil { - ctx.Error(500, "GetUserTeams", err) + ctx.Error(http.StatusInternalServerError, "GetUserTeams", err) return } @@ -73,7 +76,7 @@ func ListUserTeams(ctx *context.APIContext) { if !ok { org, err := models.GetUserByID(teams[i].OrgID) if err != nil { - ctx.Error(500, "GetUserByID", err) + ctx.Error(http.StatusInternalServerError, "GetUserByID", err) return } apiOrg = convert.ToOrganization(org) @@ -82,7 +85,7 @@ func ListUserTeams(ctx *context.APIContext) { apiTeams[i] = convert.ToTeam(teams[i]) apiTeams[i].Organization = apiOrg } - ctx.JSON(200, apiTeams) + ctx.JSON(http.StatusOK, apiTeams) } // GetTeam api for get a team @@ -102,7 +105,8 @@ func GetTeam(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/Team" - ctx.JSON(200, convert.ToTeam(ctx.Org.Team)) + + ctx.JSON(http.StatusOK, convert.ToTeam(ctx.Org.Team)) } // CreateTeam api for create a team @@ -127,6 +131,9 @@ func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) { // responses: // "201": // "$ref": "#/responses/Team" + // "422": + // "$ref": "#/responses/validationError" + team := &models.Team{ OrgID: ctx.Org.Organization.ID, Name: form.Name, @@ -151,14 +158,14 @@ func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) { if err := models.NewTeam(team); err != nil { if models.IsErrTeamAlreadyExist(err) { - ctx.Error(422, "", err) + ctx.Error(http.StatusUnprocessableEntity, "", err) } else { - ctx.Error(500, "NewTeam", err) + ctx.Error(http.StatusInternalServerError, "NewTeam", err) } return } - ctx.JSON(201, convert.ToTeam(team)) + ctx.JSON(http.StatusCreated, convert.ToTeam(team)) } // EditTeam api for edit a team @@ -183,6 +190,7 @@ func EditTeam(ctx *context.APIContext, form api.EditTeamOption) { // responses: // "200": // "$ref": "#/responses/Team" + team := ctx.Org.Team team.Description = form.Description unitTypes := models.FindUnitTypes(form.Units...) @@ -218,10 +226,10 @@ func EditTeam(ctx *context.APIContext, form api.EditTeamOption) { } if err := models.UpdateTeam(team, isAuthChanged, isIncludeAllChanged); err != nil { - ctx.Error(500, "EditTeam", err) + ctx.Error(http.StatusInternalServerError, "EditTeam", err) return } - ctx.JSON(200, convert.ToTeam(team)) + ctx.JSON(http.StatusOK, convert.ToTeam(team)) } // DeleteTeam api for delete a team @@ -239,11 +247,12 @@ func DeleteTeam(ctx *context.APIContext) { // responses: // "204": // description: team deleted + if err := models.DeleteTeam(ctx.Org.Team); err != nil { - ctx.Error(500, "DeleteTeam", err) + ctx.Error(http.StatusInternalServerError, "DeleteTeam", err) return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } // GetTeamMembers api for get a team's members @@ -263,9 +272,10 @@ func GetTeamMembers(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/UserList" + isMember, err := models.IsOrganizationMember(ctx.Org.Team.OrgID, ctx.User.ID) if err != nil { - ctx.Error(500, "IsOrganizationMember", err) + ctx.Error(http.StatusInternalServerError, "IsOrganizationMember", err) return } else if !isMember { ctx.NotFound() @@ -273,14 +283,14 @@ func GetTeamMembers(ctx *context.APIContext) { } team := ctx.Org.Team if err := team.GetMembers(); err != nil { - ctx.Error(500, "GetTeamMembers", err) + ctx.Error(http.StatusInternalServerError, "GetTeamMembers", err) return } members := make([]*api.User, len(team.Members)) for i, member := range team.Members { members[i] = convert.ToUser(member, ctx.IsSigned, ctx.User.IsAdmin) } - ctx.JSON(200, members) + ctx.JSON(http.StatusOK, members) } // GetTeamMember api for get a particular member of team @@ -305,6 +315,9 @@ func GetTeamMember(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/User" + // "404": + // "$ref": "#/responses/notFound" + u := user.GetUserByParams(ctx) if ctx.Written() { return @@ -312,13 +325,13 @@ func GetTeamMember(ctx *context.APIContext) { teamID := ctx.ParamsInt64("teamid") isTeamMember, err := models.IsUserInTeams(u.ID, []int64{teamID}) if err != nil { - ctx.Error(500, "IsUserInTeams", err) + ctx.Error(http.StatusInternalServerError, "IsUserInTeams", err) return } else if !isTeamMember { ctx.NotFound() return } - ctx.JSON(200, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin)) + ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin)) } // AddTeamMember api for add a member to a team @@ -343,15 +356,18 @@ func AddTeamMember(ctx *context.APIContext) { // responses: // "204": // "$ref": "#/responses/empty" + // "404": + // "$ref": "#/responses/notFound" + u := user.GetUserByParams(ctx) if ctx.Written() { return } if err := ctx.Org.Team.AddMember(u.ID); err != nil { - ctx.Error(500, "AddMember", err) + ctx.Error(http.StatusInternalServerError, "AddMember", err) return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } // RemoveTeamMember api for remove one member from a team @@ -376,16 +392,19 @@ func RemoveTeamMember(ctx *context.APIContext) { // responses: // "204": // "$ref": "#/responses/empty" + // "404": + // "$ref": "#/responses/notFound" + u := user.GetUserByParams(ctx) if ctx.Written() { return } if err := ctx.Org.Team.RemoveMember(u.ID); err != nil { - ctx.Error(500, "RemoveMember", err) + ctx.Error(http.StatusInternalServerError, "RemoveMember", err) return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } // GetTeamRepos api for get a team's repos @@ -405,20 +424,21 @@ func GetTeamRepos(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/RepositoryList" + team := ctx.Org.Team if err := team.GetRepositories(); err != nil { - ctx.Error(500, "GetTeamRepos", err) + ctx.Error(http.StatusInternalServerError, "GetTeamRepos", err) } repos := make([]*api.Repository, len(team.Repos)) for i, repo := range team.Repos { access, err := models.AccessLevel(ctx.User, repo) if err != nil { - ctx.Error(500, "GetTeamRepos", err) + ctx.Error(http.StatusInternalServerError, "GetTeamRepos", err) return } repos[i] = repo.APIFormat(access) } - ctx.JSON(200, repos) + ctx.JSON(http.StatusOK, repos) } // getRepositoryByParams get repository by a team's organization ID and repo name @@ -428,7 +448,7 @@ func getRepositoryByParams(ctx *context.APIContext) *models.Repository { if models.IsErrRepoNotExist(err) { ctx.NotFound() } else { - ctx.Error(500, "GetRepositoryByName", err) + ctx.Error(http.StatusInternalServerError, "GetRepositoryByName", err) } return nil } @@ -462,22 +482,25 @@ func AddTeamRepository(ctx *context.APIContext) { // responses: // "204": // "$ref": "#/responses/empty" + // "403": + // "$ref": "#/responses/forbidden" + repo := getRepositoryByParams(ctx) if ctx.Written() { return } if access, err := models.AccessLevel(ctx.User, repo); err != nil { - ctx.Error(500, "AccessLevel", err) + ctx.Error(http.StatusInternalServerError, "AccessLevel", err) return } else if access < models.AccessModeAdmin { - ctx.Error(403, "", "Must have admin-level access to the repository") + ctx.Error(http.StatusForbidden, "", "Must have admin-level access to the repository") return } if err := ctx.Org.Team.AddRepository(repo); err != nil { - ctx.Error(500, "AddRepository", err) + ctx.Error(http.StatusInternalServerError, "AddRepository", err) return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } // RemoveTeamRepository api for removing a repository from a team @@ -509,22 +532,25 @@ func RemoveTeamRepository(ctx *context.APIContext) { // responses: // "204": // "$ref": "#/responses/empty" + // "403": + // "$ref": "#/responses/forbidden" + repo := getRepositoryByParams(ctx) if ctx.Written() { return } if access, err := models.AccessLevel(ctx.User, repo); err != nil { - ctx.Error(500, "AccessLevel", err) + ctx.Error(http.StatusInternalServerError, "AccessLevel", err) return } else if access < models.AccessModeAdmin { - ctx.Error(403, "", "Must have admin-level access to the repository") + ctx.Error(http.StatusForbidden, "", "Must have admin-level access to the repository") return } if err := ctx.Org.Team.RemoveRepository(repo.ID); err != nil { - ctx.Error(500, "RemoveRepository", err) + ctx.Error(http.StatusInternalServerError, "RemoveRepository", err) return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } // SearchTeam api for searching teams @@ -568,6 +594,7 @@ func SearchTeam(ctx *context.APIContext) { // type: array // items: // "$ref": "#/definitions/Team" + opts := &models.SearchTeamOptions{ UserID: ctx.User.ID, Keyword: strings.TrimSpace(ctx.Query("q")), @@ -580,7 +607,7 @@ func SearchTeam(ctx *context.APIContext) { teams, _, err := models.SearchTeam(opts) if err != nil { log.Error("SearchTeam failed: %v", err) - ctx.JSON(500, map[string]interface{}{ + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ "ok": false, "error": "SearchTeam internal failure", }) @@ -591,7 +618,7 @@ func SearchTeam(ctx *context.APIContext) { for i := range teams { if err := teams[i].GetUnits(); err != nil { log.Error("Team GetUnits failed: %v", err) - ctx.JSON(500, map[string]interface{}{ + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ "ok": false, "error": "SearchTeam failed to get units", }) @@ -600,7 +627,7 @@ func SearchTeam(ctx *context.APIContext) { apiTeams[i] = convert.ToTeam(teams[i]) } - ctx.JSON(200, map[string]interface{}{ + ctx.JSON(http.StatusOK, map[string]interface{}{ "ok": true, "data": apiTeams, }) |