Skip to content

Commit fc7d3f7

Browse files
GiteaBotJakobDevKN4CK3R
authored
Another round of db.DefaultContext refactor (#27103) (#27262)
Backport #27103 by @JakobDev Part of #27065 Co-authored-by: JakobDev <[email protected]> Co-authored-by: KN4CK3R <[email protected]>
1 parent 597b04f commit fc7d3f7

File tree

109 files changed

+353
-306
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+353
-306
lines changed

cmd/admin.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ func runRepoSyncReleases(_ *cli.Context) error {
389389
}
390390
log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)
391391

392-
if err = repo_module.SyncReleasesWithTags(repo, gitRepo); err != nil {
392+
if err = repo_module.SyncReleasesWithTags(ctx, repo, gitRepo); err != nil {
393393
log.Warn(" SyncReleasesWithTags: %v", err)
394394
gitRepo.Close()
395395
continue
@@ -438,7 +438,7 @@ func runRegenerateKeys(_ *cli.Context) error {
438438
if err := initDB(ctx); err != nil {
439439
return err
440440
}
441-
return asymkey_model.RewriteAllPublicKeys()
441+
return asymkey_model.RewriteAllPublicKeys(ctx)
442442
}
443443

444444
func parseOAuth2Config(c *cli.Context) *oauth2.Source {

cmd/migrate_storage_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"testing"
1111

12+
"code.gitea.io/gitea/models/db"
1213
"code.gitea.io/gitea/models/packages"
1314
"code.gitea.io/gitea/models/unittest"
1415
user_model "code.gitea.io/gitea/models/user"
@@ -30,7 +31,7 @@ func TestMigratePackages(t *testing.T) {
3031
assert.NoError(t, err)
3132
defer buf.Close()
3233

33-
v, f, err := packages_service.CreatePackageAndAddFile(&packages_service.PackageCreationInfo{
34+
v, f, err := packages_service.CreatePackageAndAddFile(db.DefaultContext, &packages_service.PackageCreationInfo{
3435
PackageInfo: packages_service.PackageInfo{
3536
Owner: creator,
3637
PackageType: packages.TypeGeneric,

models/activities/user_heatmap.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package activities
55

66
import (
7+
"context"
8+
79
"code.gitea.io/gitea/models/db"
810
"code.gitea.io/gitea/models/organization"
911
user_model "code.gitea.io/gitea/models/user"
@@ -18,16 +20,16 @@ type UserHeatmapData struct {
1820
}
1921

2022
// GetUserHeatmapDataByUser returns an array of UserHeatmapData
21-
func GetUserHeatmapDataByUser(user, doer *user_model.User) ([]*UserHeatmapData, error) {
22-
return getUserHeatmapData(user, nil, doer)
23+
func GetUserHeatmapDataByUser(ctx context.Context, user, doer *user_model.User) ([]*UserHeatmapData, error) {
24+
return getUserHeatmapData(ctx, user, nil, doer)
2325
}
2426

2527
// GetUserHeatmapDataByUserTeam returns an array of UserHeatmapData
26-
func GetUserHeatmapDataByUserTeam(user *user_model.User, team *organization.Team, doer *user_model.User) ([]*UserHeatmapData, error) {
27-
return getUserHeatmapData(user, team, doer)
28+
func GetUserHeatmapDataByUserTeam(ctx context.Context, user *user_model.User, team *organization.Team, doer *user_model.User) ([]*UserHeatmapData, error) {
29+
return getUserHeatmapData(ctx, user, team, doer)
2830
}
2931

30-
func getUserHeatmapData(user *user_model.User, team *organization.Team, doer *user_model.User) ([]*UserHeatmapData, error) {
32+
func getUserHeatmapData(ctx context.Context, user *user_model.User, team *organization.Team, doer *user_model.User) ([]*UserHeatmapData, error) {
3133
hdata := make([]*UserHeatmapData, 0)
3234

3335
if !ActivityReadable(user, doer) {
@@ -60,7 +62,7 @@ func getUserHeatmapData(user *user_model.User, team *organization.Team, doer *us
6062
return nil, err
6163
}
6264

63-
return hdata, db.GetEngine(db.DefaultContext).
65+
return hdata, db.GetEngine(ctx).
6466
Select(groupBy+" AS timestamp, count(user_id) as contributions").
6567
Table("action").
6668
Where(cond).

models/activities/user_heatmap_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestGetUserHeatmapDataByUser(t *testing.T) {
8383
assert.NoError(t, err)
8484

8585
// Get the heatmap and compare
86-
heatmap, err := activities_model.GetUserHeatmapDataByUser(user, doer)
86+
heatmap, err := activities_model.GetUserHeatmapDataByUser(db.DefaultContext, user, doer)
8787
var contributions int
8888
for _, hm := range heatmap {
8989
contributions += int(hm.Contributions)

models/asymkey/gpg_key.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ func ListGPGKeys(ctx context.Context, uid int64, listOptions db.ListOptions) ([]
8888
}
8989

9090
// CountUserGPGKeys return number of gpg keys a user own
91-
func CountUserGPGKeys(userID int64) (int64, error) {
92-
return db.GetEngine(db.DefaultContext).Where("owner_id=? AND primary_key_id=''", userID).Count(&GPGKey{})
91+
func CountUserGPGKeys(ctx context.Context, userID int64) (int64, error) {
92+
return db.GetEngine(ctx).Where("owner_id=? AND primary_key_id=''", userID).Count(&GPGKey{})
9393
}
9494

9595
// GetGPGKeyByID returns public key by given ID.
96-
func GetGPGKeyByID(keyID int64) (*GPGKey, error) {
96+
func GetGPGKeyByID(ctx context.Context, keyID int64) (*GPGKey, error) {
9797
key := new(GPGKey)
98-
has, err := db.GetEngine(db.DefaultContext).ID(keyID).Get(key)
98+
has, err := db.GetEngine(ctx).ID(keyID).Get(key)
9999
if err != nil {
100100
return nil, err
101101
} else if !has {
@@ -105,9 +105,9 @@ func GetGPGKeyByID(keyID int64) (*GPGKey, error) {
105105
}
106106

107107
// GetGPGKeysByKeyID returns public key by given ID.
108-
func GetGPGKeysByKeyID(keyID string) ([]*GPGKey, error) {
108+
func GetGPGKeysByKeyID(ctx context.Context, keyID string) ([]*GPGKey, error) {
109109
keys := make([]*GPGKey, 0, 1)
110-
return keys, db.GetEngine(db.DefaultContext).Where("key_id=?", keyID).Find(&keys)
110+
return keys, db.GetEngine(ctx).Where("key_id=?", keyID).Find(&keys)
111111
}
112112

113113
// GPGKeyToEntity retrieve the imported key and the traducted entity
@@ -224,8 +224,8 @@ func deleteGPGKey(ctx context.Context, keyID string) (int64, error) {
224224
}
225225

226226
// DeleteGPGKey deletes GPG key information in database.
227-
func DeleteGPGKey(doer *user_model.User, id int64) (err error) {
228-
key, err := GetGPGKeyByID(id)
227+
func DeleteGPGKey(ctx context.Context, doer *user_model.User, id int64) (err error) {
228+
key, err := GetGPGKeyByID(ctx, id)
229229
if err != nil {
230230
if IsErrGPGKeyNotExist(err) {
231231
return nil
@@ -238,7 +238,7 @@ func DeleteGPGKey(doer *user_model.User, id int64) (err error) {
238238
return ErrGPGKeyAccessDenied{doer.ID, key.ID}
239239
}
240240

241-
ctx, committer, err := db.TxContext(db.DefaultContext)
241+
ctx, committer, err := db.TxContext(ctx)
242242
if err != nil {
243243
return err
244244
}

models/asymkey/gpg_key_add.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ func addGPGSubKey(ctx context.Context, key *GPGKey) (err error) {
6666
}
6767

6868
// AddGPGKey adds new public key to database.
69-
func AddGPGKey(ownerID int64, content, token, signature string) ([]*GPGKey, error) {
69+
func AddGPGKey(ctx context.Context, ownerID int64, content, token, signature string) ([]*GPGKey, error) {
7070
ekeys, err := checkArmoredGPGKeyString(content)
7171
if err != nil {
7272
return nil, err
7373
}
7474

75-
ctx, committer, err := db.TxContext(db.DefaultContext)
75+
ctx, committer, err := db.TxContext(ctx)
7676
if err != nil {
7777
return nil, err
7878
}

models/asymkey/gpg_key_commit_verification.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ func hashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload s
392392
if keyID == "" {
393393
return nil
394394
}
395-
keys, err := GetGPGKeysByKeyID(keyID)
395+
keys, err := GetGPGKeysByKeyID(ctx, keyID)
396396
if err != nil {
397397
log.Error("GetGPGKeysByKeyID: %v", err)
398398
return &CommitVerification{
@@ -407,7 +407,7 @@ func hashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload s
407407
for _, key := range keys {
408408
var primaryKeys []*GPGKey
409409
if key.PrimaryKeyID != "" {
410-
primaryKeys, err = GetGPGKeysByKeyID(key.PrimaryKeyID)
410+
primaryKeys, err = GetGPGKeysByKeyID(ctx, key.PrimaryKeyID)
411411
if err != nil {
412412
log.Error("GetGPGKeysByKeyID: %v", err)
413413
return &CommitVerification{

models/asymkey/gpg_key_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88
"time"
99

10+
"code.gitea.io/gitea/models/db"
1011
"code.gitea.io/gitea/models/unittest"
1112
user_model "code.gitea.io/gitea/models/user"
1213
"code.gitea.io/gitea/modules/timeutil"
@@ -228,7 +229,7 @@ Q0KHb+QcycSgbDx0ZAvdIacuKvBBcbxrsmFUI4LR+oIup0G9gUc0roPvr014jYQL
228229
=zHo9
229230
-----END PGP PUBLIC KEY BLOCK-----`
230231

231-
keys, err := AddGPGKey(1, testEmailWithUpperCaseLetters, "", "")
232+
keys, err := AddGPGKey(db.DefaultContext, 1, testEmailWithUpperCaseLetters, "", "")
232233
assert.NoError(t, err)
233234
if assert.NotEmpty(t, keys) {
234235
key := keys[0]

models/asymkey/ssh_key_authorized_keys.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func appendAuthorizedKeysToFile(keys ...*PublicKey) error {
117117
// RewriteAllPublicKeys removes any authorized key and rewrite all keys from database again.
118118
// Note: db.GetEngine(db.DefaultContext).Iterate does not get latest data after insert/delete, so we have to call this function
119119
// outside any session scope independently.
120-
func RewriteAllPublicKeys() error {
120+
func RewriteAllPublicKeys(ctx context.Context) error {
121121
// Don't rewrite key if internal server
122122
if setting.SSH.StartBuiltinServer || !setting.SSH.CreateAuthorizedKeysFile {
123123
return nil
@@ -165,7 +165,7 @@ func RewriteAllPublicKeys() error {
165165
}
166166
}
167167

168-
if err := RegeneratePublicKeys(db.DefaultContext, t); err != nil {
168+
if err := RegeneratePublicKeys(ctx, t); err != nil {
169169
return err
170170
}
171171

models/issues/issue_index.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33

44
package issues
55

6-
import "code.gitea.io/gitea/models/db"
6+
import (
7+
"context"
8+
9+
"code.gitea.io/gitea/models/db"
10+
)
711

812
// RecalculateIssueIndexForRepo create issue_index for repo if not exist and
913
// update it based on highest index of existing issues assigned to a repo
10-
func RecalculateIssueIndexForRepo(repoID int64) error {
11-
ctx, committer, err := db.TxContext(db.DefaultContext)
14+
func RecalculateIssueIndexForRepo(ctx context.Context, repoID int64) error {
15+
ctx, committer, err := db.TxContext(ctx)
1216
if err != nil {
1317
return err
1418
}

models/issues/issue_stats.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ func CountIssues(ctx context.Context, opts *IssuesOptions) (int64, error) {
8080
}
8181

8282
// GetIssueStats returns issue statistic information by given conditions.
83-
func GetIssueStats(opts *IssuesOptions) (*IssueStats, error) {
83+
func GetIssueStats(ctx context.Context, opts *IssuesOptions) (*IssueStats, error) {
8484
if len(opts.IssueIDs) <= MaxQueryParameters {
85-
return getIssueStatsChunk(opts, opts.IssueIDs)
85+
return getIssueStatsChunk(ctx, opts, opts.IssueIDs)
8686
}
8787

8888
// If too long a list of IDs is provided, we get the statistics in
@@ -95,7 +95,7 @@ func GetIssueStats(opts *IssuesOptions) (*IssueStats, error) {
9595
if chunk > len(opts.IssueIDs) {
9696
chunk = len(opts.IssueIDs)
9797
}
98-
stats, err := getIssueStatsChunk(opts, opts.IssueIDs[i:chunk])
98+
stats, err := getIssueStatsChunk(ctx, opts, opts.IssueIDs[i:chunk])
9999
if err != nil {
100100
return nil, err
101101
}
@@ -112,10 +112,10 @@ func GetIssueStats(opts *IssuesOptions) (*IssueStats, error) {
112112
return accum, nil
113113
}
114114

115-
func getIssueStatsChunk(opts *IssuesOptions, issueIDs []int64) (*IssueStats, error) {
115+
func getIssueStatsChunk(ctx context.Context, opts *IssuesOptions, issueIDs []int64) (*IssueStats, error) {
116116
stats := &IssueStats{}
117117

118-
sess := db.GetEngine(db.DefaultContext).
118+
sess := db.GetEngine(ctx).
119119
Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
120120

121121
var err error

models/issues/issue_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ func TestCorrectIssueStats(t *testing.T) {
369369

370370
// Now we will call the GetIssueStats with these IDs and if working,
371371
// get the correct stats back.
372-
issueStats, err := issues_model.GetIssueStats(&issues_model.IssuesOptions{
372+
issueStats, err := issues_model.GetIssueStats(db.DefaultContext, &issues_model.IssuesOptions{
373373
RepoIDs: []int64{1},
374374
IssueIDs: ids,
375375
})

models/issues/pull.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ func (pr *PullRequest) LoadRequestedReviewers(ctx context.Context) error {
311311
return nil
312312
}
313313

314-
reviews, err := GetReviewsByIssueID(pr.Issue.ID)
314+
reviews, err := GetReviewsByIssueID(ctx, pr.Issue.ID)
315315
if err != nil {
316316
return err
317317
}

models/issues/reaction.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ type Reaction struct {
7171
}
7272

7373
// LoadUser load user of reaction
74-
func (r *Reaction) LoadUser() (*user_model.User, error) {
74+
func (r *Reaction) LoadUser(ctx context.Context) (*user_model.User, error) {
7575
if r.User != nil {
7676
return r.User, nil
7777
}
78-
user, err := user_model.GetUserByID(db.DefaultContext, r.UserID)
78+
user, err := user_model.GetUserByID(ctx, r.UserID)
7979
if err != nil {
8080
return nil, err
8181
}
@@ -141,16 +141,16 @@ func (opts *FindReactionsOptions) toConds() builder.Cond {
141141
}
142142

143143
// FindCommentReactions returns a ReactionList of all reactions from an comment
144-
func FindCommentReactions(issueID, commentID int64) (ReactionList, int64, error) {
145-
return FindReactions(db.DefaultContext, FindReactionsOptions{
144+
func FindCommentReactions(ctx context.Context, issueID, commentID int64) (ReactionList, int64, error) {
145+
return FindReactions(ctx, FindReactionsOptions{
146146
IssueID: issueID,
147147
CommentID: commentID,
148148
})
149149
}
150150

151151
// FindIssueReactions returns a ReactionList of all reactions from an issue
152-
func FindIssueReactions(issueID int64, listOptions db.ListOptions) (ReactionList, int64, error) {
153-
return FindReactions(db.DefaultContext, FindReactionsOptions{
152+
func FindIssueReactions(ctx context.Context, issueID int64, listOptions db.ListOptions) (ReactionList, int64, error) {
153+
return FindReactions(ctx, FindReactionsOptions{
154154
ListOptions: listOptions,
155155
IssueID: issueID,
156156
CommentID: -1,
@@ -218,12 +218,12 @@ type ReactionOptions struct {
218218
}
219219

220220
// CreateReaction creates reaction for issue or comment.
221-
func CreateReaction(opts *ReactionOptions) (*Reaction, error) {
221+
func CreateReaction(ctx context.Context, opts *ReactionOptions) (*Reaction, error) {
222222
if !setting.UI.ReactionsLookup.Contains(opts.Type) {
223223
return nil, ErrForbiddenIssueReaction{opts.Type}
224224
}
225225

226-
ctx, committer, err := db.TxContext(db.DefaultContext)
226+
ctx, committer, err := db.TxContext(ctx)
227227
if err != nil {
228228
return nil, err
229229
}
@@ -241,17 +241,17 @@ func CreateReaction(opts *ReactionOptions) (*Reaction, error) {
241241
}
242242

243243
// CreateIssueReaction creates a reaction on issue.
244-
func CreateIssueReaction(doerID, issueID int64, content string) (*Reaction, error) {
245-
return CreateReaction(&ReactionOptions{
244+
func CreateIssueReaction(ctx context.Context, doerID, issueID int64, content string) (*Reaction, error) {
245+
return CreateReaction(ctx, &ReactionOptions{
246246
Type: content,
247247
DoerID: doerID,
248248
IssueID: issueID,
249249
})
250250
}
251251

252252
// CreateCommentReaction creates a reaction on comment.
253-
func CreateCommentReaction(doerID, issueID, commentID int64, content string) (*Reaction, error) {
254-
return CreateReaction(&ReactionOptions{
253+
func CreateCommentReaction(ctx context.Context, doerID, issueID, commentID int64, content string) (*Reaction, error) {
254+
return CreateReaction(ctx, &ReactionOptions{
255255
Type: content,
256256
DoerID: doerID,
257257
IssueID: issueID,
@@ -279,8 +279,8 @@ func DeleteReaction(ctx context.Context, opts *ReactionOptions) error {
279279
}
280280

281281
// DeleteIssueReaction deletes a reaction on issue.
282-
func DeleteIssueReaction(doerID, issueID int64, content string) error {
283-
return DeleteReaction(db.DefaultContext, &ReactionOptions{
282+
func DeleteIssueReaction(ctx context.Context, doerID, issueID int64, content string) error {
283+
return DeleteReaction(ctx, &ReactionOptions{
284284
Type: content,
285285
DoerID: doerID,
286286
IssueID: issueID,
@@ -289,8 +289,8 @@ func DeleteIssueReaction(doerID, issueID int64, content string) error {
289289
}
290290

291291
// DeleteCommentReaction deletes a reaction on comment.
292-
func DeleteCommentReaction(doerID, issueID, commentID int64, content string) error {
293-
return DeleteReaction(db.DefaultContext, &ReactionOptions{
292+
func DeleteCommentReaction(ctx context.Context, doerID, issueID, commentID int64, content string) error {
293+
return DeleteReaction(ctx, &ReactionOptions{
294294
Type: content,
295295
DoerID: doerID,
296296
IssueID: issueID,

0 commit comments

Comments
 (0)