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.

106 lines
2.7 KiB

  1. // Copyright 2019 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. "net/http"
  9. "net/url"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. // KeyAndOwner is the response from ServNoCommand
  14. type KeyAndOwner struct {
  15. Key *models.PublicKey `json:"key"`
  16. Owner *models.User `json:"user"`
  17. }
  18. // ServNoCommand returns information about the provided key
  19. func ServNoCommand(keyID int64) (*models.PublicKey, *models.User, error) {
  20. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/serv/none/%d",
  21. keyID)
  22. resp, err := newInternalRequest(reqURL, "GET").Response()
  23. if err != nil {
  24. return nil, nil, err
  25. }
  26. defer resp.Body.Close()
  27. if resp.StatusCode != http.StatusOK {
  28. return nil, nil, fmt.Errorf("%s", decodeJSONError(resp).Err)
  29. }
  30. var keyAndOwner KeyAndOwner
  31. if err := json.NewDecoder(resp.Body).Decode(&keyAndOwner); err != nil {
  32. return nil, nil, err
  33. }
  34. return keyAndOwner.Key, keyAndOwner.Owner, nil
  35. }
  36. // ServCommandResults are the results of a call to the private route serv
  37. type ServCommandResults struct {
  38. IsWiki bool
  39. IsDeployKey bool
  40. KeyID int64
  41. KeyName string
  42. UserName string
  43. UserID int64
  44. OwnerName string
  45. RepoName string
  46. RepoID int64
  47. }
  48. // ErrServCommand is an error returned from ServCommmand.
  49. type ErrServCommand struct {
  50. Results ServCommandResults
  51. Type string
  52. Err string
  53. StatusCode int
  54. }
  55. func (err ErrServCommand) Error() string {
  56. return err.Err
  57. }
  58. // IsErrServCommand checks if an error is a ErrServCommand.
  59. func IsErrServCommand(err error) bool {
  60. _, ok := err.(ErrServCommand)
  61. return ok
  62. }
  63. // ServCommand preps for a serv call
  64. func ServCommand(keyID int64, ownerName, repoName string, mode models.AccessMode, verbs ...string) (*ServCommandResults, error) {
  65. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/serv/command/%d/%s/%s?mode=%d",
  66. keyID,
  67. url.PathEscape(ownerName),
  68. url.PathEscape(repoName),
  69. mode)
  70. for _, verb := range verbs {
  71. if verb != "" {
  72. reqURL += fmt.Sprintf("&verb=%s", url.QueryEscape(verb))
  73. }
  74. }
  75. resp, err := newInternalRequest(reqURL, "GET").Response()
  76. if err != nil {
  77. return nil, err
  78. }
  79. defer resp.Body.Close()
  80. if resp.StatusCode != http.StatusOK {
  81. var errServCommand ErrServCommand
  82. if err := json.NewDecoder(resp.Body).Decode(&errServCommand); err != nil {
  83. return nil, err
  84. }
  85. errServCommand.StatusCode = resp.StatusCode
  86. return nil, errServCommand
  87. }
  88. var results ServCommandResults
  89. if err := json.NewDecoder(resp.Body).Decode(&results); err != nil {
  90. return nil, err
  91. }
  92. return &results, nil
  93. }