key.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "net/http"
  7. api "github.com/gogs/go-gogs-client"
  8. "github.com/pkg/errors"
  9. "gogs.io/gogs/internal/conf"
  10. "gogs.io/gogs/internal/context"
  11. "gogs.io/gogs/internal/db"
  12. "gogs.io/gogs/internal/route/api/v1/convert"
  13. )
  14. func composeDeployKeysAPILink(repoPath string) string {
  15. return conf.Server.ExternalURL + "api/v1/repos/" + repoPath + "/keys/"
  16. }
  17. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#list-deploy-keys
  18. func ListDeployKeys(c *context.APIContext) {
  19. keys, err := db.ListDeployKeys(c.Repo.Repository.ID)
  20. if err != nil {
  21. c.Error(err, "list deploy keys")
  22. return
  23. }
  24. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  25. apiKeys := make([]*api.DeployKey, len(keys))
  26. for i := range keys {
  27. if err = keys[i].GetContent(); err != nil {
  28. c.Error(err, "get content")
  29. return
  30. }
  31. apiKeys[i] = convert.ToDeployKey(apiLink, keys[i])
  32. }
  33. c.JSONSuccess(&apiKeys)
  34. }
  35. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#get-a-deploy-key
  36. func GetDeployKey(c *context.APIContext) {
  37. key, err := db.GetDeployKeyByID(c.ParamsInt64(":id"))
  38. if err != nil {
  39. c.NotFoundOrError(err, "get deploy key by ID")
  40. return
  41. }
  42. if err = key.GetContent(); err != nil {
  43. c.Error(err, "get content")
  44. return
  45. }
  46. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  47. c.JSONSuccess(convert.ToDeployKey(apiLink, key))
  48. }
  49. func HandleCheckKeyStringError(c *context.APIContext, err error) {
  50. if db.IsErrKeyUnableVerify(err) {
  51. c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Unable to verify key content"))
  52. } else {
  53. c.ErrorStatus(http.StatusUnprocessableEntity, errors.Wrap(err, "Invalid key content: %v"))
  54. }
  55. }
  56. func HandleAddKeyError(c *context.APIContext, err error) {
  57. switch {
  58. case db.IsErrKeyAlreadyExist(err):
  59. c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Key content has been used as non-deploy key"))
  60. case db.IsErrKeyNameAlreadyUsed(err):
  61. c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Key title has been used"))
  62. default:
  63. c.Error(err, "add key")
  64. }
  65. }
  66. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#add-a-new-deploy-key
  67. func CreateDeployKey(c *context.APIContext, form api.CreateKeyOption) {
  68. content, err := db.CheckPublicKeyString(form.Key)
  69. if err != nil {
  70. HandleCheckKeyStringError(c, err)
  71. return
  72. }
  73. key, err := db.AddDeployKey(c.Repo.Repository.ID, form.Title, content)
  74. if err != nil {
  75. HandleAddKeyError(c, err)
  76. return
  77. }
  78. key.Content = content
  79. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  80. c.JSON(http.StatusCreated, convert.ToDeployKey(apiLink, key))
  81. }
  82. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#remove-a-deploy-key
  83. func DeleteDeploykey(c *context.APIContext) {
  84. if err := db.DeleteDeployKey(c.User, c.ParamsInt64(":id")); err != nil {
  85. if db.IsErrKeyAccessDenied(err) {
  86. c.ErrorStatus(http.StatusForbidden, errors.New("You do not have access to this key"))
  87. } else {
  88. c.Error(err, "delete deploy key")
  89. }
  90. return
  91. }
  92. c.NoContent()
  93. }