repo_keys.go 3.5 KB

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