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.

serv.go 2.7KB

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