summaryrefslogtreecommitdiffstats
path: root/routers/api/v1/user/gpg_key.go
diff options
context:
space:
mode:
Diffstat (limited to 'routers/api/v1/user/gpg_key.go')
-rw-r--r--routers/api/v1/user/gpg_key.go29
1 files changed, 18 insertions, 11 deletions
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)
}
}