gitea源码

hook.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package private
  4. import (
  5. "context"
  6. "fmt"
  7. "net/url"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/httplib"
  10. "code.gitea.io/gitea/modules/repository"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. // Git environment variables
  14. const (
  15. GitAlternativeObjectDirectories = "GIT_ALTERNATE_OBJECT_DIRECTORIES"
  16. GitObjectDirectory = "GIT_OBJECT_DIRECTORY"
  17. GitQuarantinePath = "GIT_QUARANTINE_PATH"
  18. GitPushOptionCount = "GIT_PUSH_OPTION_COUNT"
  19. )
  20. // HookOptions represents the options for the Hook calls
  21. type HookOptions struct {
  22. OldCommitIDs []string
  23. NewCommitIDs []string
  24. RefFullNames []git.RefName
  25. UserID int64
  26. UserName string
  27. GitObjectDirectory string
  28. GitAlternativeObjectDirectories string
  29. GitQuarantinePath string
  30. GitPushOptions GitPushOptions
  31. PullRequestID int64
  32. PushTrigger repository.PushTrigger
  33. DeployKeyID int64 // if the pusher is a DeployKey, then UserID is the repo's org user.
  34. IsWiki bool
  35. ActionPerm int
  36. }
  37. // SSHLogOption ssh log options
  38. type SSHLogOption struct {
  39. IsError bool
  40. Message string
  41. }
  42. // HookPostReceiveResult represents an individual result from PostReceive
  43. type HookPostReceiveResult struct {
  44. Results []HookPostReceiveBranchResult
  45. RepoWasEmpty bool
  46. Err string
  47. }
  48. // HookPostReceiveBranchResult represents an individual branch result from PostReceive
  49. type HookPostReceiveBranchResult struct {
  50. Message bool
  51. Create bool
  52. Branch string
  53. URL string
  54. }
  55. // HookProcReceiveResult represents an individual result from ProcReceive
  56. type HookProcReceiveResult struct {
  57. Results []HookProcReceiveRefResult
  58. Err string
  59. }
  60. // HookProcReceiveRefResult represents an individual result from ProcReceive
  61. type HookProcReceiveRefResult struct {
  62. OldOID string
  63. NewOID string
  64. Ref string
  65. OriginalRef git.RefName
  66. IsForcePush bool
  67. IsNotMatched bool
  68. Err string
  69. IsCreatePR bool
  70. URL string
  71. ShouldShowMessage bool
  72. HeadBranch string
  73. }
  74. func newInternalRequestAPIForHooks(ctx context.Context, hookName, ownerName, repoName string, opts HookOptions) *httplib.Request {
  75. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/%s/%s/%s", hookName, url.PathEscape(ownerName), url.PathEscape(repoName))
  76. req := newInternalRequestAPI(ctx, reqURL, "POST", opts)
  77. // This "timeout" applies to http.Client's timeout: A Timeout of zero means no timeout.
  78. // This "timeout" was previously set to `time.Duration(60+len(opts.OldCommitIDs))` seconds, but it caused unnecessary timeout failures.
  79. // It should be good enough to remove the client side timeout, only respect the "ctx" and server side timeout.
  80. req.SetReadWriteTimeout(0)
  81. return req
  82. }
  83. // HookPreReceive check whether the provided commits are allowed
  84. func HookPreReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) ResponseExtra {
  85. req := newInternalRequestAPIForHooks(ctx, "pre-receive", ownerName, repoName, opts)
  86. _, extra := requestJSONResp(req, &ResponseText{})
  87. return extra
  88. }
  89. // HookPostReceive updates services and users
  90. func HookPostReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) (*HookPostReceiveResult, ResponseExtra) {
  91. req := newInternalRequestAPIForHooks(ctx, "post-receive", ownerName, repoName, opts)
  92. return requestJSONResp(req, &HookPostReceiveResult{})
  93. }
  94. // HookProcReceive proc-receive hook
  95. func HookProcReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) (*HookProcReceiveResult, ResponseExtra) {
  96. req := newInternalRequestAPIForHooks(ctx, "proc-receive", ownerName, repoName, opts)
  97. return requestJSONResp(req, &HookProcReceiveResult{})
  98. }
  99. // SetDefaultBranch will set the default branch to the provided branch for the provided repository
  100. func SetDefaultBranch(ctx context.Context, ownerName, repoName, branch string) ResponseExtra {
  101. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/set-default-branch/%s/%s/%s",
  102. url.PathEscape(ownerName),
  103. url.PathEscape(repoName),
  104. url.PathEscape(branch),
  105. )
  106. req := newInternalRequestAPI(ctx, reqURL, "POST")
  107. _, extra := requestJSONResp(req, &ResponseText{})
  108. return extra
  109. }
  110. // SSHLog sends ssh error log response
  111. func SSHLog(ctx context.Context, isErr bool, msg string) error {
  112. reqURL := setting.LocalURL + "api/internal/ssh/log"
  113. req := newInternalRequestAPI(ctx, reqURL, "POST", &SSHLogOption{IsError: isErr, Message: msg})
  114. _, extra := requestJSONResp(req, &ResponseText{})
  115. return extra.Error
  116. }