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.

key.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2018 The Gitea 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 private
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. // UpdateDeployKeyUpdated update deploy key updates
  13. func UpdateDeployKeyUpdated(keyID int64, repoID int64) error {
  14. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repositories/%d/keys/%d/update", repoID, keyID)
  15. log.GitLogger.Trace("UpdateDeployKeyUpdated: %s", reqURL)
  16. resp, err := newInternalRequest(reqURL, "POST").Response()
  17. if err != nil {
  18. return err
  19. }
  20. defer resp.Body.Close()
  21. // All 2XX status codes are accepted and others will return an error
  22. if resp.StatusCode/100 != 2 {
  23. return fmt.Errorf("Failed to update deploy key: %s", decodeJSONError(resp).Err)
  24. }
  25. return nil
  26. }
  27. // HasDeployKey check if repo has deploy key
  28. func HasDeployKey(keyID, repoID int64) (bool, error) {
  29. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repositories/%d/has-keys/%d", repoID, keyID)
  30. log.GitLogger.Trace("HasDeployKey: %s", reqURL)
  31. resp, err := newInternalRequest(reqURL, "GET").Response()
  32. if err != nil {
  33. return false, err
  34. }
  35. defer resp.Body.Close()
  36. if resp.StatusCode == 200 {
  37. return true, nil
  38. }
  39. return false, nil
  40. }
  41. // GetPublicKeyByID get public ssh key by his ID
  42. func GetPublicKeyByID(keyID int64) (*models.PublicKey, error) {
  43. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d", keyID)
  44. log.GitLogger.Trace("GetPublicKeyByID: %s", reqURL)
  45. resp, err := newInternalRequest(reqURL, "GET").Response()
  46. if err != nil {
  47. return nil, err
  48. }
  49. defer resp.Body.Close()
  50. if resp.StatusCode != 200 {
  51. return nil, fmt.Errorf("Failed to get repository: %s", decodeJSONError(resp).Err)
  52. }
  53. var pKey models.PublicKey
  54. if err := json.NewDecoder(resp.Body).Decode(&pKey); err != nil {
  55. return nil, err
  56. }
  57. return &pKey, nil
  58. }
  59. // GetUserByKeyID get user attached to key
  60. func GetUserByKeyID(keyID int64) (*models.User, error) {
  61. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/user", keyID)
  62. log.GitLogger.Trace("GetUserByKeyID: %s", reqURL)
  63. resp, err := newInternalRequest(reqURL, "GET").Response()
  64. if err != nil {
  65. return nil, err
  66. }
  67. defer resp.Body.Close()
  68. if resp.StatusCode != 200 {
  69. return nil, fmt.Errorf("Failed to get user: %s", decodeJSONError(resp).Err)
  70. }
  71. var user models.User
  72. if err := json.NewDecoder(resp.Body).Decode(&user); err != nil {
  73. return nil, err
  74. }
  75. return &user, nil
  76. }
  77. // UpdatePublicKeyUpdated update public key updates
  78. func UpdatePublicKeyUpdated(keyID int64) error {
  79. // Ask for running deliver hook and test pull request tasks.
  80. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update", keyID)
  81. log.GitLogger.Trace("UpdatePublicKeyUpdated: %s", reqURL)
  82. resp, err := newInternalRequest(reqURL, "POST").Response()
  83. if err != nil {
  84. return err
  85. }
  86. defer resp.Body.Close()
  87. // All 2XX status codes are accepted and others will return an error
  88. if resp.StatusCode/100 != 2 {
  89. return fmt.Errorf("Failed to update public key: %s", decodeJSONError(resp).Err)
  90. }
  91. return nil
  92. }