key.go 3.1 KB

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