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.

hook.go 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "strconv"
  9. "time"
  10. "code.gitea.io/gitea/modules/git"
  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. // GitPushOptions is a wrapper around a map[string]string
  21. type GitPushOptions map[string]string
  22. // GitPushOptions keys
  23. const (
  24. GitPushOptionRepoPrivate = "repo.private"
  25. GitPushOptionRepoTemplate = "repo.template"
  26. )
  27. // Bool checks for a key in the map and parses as a boolean
  28. func (g GitPushOptions) Bool(key string, def bool) bool {
  29. if val, ok := g[key]; ok {
  30. if b, err := strconv.ParseBool(val); err == nil {
  31. return b
  32. }
  33. }
  34. return def
  35. }
  36. // HookOptions represents the options for the Hook calls
  37. type HookOptions struct {
  38. OldCommitIDs []string
  39. NewCommitIDs []string
  40. RefFullNames []git.RefName
  41. UserID int64
  42. UserName string
  43. GitObjectDirectory string
  44. GitAlternativeObjectDirectories string
  45. GitQuarantinePath string
  46. GitPushOptions GitPushOptions
  47. PullRequestID int64
  48. DeployKeyID int64 // if the pusher is a DeployKey, then UserID is the repo's org user.
  49. IsWiki bool
  50. ActionPerm int
  51. }
  52. // SSHLogOption ssh log options
  53. type SSHLogOption struct {
  54. IsError bool
  55. Message string
  56. }
  57. // HookPostReceiveResult represents an individual result from PostReceive
  58. type HookPostReceiveResult struct {
  59. Results []HookPostReceiveBranchResult
  60. RepoWasEmpty bool
  61. Err string
  62. }
  63. // HookPostReceiveBranchResult represents an individual branch result from PostReceive
  64. type HookPostReceiveBranchResult struct {
  65. Message bool
  66. Create bool
  67. Branch string
  68. URL string
  69. }
  70. // HookProcReceiveResult represents an individual result from ProcReceive
  71. type HookProcReceiveResult struct {
  72. Results []HookProcReceiveRefResult
  73. Err string
  74. }
  75. // HookProcReceiveRefResult represents an individual result from ProcReceive
  76. type HookProcReceiveRefResult struct {
  77. OldOID string
  78. NewOID string
  79. Ref string
  80. OriginalRef git.RefName
  81. IsForcePush bool
  82. IsNotMatched bool
  83. Err string
  84. }
  85. // HookPreReceive check whether the provided commits are allowed
  86. func HookPreReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) ResponseExtra {
  87. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/pre-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName))
  88. req := newInternalRequest(ctx, reqURL, "POST", opts)
  89. req.SetReadWriteTimeout(time.Duration(60+len(opts.OldCommitIDs)) * time.Second)
  90. _, extra := requestJSONResp(req, &responseText{})
  91. return extra
  92. }
  93. // HookPostReceive updates services and users
  94. func HookPostReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) (*HookPostReceiveResult, ResponseExtra) {
  95. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/post-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName))
  96. req := newInternalRequest(ctx, reqURL, "POST", opts)
  97. req.SetReadWriteTimeout(time.Duration(60+len(opts.OldCommitIDs)) * time.Second)
  98. return requestJSONResp(req, &HookPostReceiveResult{})
  99. }
  100. // HookProcReceive proc-receive hook
  101. func HookProcReceive(ctx context.Context, ownerName, repoName string, opts HookOptions) (*HookProcReceiveResult, ResponseExtra) {
  102. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/proc-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName))
  103. req := newInternalRequest(ctx, reqURL, "POST", opts)
  104. req.SetReadWriteTimeout(time.Duration(60+len(opts.OldCommitIDs)) * time.Second)
  105. return requestJSONResp(req, &HookProcReceiveResult{})
  106. }
  107. // SetDefaultBranch will set the default branch to the provided branch for the provided repository
  108. func SetDefaultBranch(ctx context.Context, ownerName, repoName, branch string) ResponseExtra {
  109. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/set-default-branch/%s/%s/%s",
  110. url.PathEscape(ownerName),
  111. url.PathEscape(repoName),
  112. url.PathEscape(branch),
  113. )
  114. req := newInternalRequest(ctx, reqURL, "POST")
  115. _, extra := requestJSONResp(req, &responseText{})
  116. return extra
  117. }
  118. // SSHLog sends ssh error log response
  119. func SSHLog(ctx context.Context, isErr bool, msg string) error {
  120. reqURL := setting.LocalURL + "api/internal/ssh/log"
  121. req := newInternalRequest(ctx, reqURL, "POST", &SSHLogOption{IsError: isErr, Message: msg})
  122. _, extra := requestJSONResp(req, &responseText{})
  123. return extra.Error
  124. }