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.9KB

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