Skip to content

Commit 85108f7

Browse files
committed
fix: pushUpdates with ctx
1 parent d27d36f commit 85108f7

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

services/repository/push.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ var pushQueue queue.Queue
3434
// handle passed PR IDs and test the PRs
3535
func handle(data ...queue.Data) []queue.Data {
3636
for _, datum := range data {
37+
// use new context every loop, to avoid keeping cache data forever
38+
ctx := cache.WithCacheContext(db.DefaultContext)
39+
3740
opts := datum.([]*repo_module.PushUpdateOptions)
38-
if err := pushUpdates(opts); err != nil {
41+
if err := pushUpdates(ctx, opts); err != nil {
3942
log.Error("pushUpdate failed: %v", err)
4043
}
4144
}
@@ -73,7 +76,7 @@ func PushUpdates(opts []*repo_module.PushUpdateOptions) error {
7376
}
7477

7578
// pushUpdates generates push action history feeds for push updating multiple refs
76-
func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
79+
func pushUpdates(ctx context.Context, optsList []*repo_module.PushUpdateOptions) error {
7780
if len(optsList) == 0 {
7881
return nil
7982
}
@@ -122,15 +125,15 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
122125
tagName := opts.TagName()
123126
if opts.IsDelRef() {
124127
notification.NotifyPushCommits(
125-
db.DefaultContext, pusher, repo,
128+
ctx, pusher, repo,
126129
&repo_module.PushUpdateOptions{
127130
RefFullName: git.TagPrefix + tagName,
128131
OldCommitID: opts.OldCommitID,
129132
NewCommitID: git.EmptySHA,
130133
}, repo_module.NewPushCommits())
131134

132135
delTags = append(delTags, tagName)
133-
notification.NotifyDeleteRef(db.DefaultContext, pusher, repo, "tag", opts.RefFullName)
136+
notification.NotifyDeleteRef(ctx, pusher, repo, "tag", opts.RefFullName)
134137
} else { // is new tag
135138
newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
136139
if err != nil {
@@ -142,15 +145,15 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
142145
commits.CompareURL = repo.ComposeCompareURL(git.EmptySHA, opts.NewCommitID)
143146

144147
notification.NotifyPushCommits(
145-
db.DefaultContext, pusher, repo,
148+
ctx, pusher, repo,
146149
&repo_module.PushUpdateOptions{
147150
RefFullName: git.TagPrefix + tagName,
148151
OldCommitID: git.EmptySHA,
149152
NewCommitID: opts.NewCommitID,
150153
}, commits)
151154

152155
addTags = append(addTags, tagName)
153-
notification.NotifyCreateRef(db.DefaultContext, pusher, repo, "tag", opts.RefFullName, opts.NewCommitID)
156+
notification.NotifyCreateRef(ctx, pusher, repo, "tag", opts.RefFullName, opts.NewCommitID)
154157
}
155158
} else if opts.IsBranch() { // If is branch reference
156159
if pusher == nil || pusher.ID != opts.PusherID {
@@ -190,7 +193,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
190193
}
191194
}
192195
// Update the is empty and default_branch columns
193-
if err := repo_model.UpdateRepositoryCols(db.DefaultContext, repo, "default_branch", "is_empty"); err != nil {
196+
if err := repo_model.UpdateRepositoryCols(ctx, repo, "default_branch", "is_empty"); err != nil {
194197
return fmt.Errorf("UpdateRepositoryCols: %w", err)
195198
}
196199
}
@@ -199,7 +202,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
199202
if err != nil {
200203
return fmt.Errorf("newCommit.CommitsBeforeLimit: %w", err)
201204
}
202-
notification.NotifyCreateRef(db.DefaultContext, pusher, repo, "branch", opts.RefFullName, opts.NewCommitID)
205+
notification.NotifyCreateRef(ctx, pusher, repo, "branch", opts.RefFullName, opts.NewCommitID)
203206
} else {
204207
l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
205208
if err != nil {
@@ -259,7 +262,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
259262
commits.Commits = commits.Commits[:setting.UI.FeedMaxCommitNum]
260263
}
261264

262-
notification.NotifyPushCommits(db.DefaultContext, pusher, repo, opts, commits)
265+
notification.NotifyPushCommits(ctx, pusher, repo, opts, commits)
263266

264267
if err = git_model.RemoveDeletedBranchByName(ctx, repo.ID, branch); err != nil {
265268
log.Error("models.RemoveDeletedBranch %s/%s failed: %v", repo.ID, branch, err)
@@ -270,22 +273,22 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
270273
log.Error("repo_module.CacheRef %s/%s failed: %v", repo.ID, branch, err)
271274
}
272275
} else {
273-
notification.NotifyDeleteRef(db.DefaultContext, pusher, repo, "branch", opts.RefFullName)
276+
notification.NotifyDeleteRef(ctx, pusher, repo, "branch", opts.RefFullName)
274277
if err = pull_service.CloseBranchPulls(pusher, repo.ID, branch); err != nil {
275278
// close all related pulls
276279
log.Error("close related pull request failed: %v", err)
277280
}
278281
}
279282

280283
// Even if user delete a branch on a repository which he didn't watch, he will be watch that.
281-
if err = repo_model.WatchIfAuto(db.DefaultContext, opts.PusherID, repo.ID, true); err != nil {
284+
if err = repo_model.WatchIfAuto(ctx, opts.PusherID, repo.ID, true); err != nil {
282285
log.Warn("Fail to perform auto watch on user %v for repo %v: %v", opts.PusherID, repo.ID, err)
283286
}
284287
} else {
285288
log.Trace("Non-tag and non-branch commits pushed.")
286289
}
287290
}
288-
if err := PushUpdateAddDeleteTags(repo, gitRepo, addTags, delTags); err != nil {
291+
if err := PushUpdateAddDeleteTags(ctx, repo, gitRepo, addTags, delTags); err != nil {
289292
return fmt.Errorf("PushUpdateAddDeleteTags: %w", err)
290293
}
291294

@@ -298,8 +301,8 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
298301
}
299302

300303
// PushUpdateAddDeleteTags updates a number of added and delete tags
301-
func PushUpdateAddDeleteTags(repo *repo_model.Repository, gitRepo *git.Repository, addTags, delTags []string) error {
302-
return db.WithTx(db.DefaultContext, func(ctx context.Context) error {
304+
func PushUpdateAddDeleteTags(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, addTags, delTags []string) error {
305+
return db.WithTx(ctx, func(ctx context.Context) error {
303306
if err := repo_model.PushUpdateDeleteTagsContext(ctx, repo, delTags); err != nil {
304307
return err
305308
}

0 commit comments

Comments
 (0)