mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-11-01 15:01:03 +00:00
Convert all API handers to use *context.APIContext
This commit is contained in:
parent
db4da7beec
commit
dd6faf7f9b
20 changed files with 204 additions and 196 deletions
|
|
@ -20,10 +20,10 @@ func composeDeployKeysAPILink(repoPath string) string {
|
|||
}
|
||||
|
||||
// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#list-deploy-keys
|
||||
func ListDeployKeys(ctx *context.Context) {
|
||||
func ListDeployKeys(ctx *context.APIContext) {
|
||||
keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "ListDeployKeys", err)
|
||||
ctx.Error(500, "ListDeployKeys", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -31,7 +31,7 @@ func ListDeployKeys(ctx *context.Context) {
|
|||
apiKeys := make([]*api.DeployKey, len(keys))
|
||||
for i := range keys {
|
||||
if err = keys[i].GetContent(); err != nil {
|
||||
ctx.APIError(500, "GetContent", err)
|
||||
ctx.Error(500, "GetContent", err)
|
||||
return
|
||||
}
|
||||
apiKeys[i] = convert.ToApiDeployKey(apiLink, keys[i])
|
||||
|
|
@ -41,19 +41,19 @@ func ListDeployKeys(ctx *context.Context) {
|
|||
}
|
||||
|
||||
// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#get-a-deploy-key
|
||||
func GetDeployKey(ctx *context.Context) {
|
||||
func GetDeployKey(ctx *context.APIContext) {
|
||||
key, err := models.GetDeployKeyByID(ctx.ParamsInt64(":id"))
|
||||
if err != nil {
|
||||
if models.IsErrDeployKeyNotExist(err) {
|
||||
ctx.Error(404)
|
||||
ctx.Status(404)
|
||||
} else {
|
||||
ctx.Handle(500, "GetDeployKeyByID", err)
|
||||
ctx.Error(500, "GetDeployKeyByID", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err = key.GetContent(); err != nil {
|
||||
ctx.APIError(500, "GetContent", err)
|
||||
ctx.Error(500, "GetContent", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -61,27 +61,27 @@ func GetDeployKey(ctx *context.Context) {
|
|||
ctx.JSON(200, convert.ToApiDeployKey(apiLink, key))
|
||||
}
|
||||
|
||||
func HandleCheckKeyStringError(ctx *context.Context, err error) {
|
||||
func HandleCheckKeyStringError(ctx *context.APIContext, err error) {
|
||||
if models.IsErrKeyUnableVerify(err) {
|
||||
ctx.APIError(422, "", "Unable to verify key content")
|
||||
ctx.Error(422, "", "Unable to verify key content")
|
||||
} else {
|
||||
ctx.APIError(422, "", fmt.Errorf("Invalid key content: %v", err))
|
||||
ctx.Error(422, "", fmt.Errorf("Invalid key content: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
func HandleAddKeyError(ctx *context.Context, err error) {
|
||||
func HandleAddKeyError(ctx *context.APIContext, err error) {
|
||||
switch {
|
||||
case models.IsErrKeyAlreadyExist(err):
|
||||
ctx.APIError(422, "", "Key content has been used as non-deploy key")
|
||||
ctx.Error(422, "", "Key content has been used as non-deploy key")
|
||||
case models.IsErrKeyNameAlreadyUsed(err):
|
||||
ctx.APIError(422, "", "Key title has been used")
|
||||
ctx.Error(422, "", "Key title has been used")
|
||||
default:
|
||||
ctx.APIError(500, "AddKey", err)
|
||||
ctx.Error(500, "AddKey", err)
|
||||
}
|
||||
}
|
||||
|
||||
// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#add-a-new-deploy-key
|
||||
func CreateDeployKey(ctx *context.Context, form api.CreateKeyOption) {
|
||||
func CreateDeployKey(ctx *context.APIContext, form api.CreateKeyOption) {
|
||||
content, err := models.CheckPublicKeyString(form.Key)
|
||||
if err != nil {
|
||||
HandleCheckKeyStringError(ctx, err)
|
||||
|
|
@ -100,12 +100,12 @@ func CreateDeployKey(ctx *context.Context, form api.CreateKeyOption) {
|
|||
}
|
||||
|
||||
// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#remove-a-deploy-key
|
||||
func DeleteDeploykey(ctx *context.Context) {
|
||||
func DeleteDeploykey(ctx *context.APIContext) {
|
||||
if err := models.DeleteDeployKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
|
||||
if models.IsErrKeyAccessDenied(err) {
|
||||
ctx.APIError(403, "", "You do not have access to this key")
|
||||
ctx.Error(403, "", "You do not have access to this key")
|
||||
} else {
|
||||
ctx.APIError(500, "DeleteDeployKey", err)
|
||||
ctx.Error(500, "DeleteDeployKey", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue