key.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 key.RepoID != c.Repo.Repository.ID {
  40. c.NotFound()
  41. return
  42. }
  43. if err = key.GetContent(); err != nil {
  44. c.Error(err, "get content")
  45. return
  46. }
  47. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  48. c.JSONSuccess(convert.ToDeployKey(apiLink, key))
  49. }
  50. func HandleCheckKeyStringError(c *context.APIContext, err error) {
  51. if database.IsErrKeyUnableVerify(err) {
  52. c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Unable to verify key content"))
  53. } else {
  54. c.ErrorStatus(http.StatusUnprocessableEntity, errors.Wrap(err, "Invalid key content: %v"))
  55. }
  56. }
  57. func HandleAddKeyError(c *context.APIContext, err error) {
  58. switch {
  59. case database.IsErrKeyAlreadyExist(err):
  60. c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Key content has been used as non-deploy key"))
  61. case database.IsErrKeyNameAlreadyUsed(err):
  62. c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Key title has been used"))
  63. default:
  64. c.Error(err, "add key")
  65. }
  66. }
  67. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#add-a-new-deploy-key
  68. func CreateDeployKey(c *context.APIContext, form api.CreateKeyOption) {
  69. content, err := database.CheckPublicKeyString(form.Key)
  70. if err != nil {
  71. HandleCheckKeyStringError(c, err)
  72. return
  73. }
  74. key, err := database.AddDeployKey(c.Repo.Repository.ID, form.Title, content)
  75. if err != nil {
  76. HandleAddKeyError(c, err)
  77. return
  78. }
  79. key.Content = content
  80. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  81. c.JSON(http.StatusCreated, convert.ToDeployKey(apiLink, key))
  82. }
  83. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#remove-a-deploy-key
  84. func DeleteDeploykey(c *context.APIContext) {
  85. key, err := database.GetDeployKeyByID(c.ParamsInt64(":id"))
  86. if err != nil {
  87. c.NotFoundOrError(err, "get deploy key by ID")
  88. return
  89. }
  90. if key.RepoID != c.Repo.Repository.ID {
  91. c.NotFound()
  92. return
  93. }
  94. if err := database.DeleteDeployKey(c.User, key.ID); err != nil {
  95. if database.IsErrKeyAccessDenied(err) {
  96. c.ErrorStatus(http.StatusForbidden, errors.New("You do not have access to this key"))
  97. } else {
  98. c.Error(err, "delete deploy key")
  99. }
  100. return
  101. }
  102. c.NoContent()
  103. }