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

repo_keys.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // https://github.com/gogits/go-gogs-client/wiki/Repositories---Deploy-Keys#add-a-new-deploy-key
  63. func CreateRepoDeployKey(ctx *middleware.Context, form api.CreateDeployKeyOption) {
  64. content, err := models.CheckPublicKeyString(form.Key)
  65. if err != nil {
  66. if models.IsErrKeyUnableVerify(err) {
  67. ctx.APIError(422, "", "Unable to verify key content")
  68. } else {
  69. ctx.APIError(422, "", fmt.Errorf("Invalid key content: %v", err))
  70. }
  71. return
  72. }
  73. key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content)
  74. if err != nil {
  75. ctx.Data["HasError"] = true
  76. switch {
  77. case models.IsErrKeyAlreadyExist(err):
  78. ctx.APIError(422, "", "Key content has been used as non-deploy key")
  79. case models.IsErrKeyNameAlreadyUsed(err):
  80. ctx.APIError(422, "", "Key title has been used")
  81. default:
  82. ctx.APIError(500, "AddDeployKey", err)
  83. }
  84. return
  85. }
  86. key.Content = content
  87. apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name)
  88. ctx.JSON(201, ToApiDeployKey(apiLink, key))
  89. }
  90. // https://github.com/gogits/go-gogs-client/wiki/Repositories---Deploy-Keys#remove-a-deploy-key
  91. func DeleteRepoDeploykey(ctx *middleware.Context) {
  92. if err := models.DeleteDeployKey(ctx.ParamsInt64(":id")); err != nil {
  93. ctx.APIError(500, "DeleteDeployKey", err)
  94. return
  95. }
  96. ctx.Status(204)
  97. }