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/user | |
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/user')
-rw-r--r-- | routers/api/v1/user/app.go | 17 | ||||
-rw-r--r-- | routers/api/v1/user/email.go | 25 | ||||
-rw-r--r-- | routers/api/v1/user/follower.go | 26 | ||||
-rw-r--r-- | routers/api/v1/user/gpg_key.go | 29 | ||||
-rw-r--r-- | routers/api/v1/user/key.go | 25 | ||||
-rw-r--r-- | routers/api/v1/user/repo.go | 17 | ||||
-rw-r--r-- | routers/api/v1/user/star.go | 25 | ||||
-rw-r--r-- | routers/api/v1/user/user.go | 15 | ||||
-rw-r--r-- | routers/api/v1/user/watch.go | 25 |
9 files changed, 131 insertions, 73 deletions
diff --git a/routers/api/v1/user/app.go b/routers/api/v1/user/app.go index 1123dadfd4..ec52f02d38 100644 --- a/routers/api/v1/user/app.go +++ b/routers/api/v1/user/app.go @@ -6,6 +6,8 @@ package user import ( + "net/http" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" api "code.gitea.io/gitea/modules/structs" @@ -27,9 +29,10 @@ func ListAccessTokens(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/AccessTokenList" + tokens, err := models.ListAccessTokens(ctx.User.ID) if err != nil { - ctx.Error(500, "ListAccessTokens", err) + ctx.Error(http.StatusInternalServerError, "ListAccessTokens", err) return } @@ -41,7 +44,7 @@ func ListAccessTokens(ctx *context.APIContext) { TokenLastEight: tokens[i].TokenLastEight, } } - ctx.JSON(200, &apiTokens) + ctx.JSON(http.StatusOK, &apiTokens) } // CreateAccessToken create access tokens @@ -71,15 +74,16 @@ func CreateAccessToken(ctx *context.APIContext, form api.CreateAccessTokenOption // responses: // "200": // "$ref": "#/responses/AccessToken" + t := &models.AccessToken{ UID: ctx.User.ID, Name: form.Name, } if err := models.NewAccessToken(t); err != nil { - ctx.Error(500, "NewAccessToken", err) + ctx.Error(http.StatusInternalServerError, "NewAccessToken", err) return } - ctx.JSON(201, &api.AccessToken{ + ctx.JSON(http.StatusCreated, &api.AccessToken{ Name: t.Name, Token: t.Token, ID: t.ID, @@ -109,15 +113,16 @@ func DeleteAccessToken(ctx *context.APIContext) { // responses: // "204": // "$ref": "#/responses/empty" + tokenID := ctx.ParamsInt64(":id") if err := models.DeleteAccessTokenByID(tokenID, ctx.User.ID); err != nil { if models.IsErrAccessTokenNotExist(err) { ctx.NotFound() } else { - ctx.Error(500, "DeleteAccessTokenByID", err) + ctx.Error(http.StatusInternalServerError, "DeleteAccessTokenByID", err) } return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } diff --git a/routers/api/v1/user/email.go b/routers/api/v1/user/email.go index 8c0eb889ed..07fcde625e 100644 --- a/routers/api/v1/user/email.go +++ b/routers/api/v1/user/email.go @@ -5,6 +5,8 @@ package user import ( + "net/http" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -23,16 +25,17 @@ func ListEmails(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/EmailList" + emails, err := models.GetEmailAddresses(ctx.User.ID) if err != nil { - ctx.Error(500, "GetEmailAddresses", err) + ctx.Error(http.StatusInternalServerError, "GetEmailAddresses", err) return } apiEmails := make([]*api.Email, len(emails)) for i := range emails { apiEmails[i] = convert.ToEmail(emails[i]) } - ctx.JSON(200, &apiEmails) + ctx.JSON(http.StatusOK, &apiEmails) } // AddEmail add an email address @@ -55,8 +58,11 @@ func AddEmail(ctx *context.APIContext, form api.CreateEmailOption) { // responses: // '201': // "$ref": "#/responses/EmailList" + // "422": + // "$ref": "#/responses/validationError" + if len(form.Emails) == 0 { - ctx.Status(422) + ctx.Error(http.StatusUnprocessableEntity, "", "Email list empty") return } @@ -71,9 +77,9 @@ func AddEmail(ctx *context.APIContext, form api.CreateEmailOption) { if err := models.AddEmailAddresses(emails); err != nil { if models.IsErrEmailAlreadyUsed(err) { - ctx.Error(422, "", "Email address has been used: "+err.(models.ErrEmailAlreadyUsed).Email) + ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(models.ErrEmailAlreadyUsed).Email) } else { - ctx.Error(500, "AddEmailAddresses", err) + ctx.Error(http.StatusInternalServerError, "AddEmailAddresses", err) } return } @@ -82,7 +88,7 @@ func AddEmail(ctx *context.APIContext, form api.CreateEmailOption) { for i := range emails { apiEmails[i] = convert.ToEmail(emails[i]) } - ctx.JSON(201, &apiEmails) + ctx.JSON(http.StatusCreated, &apiEmails) } // DeleteEmail delete email @@ -100,8 +106,9 @@ func DeleteEmail(ctx *context.APIContext, form api.DeleteEmailOption) { // responses: // "204": // "$ref": "#/responses/empty" + if len(form.Emails) == 0 { - ctx.Status(204) + ctx.Status(http.StatusNoContent) return } @@ -114,8 +121,8 @@ func DeleteEmail(ctx *context.APIContext, form api.DeleteEmailOption) { } if err := models.DeleteEmailAddresses(emails); err != nil { - ctx.Error(500, "DeleteEmailAddresses", err) + ctx.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err) return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } diff --git a/routers/api/v1/user/follower.go b/routers/api/v1/user/follower.go index ec512b9806..bd68070265 100644 --- a/routers/api/v1/user/follower.go +++ b/routers/api/v1/user/follower.go @@ -5,6 +5,8 @@ package user import ( + "net/http" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -16,13 +18,13 @@ func responseAPIUsers(ctx *context.APIContext, users []*models.User) { for i := range users { apiUsers[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin) } - ctx.JSON(200, &apiUsers) + ctx.JSON(http.StatusOK, &apiUsers) } func listUserFollowers(ctx *context.APIContext, u *models.User) { users, err := u.GetFollowers(ctx.QueryInt("page")) if err != nil { - ctx.Error(500, "GetUserFollowers", err) + ctx.Error(http.StatusInternalServerError, "GetUserFollowers", err) return } responseAPIUsers(ctx, users) @@ -38,6 +40,7 @@ func ListMyFollowers(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/UserList" + listUserFollowers(ctx, ctx.User) } @@ -57,6 +60,7 @@ func ListFollowers(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/UserList" + u := GetUserByParams(ctx) if ctx.Written() { return @@ -67,7 +71,7 @@ func ListFollowers(ctx *context.APIContext) { func listUserFollowing(ctx *context.APIContext, u *models.User) { users, err := u.GetFollowing(ctx.QueryInt("page")) if err != nil { - ctx.Error(500, "GetFollowing", err) + ctx.Error(http.StatusInternalServerError, "GetFollowing", err) return } responseAPIUsers(ctx, users) @@ -83,6 +87,7 @@ func ListMyFollowing(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/UserList" + listUserFollowing(ctx, ctx.User) } @@ -102,6 +107,7 @@ func ListFollowing(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/UserList" + u := GetUserByParams(ctx) if ctx.Written() { return @@ -111,7 +117,7 @@ func ListFollowing(ctx *context.APIContext) { func checkUserFollowing(ctx *context.APIContext, u *models.User, followID int64) { if u.IsFollowing(followID) { - ctx.Status(204) + ctx.Status(http.StatusNoContent) } else { ctx.NotFound() } @@ -133,6 +139,7 @@ func CheckMyFollowing(ctx *context.APIContext) { // "$ref": "#/responses/empty" // "404": // "$ref": "#/responses/notFound" + target := GetUserByParams(ctx) if ctx.Written() { return @@ -161,6 +168,7 @@ func CheckFollowing(ctx *context.APIContext) { // "$ref": "#/responses/empty" // "404": // "$ref": "#/responses/notFound" + u := GetUserByParams(ctx) if ctx.Written() { return @@ -186,15 +194,16 @@ func Follow(ctx *context.APIContext) { // responses: // "204": // "$ref": "#/responses/empty" + target := GetUserByParams(ctx) if ctx.Written() { return } if err := models.FollowUser(ctx.User.ID, target.ID); err != nil { - ctx.Error(500, "FollowUser", err) + ctx.Error(http.StatusInternalServerError, "FollowUser", err) return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } // Unfollow unfollow a user @@ -211,13 +220,14 @@ func Unfollow(ctx *context.APIContext) { // responses: // "204": // "$ref": "#/responses/empty" + target := GetUserByParams(ctx) if ctx.Written() { return } if err := models.UnfollowUser(ctx.User.ID, target.ID); err != nil { - ctx.Error(500, "UnfollowUser", err) + ctx.Error(http.StatusInternalServerError, "UnfollowUser", err) return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } diff --git a/routers/api/v1/user/gpg_key.go b/routers/api/v1/user/gpg_key.go index 82113caf0c..b6133ca7be 100644 --- a/routers/api/v1/user/gpg_key.go +++ b/routers/api/v1/user/gpg_key.go @@ -5,6 +5,8 @@ package user import ( + "net/http" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -14,7 +16,7 @@ import ( func listGPGKeys(ctx *context.APIContext, uid int64) { keys, err := models.ListGPGKeys(uid) if err != nil { - ctx.Error(500, "ListGPGKeys", err) + ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err) return } @@ -23,7 +25,7 @@ func listGPGKeys(ctx *context.APIContext, uid int64) { apiKeys[i] = convert.ToGPGKey(keys[i]) } - ctx.JSON(200, &apiKeys) + ctx.JSON(http.StatusOK, &apiKeys) } //ListGPGKeys get the GPG key list of a user @@ -42,6 +44,7 @@ func ListGPGKeys(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/GPGKeyList" + user := GetUserByParams(ctx) if ctx.Written() { return @@ -59,6 +62,7 @@ func ListMyGPGKeys(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/GPGKeyList" + listGPGKeys(ctx, ctx.User.ID) } @@ -81,16 +85,17 @@ func GetGPGKey(ctx *context.APIContext) { // "$ref": "#/responses/GPGKey" // "404": // "$ref": "#/responses/notFound" + key, err := models.GetGPGKeyByID(ctx.ParamsInt64(":id")) if err != nil { if models.IsErrGPGKeyNotExist(err) { ctx.NotFound() } else { - ctx.Error(500, "GetGPGKeyByID", err) + ctx.Error(http.StatusInternalServerError, "GetGPGKeyByID", err) } return } - ctx.JSON(200, convert.ToGPGKey(key)) + ctx.JSON(http.StatusOK, convert.ToGPGKey(key)) } // CreateUserGPGKey creates new GPG key to given user by ID. @@ -100,7 +105,7 @@ func CreateUserGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid HandleAddGPGKeyError(ctx, err) return } - ctx.JSON(201, convert.ToGPGKey(key)) + ctx.JSON(http.StatusCreated, convert.ToGPGKey(key)) } // swagger:parameters userCurrentPostGPGKey @@ -123,6 +128,7 @@ func CreateGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption) { // "$ref": "#/responses/GPGKey" // "422": // "$ref": "#/responses/validationError" + CreateUserGPGKey(ctx, form, ctx.User.ID) } @@ -145,26 +151,27 @@ func DeleteGPGKey(ctx *context.APIContext) { // "$ref": "#/responses/empty" // "403": // "$ref": "#/responses/forbidden" + if err := models.DeleteGPGKey(ctx.User, ctx.ParamsInt64(":id")); err != nil { if models.IsErrGPGKeyAccessDenied(err) { - ctx.Error(403, "", "You do not have access to this key") + ctx.Error(http.StatusForbidden, "", "You do not have access to this key") } else { - ctx.Error(500, "DeleteGPGKey", err) + ctx.Error(http.StatusInternalServerError, "DeleteGPGKey", err) } return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } // HandleAddGPGKeyError handle add GPGKey error func HandleAddGPGKeyError(ctx *context.APIContext, err error) { switch { case models.IsErrGPGKeyAccessDenied(err): - ctx.Error(422, "", "You do not have access to this GPG key") + ctx.Error(http.StatusUnprocessableEntity, "", "You do not have access to this GPG key") case models.IsErrGPGKeyIDAlreadyUsed(err): - ctx.Error(422, "", "A key with the same id already exists") + ctx.Error(http.StatusUnprocessableEntity, "", "A key with the same id already exists") default: - ctx.Error(500, "AddGPGKey", err) + ctx.Error(http.StatusInternalServerError, "AddGPGKey", err) } } diff --git a/routers/api/v1/user/key.go b/routers/api/v1/user/key.go index a812edfcc7..7cf6fa383d 100644 --- a/routers/api/v1/user/key.go +++ b/routers/api/v1/user/key.go @@ -5,6 +5,8 @@ package user import ( + "net/http" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -43,7 +45,7 @@ func GetUserByParamsName(ctx *context.APIContext, name string) *models.User { if models.IsErrUserNotExist(err) { ctx.NotFound() } else { - ctx.Error(500, "GetUserByName", err) + ctx.Error(http.StatusInternalServerError, "GetUserByName", err) } return nil } @@ -81,7 +83,7 @@ func listPublicKeys(ctx *context.APIContext, user *models.User) { } if err != nil { - ctx.Error(500, "ListPublicKeys", err) + ctx.Error(http.StatusInternalServerError, "ListPublicKeys", err) return } @@ -94,7 +96,7 @@ func listPublicKeys(ctx *context.APIContext, user *models.User) { } } - ctx.JSON(200, &apiKeys) + ctx.JSON(http.StatusOK, &apiKeys) } // ListMyPublicKeys list all of the authenticated user's public keys @@ -112,6 +114,7 @@ func ListMyPublicKeys(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/PublicKeyList" + listPublicKeys(ctx, ctx.User) } @@ -135,6 +138,7 @@ func ListPublicKeys(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/PublicKeyList" + user := GetUserByParams(ctx) if ctx.Written() { return @@ -161,12 +165,13 @@ func GetPublicKey(ctx *context.APIContext) { // "$ref": "#/responses/PublicKey" // "404": // "$ref": "#/responses/notFound" + key, err := models.GetPublicKeyByID(ctx.ParamsInt64(":id")) if err != nil { if models.IsErrKeyNotExist(err) { ctx.NotFound() } else { - ctx.Error(500, "GetPublicKeyByID", err) + ctx.Error(http.StatusInternalServerError, "GetPublicKeyByID", err) } return } @@ -176,7 +181,7 @@ func GetPublicKey(ctx *context.APIContext) { if ctx.User.IsAdmin || ctx.User.ID == key.OwnerID { apiKey, _ = appendPrivateInformation(apiKey, key, ctx.User) } - ctx.JSON(200, apiKey) + ctx.JSON(http.StatusOK, apiKey) } // CreateUserPublicKey creates new public key to given user by ID. @@ -197,7 +202,7 @@ func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid if ctx.User.IsAdmin || ctx.User.ID == key.OwnerID { apiKey, _ = appendPrivateInformation(apiKey, key, ctx.User) } - ctx.JSON(201, apiKey) + ctx.JSON(http.StatusCreated, apiKey) } // CreatePublicKey create one public key for me @@ -219,6 +224,7 @@ func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) { // "$ref": "#/responses/PublicKey" // "422": // "$ref": "#/responses/validationError" + CreateUserPublicKey(ctx, form, ctx.User.ID) } @@ -243,16 +249,17 @@ func DeletePublicKey(ctx *context.APIContext) { // "$ref": "#/responses/forbidden" // "404": // "$ref": "#/responses/notFound" + if err := models.DeletePublicKey(ctx.User, ctx.ParamsInt64(":id")); err != nil { if models.IsErrKeyNotExist(err) { ctx.NotFound() } else if models.IsErrKeyAccessDenied(err) { - ctx.Error(403, "", "You do not have access to this key") + ctx.Error(http.StatusForbidden, "", "You do not have access to this key") } else { - ctx.Error(500, "DeletePublicKey", err) + ctx.Error(http.StatusInternalServerError, "DeletePublicKey", err) } return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go index 99839b020a..90518f95e5 100644 --- a/routers/api/v1/user/repo.go +++ b/routers/api/v1/user/repo.go @@ -5,6 +5,8 @@ package user import ( + "net/http" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" api "code.gitea.io/gitea/modules/structs" @@ -14,7 +16,7 @@ import ( func listUserRepos(ctx *context.APIContext, u *models.User, private bool) { repos, err := models.GetUserRepositories(u.ID, private, 1, u.NumRepos, "") if err != nil { - ctx.Error(500, "GetUserRepositories", err) + ctx.Error(http.StatusInternalServerError, "GetUserRepositories", err) return } @@ -22,14 +24,14 @@ func listUserRepos(ctx *context.APIContext, u *models.User, private bool) { for i := range repos { access, err := models.AccessLevel(ctx.User, repos[i]) if err != nil { - ctx.Error(500, "AccessLevel", err) + ctx.Error(http.StatusInternalServerError, "AccessLevel", err) return } if ctx.IsSigned && ctx.User.IsAdmin || access >= models.AccessModeRead { apiRepos = append(apiRepos, repos[i].APIFormat(access)) } } - ctx.JSON(200, &apiRepos) + ctx.JSON(http.StatusOK, &apiRepos) } // ListUserRepos - list the repos owned by the given user. @@ -48,6 +50,7 @@ func ListUserRepos(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/RepositoryList" + user := GetUserByParams(ctx) if ctx.Written() { return @@ -66,14 +69,15 @@ func ListMyRepos(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/RepositoryList" + ownRepos, err := models.GetUserRepositories(ctx.User.ID, true, 1, ctx.User.NumRepos, "") if err != nil { - ctx.Error(500, "GetUserRepositories", err) + ctx.Error(http.StatusInternalServerError, "GetUserRepositories", err) return } accessibleReposMap, err := ctx.User.GetRepositoryAccesses() if err != nil { - ctx.Error(500, "GetRepositoryAccesses", err) + ctx.Error(http.StatusInternalServerError, "GetRepositoryAccesses", err) return } @@ -86,7 +90,7 @@ func ListMyRepos(ctx *context.APIContext) { apiRepos[i] = repo.APIFormat(access) i++ } - ctx.JSON(200, &apiRepos) + ctx.JSON(http.StatusOK, &apiRepos) } // ListOrgRepos - list the repositories of an organization. @@ -105,5 +109,6 @@ func ListOrgRepos(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/RepositoryList" + listUserRepos(ctx, ctx.Org.Organization, ctx.IsSigned) } diff --git a/routers/api/v1/user/star.go b/routers/api/v1/user/star.go index 744e0bf7d5..e5d3a8f0a0 100644 --- a/routers/api/v1/user/star.go +++ b/routers/api/v1/user/star.go @@ -5,6 +5,8 @@ package user import ( + "net/http" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" api "code.gitea.io/gitea/modules/structs" @@ -45,13 +47,14 @@ func GetStarredRepos(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/RepositoryList" + user := GetUserByParams(ctx) private := user.ID == ctx.User.ID repos, err := getStarredRepos(user, private) if err != nil { - ctx.Error(500, "getStarredRepos", err) + ctx.Error(http.StatusInternalServerError, "getStarredRepos", err) } - ctx.JSON(200, &repos) + ctx.JSON(http.StatusOK, &repos) } // GetMyStarredRepos returns the repos that the authenticated user has starred @@ -64,11 +67,12 @@ func GetMyStarredRepos(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/RepositoryList" + repos, err := getStarredRepos(ctx.User, true) if err != nil { - ctx.Error(500, "getStarredRepos", err) + ctx.Error(http.StatusInternalServerError, "getStarredRepos", err) } - ctx.JSON(200, &repos) + ctx.JSON(http.StatusOK, &repos) } // IsStarring returns whether the authenticated is starring the repo @@ -92,8 +96,9 @@ func IsStarring(ctx *context.APIContext) { // "$ref": "#/responses/empty" // "404": // "$ref": "#/responses/notFound" + if models.IsStaring(ctx.User.ID, ctx.Repo.Repository.ID) { - ctx.Status(204) + ctx.Status(http.StatusNoContent) } else { ctx.NotFound() } @@ -118,12 +123,13 @@ func Star(ctx *context.APIContext) { // responses: // "204": // "$ref": "#/responses/empty" + err := models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, true) if err != nil { - ctx.Error(500, "StarRepo", err) + ctx.Error(http.StatusInternalServerError, "StarRepo", err) return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } // Unstar the repo specified in the APIContext, as the authenticated user @@ -145,10 +151,11 @@ func Unstar(ctx *context.APIContext) { // responses: // "204": // "$ref": "#/responses/empty" + err := models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, false) if err != nil { - ctx.Error(500, "StarRepo", err) + ctx.Error(http.StatusInternalServerError, "StarRepo", err) return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index 6f3fc4d32b..3f17a6f91b 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -49,6 +49,7 @@ func Search(ctx *context.APIContext) { // type: array // items: // "$ref": "#/definitions/User" + opts := &models.SearchUserOptions{ Keyword: strings.Trim(ctx.Query("q"), " "), UID: com.StrTo(ctx.Query("uid")).MustInt64(), @@ -58,7 +59,7 @@ func Search(ctx *context.APIContext) { users, _, err := models.SearchUsers(opts) if err != nil { - ctx.JSON(500, map[string]interface{}{ + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ "ok": false, "error": err.Error(), }) @@ -70,7 +71,7 @@ func Search(ctx *context.APIContext) { results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin) } - ctx.JSON(200, map[string]interface{}{ + ctx.JSON(http.StatusOK, map[string]interface{}{ "ok": true, "data": results, }) @@ -94,17 +95,18 @@ func GetInfo(ctx *context.APIContext) { // "$ref": "#/responses/User" // "404": // "$ref": "#/responses/notFound" + u, err := models.GetUserByName(ctx.Params(":username")) if err != nil { if models.IsErrUserNotExist(err) { ctx.NotFound() } else { - ctx.Error(500, "GetUserByName", err) + ctx.Error(http.StatusInternalServerError, "GetUserByName", err) } return } - ctx.JSON(200, convert.ToUser(u, ctx.IsSigned, ctx.User != nil && (ctx.User.ID == u.ID || ctx.User.IsAdmin))) + ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.IsSigned, ctx.User != nil && (ctx.User.ID == u.ID || ctx.User.IsAdmin))) } // GetAuthenticatedUser get current user's information @@ -117,7 +119,8 @@ func GetAuthenticatedUser(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/User" - ctx.JSON(200, convert.ToUser(ctx.User, ctx.IsSigned, ctx.User != nil)) + + ctx.JSON(http.StatusOK, convert.ToUser(ctx.User, ctx.IsSigned, ctx.User != nil)) } // GetUserHeatmapData is the handler to get a users heatmap @@ -155,5 +158,5 @@ func GetUserHeatmapData(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "GetUserHeatmapDataByUser", err) return } - ctx.JSON(200, heatmap) + ctx.JSON(http.StatusOK, heatmap) } diff --git a/routers/api/v1/user/watch.go b/routers/api/v1/user/watch.go index 87739b15fb..ec8543dcf0 100644 --- a/routers/api/v1/user/watch.go +++ b/routers/api/v1/user/watch.go @@ -5,6 +5,8 @@ package user import ( + "net/http" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" @@ -46,13 +48,14 @@ func GetWatchedRepos(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/RepositoryList" + user := GetUserByParams(ctx) private := user.ID == ctx.User.ID repos, err := getWatchedRepos(user, private) if err != nil { - ctx.Error(500, "getWatchedRepos", err) + ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err) } - ctx.JSON(200, &repos) + ctx.JSON(http.StatusOK, &repos) } // GetMyWatchedRepos returns the repos that the authenticated user is watching @@ -65,11 +68,12 @@ func GetMyWatchedRepos(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/RepositoryList" + repos, err := getWatchedRepos(ctx.User, true) if err != nil { - ctx.Error(500, "getWatchedRepos", err) + ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err) } - ctx.JSON(200, &repos) + ctx.JSON(http.StatusOK, &repos) } // IsWatching returns whether the authenticated user is watching the repo @@ -92,8 +96,9 @@ func IsWatching(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/WatchInfo" + if models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID) { - ctx.JSON(200, api.WatchInfo{ + ctx.JSON(http.StatusOK, api.WatchInfo{ Subscribed: true, Ignored: false, Reason: nil, @@ -125,12 +130,13 @@ func Watch(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/WatchInfo" + err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, true) if err != nil { - ctx.Error(500, "WatchRepo", err) + ctx.Error(http.StatusInternalServerError, "WatchRepo", err) return } - ctx.JSON(200, api.WatchInfo{ + ctx.JSON(http.StatusOK, api.WatchInfo{ Subscribed: true, Ignored: false, Reason: nil, @@ -160,12 +166,13 @@ func Unwatch(ctx *context.APIContext) { // responses: // "204": // "$ref": "#/responses/empty" + err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, false) if err != nil { - ctx.Error(500, "UnwatchRepo", err) + ctx.Error(http.StatusInternalServerError, "UnwatchRepo", err) return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } // subscriptionURL returns the URL of the subscription API endpoint of a repo |