key.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. "fmt"
  7. convert2 "gogs.io/gogs/internal/route/api/v1/convert"
  8. api "github.com/gogs/go-gogs-client"
  9. "gogs.io/gogs/internal/conf"
  10. "gogs.io/gogs/internal/context"
  11. "gogs.io/gogs/internal/db"
  12. )
  13. func composeDeployKeysAPILink(repoPath string) string {
  14. return conf.Server.ExternalURL + "api/v1/repos/" + repoPath + "/keys/"
  15. }
  16. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#list-deploy-keys
  17. func ListDeployKeys(c *context.APIContext) {
  18. keys, err := db.ListDeployKeys(c.Repo.Repository.ID)
  19. if err != nil {
  20. c.Error(500, "ListDeployKeys", err)
  21. return
  22. }
  23. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  24. apiKeys := make([]*api.DeployKey, len(keys))
  25. for i := range keys {
  26. if err = keys[i].GetContent(); err != nil {
  27. c.Error(500, "GetContent", err)
  28. return
  29. }
  30. apiKeys[i] = convert2.ToDeployKey(apiLink, keys[i])
  31. }
  32. c.JSON(200, &apiKeys)
  33. }
  34. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#get-a-deploy-key
  35. func GetDeployKey(c *context.APIContext) {
  36. key, err := db.GetDeployKeyByID(c.ParamsInt64(":id"))
  37. if err != nil {
  38. if db.IsErrDeployKeyNotExist(err) {
  39. c.Status(404)
  40. } else {
  41. c.Error(500, "GetDeployKeyByID", err)
  42. }
  43. return
  44. }
  45. if err = key.GetContent(); err != nil {
  46. c.Error(500, "GetContent", err)
  47. return
  48. }
  49. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  50. c.JSON(200, convert2.ToDeployKey(apiLink, key))
  51. }
  52. func HandleCheckKeyStringError(c *context.APIContext, err error) {
  53. if db.IsErrKeyUnableVerify(err) {
  54. c.Error(422, "", "Unable to verify key content")
  55. } else {
  56. c.Error(422, "", fmt.Errorf("Invalid key content: %v", err))
  57. }
  58. }
  59. func HandleAddKeyError(c *context.APIContext, err error) {
  60. switch {
  61. case db.IsErrKeyAlreadyExist(err):
  62. c.Error(422, "", "Key content has been used as non-deploy key")
  63. case db.IsErrKeyNameAlreadyUsed(err):
  64. c.Error(422, "", "Key title has been used")
  65. default:
  66. c.Error(500, "AddKey", err)
  67. }
  68. }
  69. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#add-a-new-deploy-key
  70. func CreateDeployKey(c *context.APIContext, form api.CreateKeyOption) {
  71. content, err := db.CheckPublicKeyString(form.Key)
  72. if err != nil {
  73. HandleCheckKeyStringError(c, err)
  74. return
  75. }
  76. key, err := db.AddDeployKey(c.Repo.Repository.ID, form.Title, content)
  77. if err != nil {
  78. HandleAddKeyError(c, err)
  79. return
  80. }
  81. key.Content = content
  82. apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
  83. c.JSON(201, convert2.ToDeployKey(apiLink, key))
  84. }
  85. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#remove-a-deploy-key
  86. func DeleteDeploykey(c *context.APIContext) {
  87. if err := db.DeleteDeployKey(c.User, c.ParamsInt64(":id")); err != nil {
  88. if db.IsErrKeyAccessDenied(err) {
  89. c.Error(403, "", "You do not have access to this key")
  90. } else {
  91. c.Error(500, "DeleteDeployKey", err)
  92. }
  93. return
  94. }
  95. c.Status(204)
  96. }