repo_key.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package v1
  2. import (
  3. "net/http"
  4. "github.com/cockroachdb/errors"
  5. "gogs.io/gogs/internal/conf"
  6. "gogs.io/gogs/internal/context"
  7. "gogs.io/gogs/internal/database"
  8. "gogs.io/gogs/internal/route/api/v1/types"
  9. )
  10. func composeDeployKeysAPILink(repoPath string) string {
  11. return conf.Server.ExternalURL + "api/v1/repos/" + repoPath + "/keys/"
  12. }
  13. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#list-deploy-keys
  14. func listDeployKeys(c *context.APIContext) {
  15. keys, err := database.ListDeployKeys(c.Repo.Repository.ID)
  16. if err != nil {
  17. c.Error(err, "list deploy keys")
  18. return
  19. }
  20. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  21. apiKeys := make([]*types.RepositoryDeployKey, len(keys))
  22. for i := range keys {
  23. if err = keys[i].GetContent(); err != nil {
  24. c.Error(err, "get content")
  25. return
  26. }
  27. apiKeys[i] = toDeployKey(apiLink, keys[i])
  28. }
  29. c.JSONSuccess(&apiKeys)
  30. }
  31. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#get-a-deploy-key
  32. func getDeployKey(c *context.APIContext) {
  33. key, err := database.GetDeployKeyByID(c.ParamsInt64(":id"))
  34. if err != nil {
  35. c.NotFoundOrError(err, "get deploy key by ID")
  36. return
  37. }
  38. if key.RepoID != c.Repo.Repository.ID {
  39. c.NotFound()
  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(toDeployKey(apiLink, key))
  48. }
  49. func handleCheckKeyStringError(c *context.APIContext, err error) {
  50. if database.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 database.IsErrKeyAlreadyExist(err):
  59. c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Key content has been used as non-deploy key"))
  60. case database.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. type createDeployKeyRequest struct {
  68. Title string `json:"title" binding:"Required"`
  69. Key string `json:"key" binding:"Required"`
  70. }
  71. func createDeployKey(c *context.APIContext, form createDeployKeyRequest) {
  72. content, err := database.CheckPublicKeyString(form.Key)
  73. if err != nil {
  74. handleCheckKeyStringError(c, err)
  75. return
  76. }
  77. key, err := database.AddDeployKey(c.Repo.Repository.ID, form.Title, content)
  78. if err != nil {
  79. handleAddKeyError(c, err)
  80. return
  81. }
  82. key.Content = content
  83. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  84. c.JSON(http.StatusCreated, toDeployKey(apiLink, key))
  85. }
  86. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#remove-a-deploy-key
  87. func deleteDeploykey(c *context.APIContext) {
  88. key, err := database.GetDeployKeyByID(c.ParamsInt64(":id"))
  89. if err != nil {
  90. c.NotFoundOrError(err, "get deploy key by ID")
  91. return
  92. }
  93. if key.RepoID != c.Repo.Repository.ID {
  94. c.NotFound()
  95. return
  96. }
  97. if err := database.DeleteDeployKey(c.User, key.ID); err != nil {
  98. if database.IsErrKeyAccessDenied(err) {
  99. c.ErrorStatus(http.StatusForbidden, errors.New("You do not have access to this key"))
  100. } else {
  101. c.Error(err, "delete deploy key")
  102. }
  103. return
  104. }
  105. c.NoContent()
  106. }