@@ -15,6 +15,7 @@ import (
15
15
pull_model "code.gitea.io/gitea/models/pull"
16
16
repo_model "code.gitea.io/gitea/models/repo"
17
17
user_model "code.gitea.io/gitea/models/user"
18
+ "code.gitea.io/gitea/modules/cache"
18
19
"code.gitea.io/gitea/modules/git"
19
20
"code.gitea.io/gitea/modules/gitrepo"
20
21
"code.gitea.io/gitea/modules/log"
@@ -162,10 +163,8 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
162
163
}
163
164
}
164
165
165
- var pusher * user_model.User
166
-
167
166
// handle pull request merging, a pull request action should push at least 1 commit
168
- handlePullRequestMerging (ctx , opts , pusher , ownerName , repoName , updates )
167
+ handlePullRequestMerging (ctx , opts , ownerName , repoName , updates )
169
168
if ctx .Written () {
170
169
return
171
170
}
@@ -184,17 +183,17 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
184
183
wasEmpty = repo .IsEmpty
185
184
}
186
185
187
- if pusher == nil {
188
- var err error
189
- pusher , err = user_model .GetUserByID (ctx , opts .UserID )
190
- if err != nil {
191
- log .Error ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
192
- ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
193
- Err : fmt .Sprintf ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err ),
194
- })
195
- return
196
- }
186
+ pusher , err := cache .GetWithContextCache (ctx , contextCachePusherKey , opts .UserID , func () (* user_model.User , error ) {
187
+ return user_model .GetUserByID (ctx , opts .UserID )
188
+ })
189
+ if err != nil {
190
+ log .Error ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
191
+ ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
192
+ Err : fmt .Sprintf ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err ),
193
+ })
194
+ return
197
195
}
196
+
198
197
perm , err := access_model .GetUserRepoPermission (ctx , repo , pusher )
199
198
if err != nil {
200
199
log .Error ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
@@ -323,7 +322,9 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
323
322
})
324
323
}
325
324
326
- func handlePullRequestMerging (ctx * gitea_context.PrivateContext , opts * private.HookOptions , pusher * user_model.User , ownerName , repoName string , updates []* repo_module.PushUpdateOptions ) {
325
+ const contextCachePusherKey = "hook_post_receive_pusher"
326
+
327
+ func handlePullRequestMerging (ctx * gitea_context.PrivateContext , opts * private.HookOptions , ownerName , repoName string , updates []* repo_module.PushUpdateOptions ) {
327
328
// handle pull request merging, a pull request action should only push 1 commit
328
329
if opts .PullRequestAction == repo_module .PullRequestActionMerge && len (updates ) >= 1 {
329
330
// Get the pull request
@@ -336,15 +337,15 @@ func handlePullRequestMerging(ctx *gitea_context.PrivateContext, opts *private.H
336
337
return
337
338
}
338
339
339
- if pusher == nil {
340
- pusher , err = user_model .GetUserByID (ctx , opts .UserID )
341
- if err != nil {
342
- log . Error ( "Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
343
- ctx . JSON ( http . StatusInternalServerError , private. HookPostReceiveResult {
344
- Err : fmt . Sprintf ( "Failed to Update: %s/%s Error: %v" , ownerName , repoName , err ),
345
- })
346
- return
347
- }
340
+ pusher , err := cache . GetWithContextCache ( ctx , contextCachePusherKey , opts . UserID , func () ( * user_model. User , error ) {
341
+ return user_model .GetUserByID (ctx , opts .UserID )
342
+ })
343
+ if err != nil {
344
+ log . Error ( "Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
345
+ ctx . JSON ( http . StatusInternalServerError , private. HookPostReceiveResult {
346
+ Err : fmt . Sprintf ( "Failed to Update: %s/%s Error: %v" , ownerName , repoName , err ),
347
+ })
348
+ return
348
349
}
349
350
350
351
pr .MergedCommitID = updates [len (updates )- 1 ].NewCommitID
0 commit comments