Skip to content

Commit a2d8be1

Browse files
lunnyAbdulrhmnGhanem
authored andcommitted
Move almost all functions' parameter db.Engine to context.Context (go-gitea#19748)
* Move almost all functions' parameter db.Engine to context.Context * remove some unnecessary wrap functions
1 parent 40af383 commit a2d8be1

File tree

232 files changed

+1448
-2093
lines changed

Some content is hidden

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

232 files changed

+1448
-2093
lines changed

cmd/admin.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ func runChangePassword(c *cli.Context) error {
490490
return errors.New("The password you chose is on a list of stolen passwords previously exposed in public data breaches. Please try again with a different password.\nFor more details, see https://haveibeenpwned.com/Passwords")
491491
}
492492
uname := c.String("username")
493-
user, err := user_model.GetUserByName(uname)
493+
user, err := user_model.GetUserByName(ctx, uname)
494494
if err != nil {
495495
return err
496496
}
@@ -659,7 +659,7 @@ func runDeleteUser(c *cli.Context) error {
659659
if c.IsSet("email") {
660660
user, err = user_model.GetUserByEmail(c.String("email"))
661661
} else if c.IsSet("username") {
662-
user, err = user_model.GetUserByName(c.String("username"))
662+
user, err = user_model.GetUserByName(ctx, c.String("username"))
663663
} else {
664664
user, err = user_model.GetUserByID(c.Int64("id"))
665665
}
@@ -689,7 +689,7 @@ func runGenerateAccessToken(c *cli.Context) error {
689689
return err
690690
}
691691

692-
user, err := user_model.GetUserByName(c.String("username"))
692+
user, err := user_model.GetUserByName(ctx, c.String("username"))
693693
if err != nil {
694694
return err
695695
}

integrations/api_issue_tracked_time_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestAPIGetTrackedTimes(t *testing.T) {
3333
resp := session.MakeRequest(t, req, http.StatusOK)
3434
var apiTimes api.TrackedTimeList
3535
DecodeJSON(t, resp, &apiTimes)
36-
expect, err := models.GetTrackedTimes(&models.FindTrackedTimesOptions{IssueID: issue2.ID})
36+
expect, err := models.GetTrackedTimes(db.DefaultContext, &models.FindTrackedTimesOptions{IssueID: issue2.ID})
3737
assert.NoError(t, err)
3838
assert.Len(t, apiTimes, 3)
3939

@@ -83,15 +83,15 @@ func TestAPIDeleteTrackedTime(t *testing.T) {
8383
session.MakeRequest(t, req, http.StatusNotFound)
8484

8585
// Reset time of user 2 on issue 2
86-
trackedSeconds, err := models.GetTrackedSeconds(models.FindTrackedTimesOptions{IssueID: 2, UserID: 2})
86+
trackedSeconds, err := models.GetTrackedSeconds(db.DefaultContext, models.FindTrackedTimesOptions{IssueID: 2, UserID: 2})
8787
assert.NoError(t, err)
8888
assert.Equal(t, int64(3661), trackedSeconds)
8989

9090
req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, token)
9191
session.MakeRequest(t, req, http.StatusNoContent)
9292
session.MakeRequest(t, req, http.StatusNotFound)
9393

94-
trackedSeconds, err = models.GetTrackedSeconds(models.FindTrackedTimesOptions{IssueID: 2, UserID: 2})
94+
trackedSeconds, err = models.GetTrackedSeconds(db.DefaultContext, models.FindTrackedTimesOptions{IssueID: 2, UserID: 2})
9595
assert.NoError(t, err)
9696
assert.Equal(t, int64(0), trackedSeconds)
9797
}

integrations/api_repo_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func testAPIRepoMigrateConflict(t *testing.T, u *url.URL) {
388388
defer util.RemoveAll(dstPath)
389389
t.Run("CreateRepo", doAPICreateRepository(httpContext, false))
390390

391-
user, err := user_model.GetUserByName(httpContext.Username)
391+
user, err := user_model.GetUserByName(db.DefaultContext, httpContext.Username)
392392
assert.NoError(t, err)
393393
userID := user.ID
394394

integrations/auth_ldap_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func TestLDAPGroupTeamSyncAddMember(t *testing.T) {
321321
addAuthSourceLDAP(t, "", "on", `{"cn=ship_crew,ou=people,dc=planetexpress,dc=com":{"org26": ["team11"]},"cn=admin_staff,ou=people,dc=planetexpress,dc=com": {"non-existent": ["non-existent"]}}`)
322322
org, err := organization.GetOrgByName("org26")
323323
assert.NoError(t, err)
324-
team, err := organization.GetTeam(org.ID, "team11")
324+
team, err := organization.GetTeam(db.DefaultContext, org.ID, "team11")
325325
assert.NoError(t, err)
326326
auth.SyncExternalUsers(context.Background(), true)
327327
for _, gitLDAPUser := range gitLDAPUsers {
@@ -366,7 +366,7 @@ func TestLDAPGroupTeamSyncRemoveMember(t *testing.T) {
366366
addAuthSourceLDAP(t, "", "on", `{"cn=dispatch,ou=people,dc=planetexpress,dc=com": {"org26": ["team11"]}}`)
367367
org, err := organization.GetOrgByName("org26")
368368
assert.NoError(t, err)
369-
team, err := organization.GetTeam(org.ID, "team11")
369+
team, err := organization.GetTeam(db.DefaultContext, org.ID, "team11")
370370
assert.NoError(t, err)
371371
loginUserWithPassword(t, gitLDAPUsers[0].UserName, gitLDAPUsers[0].Password)
372372
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{

integrations/git_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919

2020
"code.gitea.io/gitea/models"
21+
"code.gitea.io/gitea/models/db"
2122
"code.gitea.io/gitea/models/perm"
2223
repo_model "code.gitea.io/gitea/models/repo"
2324
"code.gitea.io/gitea/models/unittest"
@@ -438,7 +439,7 @@ func doProtectBranch(ctx APITestContext, branch, userToWhitelist, unprotectedFil
438439
})
439440
ctx.Session.MakeRequest(t, req, http.StatusSeeOther)
440441
} else {
441-
user, err := user_model.GetUserByName(userToWhitelist)
442+
user, err := user_model.GetUserByName(db.DefaultContext, userToWhitelist)
442443
assert.NoError(t, err)
443444
// Change branch to protected
444445
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings/branches/%s", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), url.PathEscape(branch)), map[string]string{

integrations/mirror_pull_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func TestMirrorPull(t *testing.T) {
7575
IsTag: true,
7676
}, nil, ""))
7777

78-
_, err = repo_model.GetMirrorByRepoID(mirror.ID)
78+
_, err = repo_model.GetMirrorByRepoID(ctx, mirror.ID)
7979
assert.NoError(t, err)
8080

8181
ok := mirror_service.SyncPullMirror(ctx, mirror.ID)

integrations/pull_merge_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919

2020
"code.gitea.io/gitea/models"
21+
"code.gitea.io/gitea/models/db"
2122
repo_model "code.gitea.io/gitea/models/repo"
2223
"code.gitea.io/gitea/models/unittest"
2324
user_model "code.gitea.io/gitea/models/user"
@@ -407,7 +408,7 @@ func TestConflictChecking(t *testing.T) {
407408
assert.NoError(t, err)
408409

409410
issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{Title: "PR with conflict!"}).(*models.Issue)
410-
conflictingPR, err := models.GetPullRequestByIssueID(issue.ID)
411+
conflictingPR, err := models.GetPullRequestByIssueID(db.DefaultContext, issue.ID)
411412
assert.NoError(t, err)
412413

413414
// Ensure conflictedFiles is populated.

integrations/pull_update_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"code.gitea.io/gitea/models"
14+
"code.gitea.io/gitea/models/db"
1415
"code.gitea.io/gitea/models/unittest"
1516
user_model "code.gitea.io/gitea/models/user"
1617
"code.gitea.io/gitea/modules/git"
@@ -165,7 +166,7 @@ func createOutdatedPR(t *testing.T, actor, forkOrg *user_model.User) *models.Pul
165166
assert.NoError(t, err)
166167

167168
issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{Title: "Test Pull -to-update-"}).(*models.Issue)
168-
pr, err := models.GetPullRequestByIssueID(issue.ID)
169+
pr, err := models.GetPullRequestByIssueID(db.DefaultContext, issue.ID)
169170
assert.NoError(t, err)
170171

171172
return pr

models/action.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,8 @@ func (a *Action) getCommentLink(ctx context.Context) string {
222222
if a == nil {
223223
return "#"
224224
}
225-
e := db.GetEngine(ctx)
226225
if a.Comment == nil && a.CommentID != 0 {
227-
a.Comment, _ = getCommentByID(e, a.CommentID)
226+
a.Comment, _ = GetCommentByID(ctx, a.CommentID)
228227
}
229228
if a.Comment != nil {
230229
return a.Comment.HTMLURL()
@@ -239,7 +238,7 @@ func (a *Action) getCommentLink(ctx context.Context) string {
239238
return "#"
240239
}
241240

242-
issue, err := getIssueByID(e, issueID)
241+
issue, err := getIssueByID(ctx, issueID)
243242
if err != nil {
244243
return "#"
245244
}
@@ -340,8 +339,7 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, error) {
340339
return nil, err
341340
}
342341

343-
e := db.GetEngine(ctx)
344-
sess := e.Where(cond).
342+
sess := db.GetEngine(ctx).Where(cond).
345343
Select("`action`.*"). // this line will avoid select other joined table's columns
346344
Join("INNER", "repository", "`repository`.id = `action`.repo_id")
347345

@@ -354,7 +352,7 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, error) {
354352
return nil, fmt.Errorf("Find: %v", err)
355353
}
356354

357-
if err := ActionList(actions).loadAttributes(e); err != nil {
355+
if err := ActionList(actions).loadAttributes(ctx); err != nil {
358356
return nil, fmt.Errorf("LoadAttributes: %v", err)
359357
}
360358

@@ -504,7 +502,7 @@ func notifyWatchers(ctx context.Context, actions ...*Action) error {
504502
permIssue = make([]bool, len(watchers))
505503
permPR = make([]bool, len(watchers))
506504
for i, watcher := range watchers {
507-
user, err := user_model.GetUserByIDEngine(e, watcher.UserID)
505+
user, err := user_model.GetUserByIDCtx(ctx, watcher.UserID)
508506
if err != nil {
509507
permCode[i] = false
510508
permIssue[i] = false

models/action_list.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package models
66

77
import (
8+
"context"
89
"fmt"
910

1011
"code.gitea.io/gitea/models/db"
@@ -26,14 +27,14 @@ func (actions ActionList) getUserIDs() []int64 {
2627
return container.KeysInt64(userIDs)
2728
}
2829

29-
func (actions ActionList) loadUsers(e db.Engine) (map[int64]*user_model.User, error) {
30+
func (actions ActionList) loadUsers(ctx context.Context) (map[int64]*user_model.User, error) {
3031
if len(actions) == 0 {
3132
return nil, nil
3233
}
3334

3435
userIDs := actions.getUserIDs()
3536
userMaps := make(map[int64]*user_model.User, len(userIDs))
36-
err := e.
37+
err := db.GetEngine(ctx).
3738
In("id", userIDs).
3839
Find(&userMaps)
3940
if err != nil {
@@ -56,14 +57,14 @@ func (actions ActionList) getRepoIDs() []int64 {
5657
return container.KeysInt64(repoIDs)
5758
}
5859

59-
func (actions ActionList) loadRepositories(e db.Engine) error {
60+
func (actions ActionList) loadRepositories(ctx context.Context) error {
6061
if len(actions) == 0 {
6162
return nil
6263
}
6364

6465
repoIDs := actions.getRepoIDs()
6566
repoMaps := make(map[int64]*repo_model.Repository, len(repoIDs))
66-
err := e.In("id", repoIDs).Find(&repoMaps)
67+
err := db.GetEngine(ctx).In("id", repoIDs).Find(&repoMaps)
6768
if err != nil {
6869
return fmt.Errorf("find repository: %v", err)
6970
}
@@ -74,7 +75,7 @@ func (actions ActionList) loadRepositories(e db.Engine) error {
7475
return nil
7576
}
7677

77-
func (actions ActionList) loadRepoOwner(e db.Engine, userMap map[int64]*user_model.User) (err error) {
78+
func (actions ActionList) loadRepoOwner(ctx context.Context, userMap map[int64]*user_model.User) (err error) {
7879
if userMap == nil {
7980
userMap = make(map[int64]*user_model.User)
8081
}
@@ -85,7 +86,7 @@ func (actions ActionList) loadRepoOwner(e db.Engine, userMap map[int64]*user_mod
8586
}
8687
repoOwner, ok := userMap[action.Repo.OwnerID]
8788
if !ok {
88-
repoOwner, err = user_model.GetUserByID(action.Repo.OwnerID)
89+
repoOwner, err = user_model.GetUserByIDCtx(ctx, action.Repo.OwnerID)
8990
if err != nil {
9091
if user_model.IsErrUserNotExist(err) {
9192
continue
@@ -101,15 +102,15 @@ func (actions ActionList) loadRepoOwner(e db.Engine, userMap map[int64]*user_mod
101102
}
102103

103104
// loadAttributes loads all attributes
104-
func (actions ActionList) loadAttributes(e db.Engine) error {
105-
userMap, err := actions.loadUsers(e)
105+
func (actions ActionList) loadAttributes(ctx context.Context) error {
106+
userMap, err := actions.loadUsers(ctx)
106107
if err != nil {
107108
return err
108109
}
109110

110-
if err := actions.loadRepositories(e); err != nil {
111+
if err := actions.loadRepositories(ctx); err != nil {
111112
return err
112113
}
113114

114-
return actions.loadRepoOwner(e, userMap)
115+
return actions.loadRepoOwner(ctx, userMap)
115116
}

models/asymkey/gpg_key.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,16 @@ func parseGPGKey(ownerID int64, e *openpgp.Entity, verified bool) (*GPGKey, erro
198198
}
199199

200200
// deleteGPGKey does the actual key deletion
201-
func deleteGPGKey(e db.Engine, keyID string) (int64, error) {
201+
func deleteGPGKey(ctx context.Context, keyID string) (int64, error) {
202202
if keyID == "" {
203203
return 0, fmt.Errorf("empty KeyId forbidden") // Should never happen but just to be sure
204204
}
205205
// Delete imported key
206-
n, err := e.Where("key_id=?", keyID).Delete(new(GPGKeyImport))
206+
n, err := db.GetEngine(ctx).Where("key_id=?", keyID).Delete(new(GPGKeyImport))
207207
if err != nil {
208208
return n, err
209209
}
210-
return e.Where("key_id=?", keyID).Or("primary_key_id=?", keyID).Delete(new(GPGKey))
210+
return db.GetEngine(ctx).Where("key_id=?", keyID).Or("primary_key_id=?", keyID).Delete(new(GPGKey))
211211
}
212212

213213
// DeleteGPGKey deletes GPG key information in database.
@@ -231,7 +231,7 @@ func DeleteGPGKey(doer *user_model.User, id int64) (err error) {
231231
}
232232
defer committer.Close()
233233

234-
if _, err = deleteGPGKey(db.GetEngine(ctx), key.KeyID); err != nil {
234+
if _, err = deleteGPGKey(ctx, key.KeyID); err != nil {
235235
return err
236236
}
237237

models/asymkey/gpg_key_add.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package asymkey
66

77
import (
8+
"context"
89
"strings"
910

1011
"code.gitea.io/gitea/models/db"
@@ -29,36 +30,36 @@ import (
2930
// This file contains functions relating to adding GPG Keys
3031

3132
// addGPGKey add key, import and subkeys to database
32-
func addGPGKey(e db.Engine, key *GPGKey, content string) (err error) {
33+
func addGPGKey(ctx context.Context, key *GPGKey, content string) (err error) {
3334
// Add GPGKeyImport
34-
if _, err = e.Insert(GPGKeyImport{
35+
if err = db.Insert(ctx, &GPGKeyImport{
3536
KeyID: key.KeyID,
3637
Content: content,
3738
}); err != nil {
3839
return err
3940
}
4041
// Save GPG primary key.
41-
if _, err = e.Insert(key); err != nil {
42+
if err = db.Insert(ctx, key); err != nil {
4243
return err
4344
}
4445
// Save GPG subs key.
4546
for _, subkey := range key.SubsKey {
46-
if err := addGPGSubKey(e, subkey); err != nil {
47+
if err := addGPGSubKey(ctx, subkey); err != nil {
4748
return err
4849
}
4950
}
5051
return nil
5152
}
5253

5354
// addGPGSubKey add subkeys to database
54-
func addGPGSubKey(e db.Engine, key *GPGKey) (err error) {
55+
func addGPGSubKey(ctx context.Context, key *GPGKey) (err error) {
5556
// Save GPG primary key.
56-
if _, err = e.Insert(key); err != nil {
57+
if err = db.Insert(ctx, key); err != nil {
5758
return err
5859
}
5960
// Save GPG subs key.
6061
for _, subkey := range key.SubsKey {
61-
if err := addGPGSubKey(e, subkey); err != nil {
62+
if err := addGPGSubKey(ctx, subkey); err != nil {
6263
return err
6364
}
6465
}
@@ -158,7 +159,7 @@ func AddGPGKey(ownerID int64, content, token, signature string) ([]*GPGKey, erro
158159
return nil, err
159160
}
160161

161-
if err = addGPGKey(db.GetEngine(ctx), key, content); err != nil {
162+
if err = addGPGKey(ctx, key, content); err != nil {
162163
return nil, err
163164
}
164165
keys = append(keys, key)

0 commit comments

Comments
 (0)