Files
Gogs/internal/route/api/v1/repo/key.go

115 lines
3.1 KiB
Go
Raw Normal View History

2015-11-18 21:21:47 -05:00
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package repo
2015-11-18 21:21:47 -05:00
import (
"fmt"
convert2 "gogs.io/gogs/internal/route/api/v1/convert"
2015-11-18 21:21:47 -05:00
2018-05-27 08:53:48 +08:00
api "github.com/gogs/go-gogs-client"
2015-11-18 21:21:47 -05:00
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
"gogs.io/gogs/internal/setting"
2015-11-18 21:21:47 -05:00
)
func composeDeployKeysAPILink(repoPath string) string {
return setting.AppURL + "api/v1/repos/" + repoPath + "/keys/"
2015-11-18 21:21:47 -05:00
}
2018-05-27 08:53:48 +08:00
// https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#list-deploy-keys
2017-06-03 07:26:09 -04:00
func ListDeployKeys(c *context.APIContext) {
keys, err := db.ListDeployKeys(c.Repo.Repository.ID)
2015-11-18 21:21:47 -05:00
if err != nil {
2017-06-03 07:26:09 -04:00
c.Error(500, "ListDeployKeys", err)
2015-11-18 21:21:47 -05:00
return
}
2017-06-03 07:26:09 -04:00
apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
2015-11-18 21:21:47 -05:00
apiKeys := make([]*api.DeployKey, len(keys))
for i := range keys {
if err = keys[i].GetContent(); err != nil {
2017-06-03 07:26:09 -04:00
c.Error(500, "GetContent", err)
2015-11-18 21:21:47 -05:00
return
}
apiKeys[i] = convert2.ToDeployKey(apiLink, keys[i])
2015-11-18 21:21:47 -05:00
}
2017-06-03 07:26:09 -04:00
c.JSON(200, &apiKeys)
2015-11-18 21:21:47 -05:00
}
2018-05-27 08:53:48 +08:00
// https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#get-a-deploy-key
2017-06-03 07:26:09 -04:00
func GetDeployKey(c *context.APIContext) {
key, err := db.GetDeployKeyByID(c.ParamsInt64(":id"))
2015-11-18 21:21:47 -05:00
if err != nil {
if db.IsErrDeployKeyNotExist(err) {
2017-06-03 07:26:09 -04:00
c.Status(404)
2015-11-18 21:21:47 -05:00
} else {
2017-06-03 07:26:09 -04:00
c.Error(500, "GetDeployKeyByID", err)
2015-11-18 21:21:47 -05:00
}
return
}
if err = key.GetContent(); err != nil {
2017-06-03 07:26:09 -04:00
c.Error(500, "GetContent", err)
2015-11-18 21:21:47 -05:00
return
}
2017-06-03 07:26:09 -04:00
apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
c.JSON(200, convert2.ToDeployKey(apiLink, key))
2015-11-18 21:21:47 -05:00
}
2017-06-03 07:26:09 -04:00
func HandleCheckKeyStringError(c *context.APIContext, err error) {
if db.IsErrKeyUnableVerify(err) {
2017-06-03 07:26:09 -04:00
c.Error(422, "", "Unable to verify key content")
2015-12-03 00:24:37 -05:00
} else {
2017-06-03 07:26:09 -04:00
c.Error(422, "", fmt.Errorf("Invalid key content: %v", err))
2015-12-03 00:24:37 -05:00
}
}
2017-06-03 07:26:09 -04:00
func HandleAddKeyError(c *context.APIContext, err error) {
2015-12-03 00:24:37 -05:00
switch {
case db.IsErrKeyAlreadyExist(err):
2017-06-03 07:26:09 -04:00
c.Error(422, "", "Key content has been used as non-deploy key")
case db.IsErrKeyNameAlreadyUsed(err):
2017-06-03 07:26:09 -04:00
c.Error(422, "", "Key title has been used")
2015-12-03 00:24:37 -05:00
default:
2017-06-03 07:26:09 -04:00
c.Error(500, "AddKey", err)
2015-12-03 00:24:37 -05:00
}
}
2018-05-27 08:53:48 +08:00
// https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#add-a-new-deploy-key
2017-06-03 07:26:09 -04:00
func CreateDeployKey(c *context.APIContext, form api.CreateKeyOption) {
content, err := db.CheckPublicKeyString(form.Key)
2015-11-18 21:21:47 -05:00
if err != nil {
2017-06-03 07:26:09 -04:00
HandleCheckKeyStringError(c, err)
2015-11-18 21:21:47 -05:00
return
}
key, err := db.AddDeployKey(c.Repo.Repository.ID, form.Title, content)
2015-11-18 21:21:47 -05:00
if err != nil {
2017-06-03 07:26:09 -04:00
HandleAddKeyError(c, err)
2015-11-18 21:21:47 -05:00
return
}
key.Content = content
2017-06-03 07:26:09 -04:00
apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
c.JSON(201, convert2.ToDeployKey(apiLink, key))
2015-11-18 21:21:47 -05:00
}
2018-05-27 08:53:48 +08:00
// https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#remove-a-deploy-key
2017-06-03 07:26:09 -04:00
func DeleteDeploykey(c *context.APIContext) {
if err := db.DeleteDeployKey(c.User, c.ParamsInt64(":id")); err != nil {
if db.IsErrKeyAccessDenied(err) {
2017-06-03 07:26:09 -04:00
c.Error(403, "", "You do not have access to this key")
2015-12-03 00:24:37 -05:00
} else {
2017-06-03 07:26:09 -04:00
c.Error(500, "DeleteDeployKey", err)
2015-12-03 00:24:37 -05:00
}
2015-11-18 21:21:47 -05:00
return
}
2017-06-03 07:26:09 -04:00
c.Status(204)
2015-11-18 21:21:47 -05:00
}