Skip to content

Commit 188e55b

Browse files
committed
Use context cache for loading pusher
1 parent e65d6fc commit 188e55b

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

routers/private/hook_post_receive.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
pull_model "code.gitea.io/gitea/models/pull"
1616
repo_model "code.gitea.io/gitea/models/repo"
1717
user_model "code.gitea.io/gitea/models/user"
18+
"code.gitea.io/gitea/modules/cache"
1819
"code.gitea.io/gitea/modules/git"
1920
"code.gitea.io/gitea/modules/gitrepo"
2021
"code.gitea.io/gitea/modules/log"
@@ -162,10 +163,8 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
162163
}
163164
}
164165

165-
var pusher *user_model.User
166-
167166
// 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)
169168
if ctx.Written() {
170169
return
171170
}
@@ -184,17 +183,17 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
184183
wasEmpty = repo.IsEmpty
185184
}
186185

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
197195
}
196+
198197
perm, err := access_model.GetUserRepoPermission(ctx, repo, pusher)
199198
if err != nil {
200199
log.Error("Failed to Update: %s/%s Error: %v", ownerName, repoName, err)
@@ -323,7 +322,9 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
323322
})
324323
}
325324

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) {
327328
// handle pull request merging, a pull request action should only push 1 commit
328329
if opts.PullRequestAction == repo_module.PullRequestActionMerge && len(updates) >= 1 {
329330
// Get the pull request
@@ -336,15 +337,15 @@ func handlePullRequestMerging(ctx *gitea_context.PrivateContext, opts *private.H
336337
return
337338
}
338339

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
348349
}
349350

350351
pr.MergedCommitID = updates[len(updates)-1].NewCommitID

0 commit comments

Comments
 (0)