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