key.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package repo
  2. import (
  3. "fmt"
  4. "gitote/gitote/models"
  5. "gitote/gitote/pkg/context"
  6. "gitote/gitote/pkg/setting"
  7. "gitote/gitote/routes/api/v1/convert"
  8. api "gitlab.com/gitote/go-gitote-client"
  9. )
  10. func composeDeployKeysAPILink(repoPath string) string {
  11. return setting.AppURL + "api/v1/repos/" + repoPath + "/keys/"
  12. }
  13. func ListDeployKeys(c *context.APIContext) {
  14. keys, err := models.ListDeployKeys(c.Repo.Repository.ID)
  15. if err != nil {
  16. c.Error(500, "ListDeployKeys", err)
  17. return
  18. }
  19. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  20. apiKeys := make([]*api.DeployKey, len(keys))
  21. for i := range keys {
  22. if err = keys[i].GetContent(); err != nil {
  23. c.Error(500, "GetContent", err)
  24. return
  25. }
  26. apiKeys[i] = convert.ToDeployKey(apiLink, keys[i])
  27. }
  28. c.JSON(200, &apiKeys)
  29. }
  30. func GetDeployKey(c *context.APIContext) {
  31. key, err := models.GetDeployKeyByID(c.ParamsInt64(":id"))
  32. if err != nil {
  33. if models.IsErrDeployKeyNotExist(err) {
  34. c.Status(404)
  35. } else {
  36. c.Error(500, "GetDeployKeyByID", err)
  37. }
  38. return
  39. }
  40. if err = key.GetContent(); err != nil {
  41. c.Error(500, "GetContent", err)
  42. return
  43. }
  44. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  45. c.JSON(200, convert.ToDeployKey(apiLink, key))
  46. }
  47. func HandleCheckKeyStringError(c *context.APIContext, err error) {
  48. if models.IsErrKeyUnableVerify(err) {
  49. c.Error(422, "", "Unable to verify key content")
  50. } else {
  51. c.Error(422, "", fmt.Errorf("Invalid key content: %v", err))
  52. }
  53. }
  54. func HandleAddKeyError(c *context.APIContext, err error) {
  55. switch {
  56. case models.IsErrKeyAlreadyExist(err):
  57. c.Error(422, "", "Key content has been used as non-deploy key")
  58. case models.IsErrKeyNameAlreadyUsed(err):
  59. c.Error(422, "", "Key title has been used")
  60. default:
  61. c.Error(500, "AddKey", err)
  62. }
  63. }
  64. func CreateDeployKey(c *context.APIContext, form api.CreateKeyOption) {
  65. content, err := models.CheckPublicKeyString(form.Key)
  66. if err != nil {
  67. HandleCheckKeyStringError(c, err)
  68. return
  69. }
  70. key, err := models.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(201, convert.ToDeployKey(apiLink, key))
  78. }
  79. func DeleteDeploykey(c *context.APIContext) {
  80. if err := models.DeleteDeployKey(c.User, c.ParamsInt64(":id")); err != nil {
  81. if models.IsErrKeyAccessDenied(err) {
  82. c.Error(403, "", "You do not have access to this key")
  83. } else {
  84. c.Error(500, "DeleteDeployKey", err)
  85. }
  86. return
  87. }
  88. c.Status(204)
  89. }