You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

key.go 3.6 kB

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