Skip to content

Commit 76659b1

Browse files
JakobDevwxiaoguang
andauthored
Reduce usage of db.DefaultContext (#27073)
Part of #27065 This reduces the usage of `db.DefaultContext`. I think I've got enough files for the first PR. When this is merged, I will continue working on this. Considering how many files this PR affect, I hope it won't take to long to merge, so I don't end up in the merge conflict hell. --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent 0de09d3 commit 76659b1

Some content is hidden

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

83 files changed

+339
-324
lines changed

cmd/admin_user_create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func runCreateUser(c *cli.Context) error {
115115

116116
// If this is the first user being created.
117117
// Take it as the admin and don't force a password update.
118-
if n := user_model.CountUsers(nil); n == 0 {
118+
if n := user_model.CountUsers(ctx, nil); n == 0 {
119119
changePassword = false
120120
}
121121

@@ -146,7 +146,7 @@ func runCreateUser(c *cli.Context) error {
146146
IsRestricted: restricted,
147147
}
148148

149-
if err := user_model.CreateUser(u, overwriteDefault); err != nil {
149+
if err := user_model.CreateUser(ctx, u, overwriteDefault); err != nil {
150150
return fmt.Errorf("CreateUser: %w", err)
151151
}
152152

cmd/admin_user_list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func runListUsers(c *cli.Context) error {
3333
return err
3434
}
3535

36-
users, err := user_model.GetAllUsers()
36+
users, err := user_model.GetAllUsers(ctx)
3737
if err != nil {
3838
return err
3939
}
@@ -48,7 +48,7 @@ func runListUsers(c *cli.Context) error {
4848
}
4949
}
5050
} else {
51-
twofa := user_model.UserList(users).GetTwoFaStatus()
51+
twofa := user_model.UserList(users).GetTwoFaStatus(ctx)
5252
fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\tIsAdmin\t2FA\n")
5353
for _, u := range users {
5454
fmt.Fprintf(w, "%d\t%s\t%s\t%t\t%t\t%t\n", u.ID, u.Name, u.Email, u.IsActive, u.IsAdmin, twofa[u.ID])

models/activities/statistic.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package activities
55

66
import (
7+
"context"
8+
79
asymkey_model "code.gitea.io/gitea/models/asymkey"
810
"code.gitea.io/gitea/models/auth"
911
"code.gitea.io/gitea/models/db"
@@ -47,12 +49,12 @@ type IssueByRepositoryCount struct {
4749
}
4850

4951
// GetStatistic returns the database statistics
50-
func GetStatistic() (stats Statistic) {
51-
e := db.GetEngine(db.DefaultContext)
52-
stats.Counter.User = user_model.CountUsers(nil)
52+
func GetStatistic(ctx context.Context) (stats Statistic) {
53+
e := db.GetEngine(ctx)
54+
stats.Counter.User = user_model.CountUsers(ctx, nil)
5355
stats.Counter.Org, _ = organization.CountOrgs(organization.FindOrgOptions{IncludePrivate: true})
5456
stats.Counter.PublicKey, _ = e.Count(new(asymkey_model.PublicKey))
55-
stats.Counter.Repo, _ = repo_model.CountRepositories(db.DefaultContext, repo_model.CountRepositoryOptions{})
57+
stats.Counter.Repo, _ = repo_model.CountRepositories(ctx, repo_model.CountRepositoryOptions{})
5658
stats.Counter.Watch, _ = e.Count(new(repo_model.Watch))
5759
stats.Counter.Star, _ = e.Count(new(repo_model.Star))
5860
stats.Counter.Access, _ = e.Count(new(access_model.Access))

models/asymkey/gpg_key.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func parseSubGPGKey(ownerID int64, primaryID string, pubkey *packet.PublicKey, e
144144
}
145145

146146
// parseGPGKey parse a PrimaryKey entity (primary key + subs keys + self-signature)
147-
func parseGPGKey(ownerID int64, e *openpgp.Entity, verified bool) (*GPGKey, error) {
147+
func parseGPGKey(ctx context.Context, ownerID int64, e *openpgp.Entity, verified bool) (*GPGKey, error) {
148148
pubkey := e.PrimaryKey
149149
expiry := getExpiryTime(e)
150150

@@ -159,7 +159,7 @@ func parseGPGKey(ownerID int64, e *openpgp.Entity, verified bool) (*GPGKey, erro
159159
}
160160

161161
// Check emails
162-
userEmails, err := user_model.GetEmailAddresses(ownerID)
162+
userEmails, err := user_model.GetEmailAddresses(ctx, ownerID)
163163
if err != nil {
164164
return nil, err
165165
}
@@ -251,7 +251,7 @@ func DeleteGPGKey(doer *user_model.User, id int64) (err error) {
251251
return committer.Commit()
252252
}
253253

254-
func checkKeyEmails(email string, keys ...*GPGKey) (bool, string) {
254+
func checkKeyEmails(ctx context.Context, email string, keys ...*GPGKey) (bool, string) {
255255
uid := int64(0)
256256
var userEmails []*user_model.EmailAddress
257257
var user *user_model.User
@@ -263,10 +263,10 @@ func checkKeyEmails(email string, keys ...*GPGKey) (bool, string) {
263263
}
264264
if key.Verified && key.OwnerID != 0 {
265265
if uid != key.OwnerID {
266-
userEmails, _ = user_model.GetEmailAddresses(key.OwnerID)
266+
userEmails, _ = user_model.GetEmailAddresses(ctx, key.OwnerID)
267267
uid = key.OwnerID
268268
user = &user_model.User{ID: uid}
269-
_, _ = user_model.GetUser(user)
269+
_, _ = user_model.GetUser(ctx, user)
270270
}
271271
for _, e := range userEmails {
272272
if e.IsActivated && (email == "" || strings.EqualFold(e.Email, email)) {

models/asymkey/gpg_key_add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func AddGPGKey(ownerID int64, content, token, signature string) ([]*GPGKey, erro
153153

154154
// Get DB session
155155

156-
key, err := parseGPGKey(ownerID, ekey, verified)
156+
key, err := parseGPGKey(ctx, ownerID, ekey, verified)
157157
if err != nil {
158158
return nil, err
159159
}

models/asymkey/gpg_key_commit_verification.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *CommitVerific
125125

126126
// If this a SSH signature handle it differently
127127
if strings.HasPrefix(c.Signature.Signature, "-----BEGIN SSH SIGNATURE-----") {
128-
return ParseCommitWithSSHSignature(c, committer)
128+
return ParseCommitWithSSHSignature(ctx, c, committer)
129129
}
130130

131131
// Parsing signature
@@ -150,6 +150,7 @@ func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *CommitVerific
150150

151151
// First check if the sig has a keyID and if so just look at that
152152
if commitVerification := hashAndVerifyForKeyID(
153+
ctx,
153154
sig,
154155
c.Signature.Payload,
155156
committer,
@@ -165,7 +166,7 @@ func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *CommitVerific
165166

166167
// Now try to associate the signature with the committer, if present
167168
if committer.ID != 0 {
168-
keys, err := ListGPGKeys(db.DefaultContext, committer.ID, db.ListOptions{})
169+
keys, err := ListGPGKeys(ctx, committer.ID, db.ListOptions{})
169170
if err != nil { // Skipping failed to get gpg keys of user
170171
log.Error("ListGPGKeys: %v", err)
171172
return &CommitVerification{
@@ -175,7 +176,7 @@ func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *CommitVerific
175176
}
176177
}
177178

178-
committerEmailAddresses, _ := user_model.GetEmailAddresses(committer.ID)
179+
committerEmailAddresses, _ := user_model.GetEmailAddresses(ctx, committer.ID)
179180
activated := false
180181
for _, e := range committerEmailAddresses {
181182
if e.IsActivated && strings.EqualFold(e.Email, c.Committer.Email) {
@@ -222,7 +223,7 @@ func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *CommitVerific
222223
}
223224
if err := gpgSettings.LoadPublicKeyContent(); err != nil {
224225
log.Error("Error getting default signing key: %s %v", gpgSettings.KeyID, err)
225-
} else if commitVerification := verifyWithGPGSettings(&gpgSettings, sig, c.Signature.Payload, committer, keyID); commitVerification != nil {
226+
} else if commitVerification := verifyWithGPGSettings(ctx, &gpgSettings, sig, c.Signature.Payload, committer, keyID); commitVerification != nil {
226227
if commitVerification.Reason == BadSignature {
227228
defaultReason = BadSignature
228229
} else {
@@ -237,7 +238,7 @@ func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *CommitVerific
237238
} else if defaultGPGSettings == nil {
238239
log.Warn("Unable to get defaultGPGSettings for unattached commit: %s", c.ID.String())
239240
} else if defaultGPGSettings.Sign {
240-
if commitVerification := verifyWithGPGSettings(defaultGPGSettings, sig, c.Signature.Payload, committer, keyID); commitVerification != nil {
241+
if commitVerification := verifyWithGPGSettings(ctx, defaultGPGSettings, sig, c.Signature.Payload, committer, keyID); commitVerification != nil {
241242
if commitVerification.Reason == BadSignature {
242243
defaultReason = BadSignature
243244
} else {
@@ -257,9 +258,9 @@ func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *CommitVerific
257258
}
258259
}
259260

260-
func verifyWithGPGSettings(gpgSettings *git.GPGSettings, sig *packet.Signature, payload string, committer *user_model.User, keyID string) *CommitVerification {
261+
func verifyWithGPGSettings(ctx context.Context, gpgSettings *git.GPGSettings, sig *packet.Signature, payload string, committer *user_model.User, keyID string) *CommitVerification {
261262
// First try to find the key in the db
262-
if commitVerification := hashAndVerifyForKeyID(sig, payload, committer, gpgSettings.KeyID, gpgSettings.Name, gpgSettings.Email); commitVerification != nil {
263+
if commitVerification := hashAndVerifyForKeyID(ctx, sig, payload, committer, gpgSettings.KeyID, gpgSettings.Name, gpgSettings.Email); commitVerification != nil {
263264
return commitVerification
264265
}
265266

@@ -387,7 +388,7 @@ func hashAndVerifyWithSubKeysCommitVerification(sig *packet.Signature, payload s
387388
return nil
388389
}
389390

390-
func hashAndVerifyForKeyID(sig *packet.Signature, payload string, committer *user_model.User, keyID, name, email string) *CommitVerification {
391+
func hashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload string, committer *user_model.User, keyID, name, email string) *CommitVerification {
391392
if keyID == "" {
392393
return nil
393394
}
@@ -417,7 +418,7 @@ func hashAndVerifyForKeyID(sig *packet.Signature, payload string, committer *use
417418
}
418419
}
419420

420-
activated, email := checkKeyEmails(email, append([]*GPGKey{key}, primaryKeys...)...)
421+
activated, email := checkKeyEmails(ctx, email, append([]*GPGKey{key}, primaryKeys...)...)
421422
if !activated {
422423
continue
423424
}
@@ -427,7 +428,7 @@ func hashAndVerifyForKeyID(sig *packet.Signature, payload string, committer *use
427428
Email: email,
428429
}
429430
if key.OwnerID != 0 {
430-
owner, err := user_model.GetUserByID(db.DefaultContext, key.OwnerID)
431+
owner, err := user_model.GetUserByID(ctx, key.OwnerID)
431432
if err == nil {
432433
signer = owner
433434
} else if !user_model.IsErrUserNotExist(err) {

models/asymkey/ssh_key_commit_verification.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package asymkey
55

66
import (
77
"bytes"
8+
"context"
89
"fmt"
910
"strings"
1011

@@ -17,7 +18,7 @@ import (
1718
)
1819

1920
// ParseCommitWithSSHSignature check if signature is good against keystore.
20-
func ParseCommitWithSSHSignature(c *git.Commit, committer *user_model.User) *CommitVerification {
21+
func ParseCommitWithSSHSignature(ctx context.Context, c *git.Commit, committer *user_model.User) *CommitVerification {
2122
// Now try to associate the signature with the committer, if present
2223
if committer.ID != 0 {
2324
keys, err := ListPublicKeys(committer.ID, db.ListOptions{})
@@ -30,7 +31,7 @@ func ParseCommitWithSSHSignature(c *git.Commit, committer *user_model.User) *Com
3031
}
3132
}
3233

33-
committerEmailAddresses, err := user_model.GetEmailAddresses(committer.ID)
34+
committerEmailAddresses, err := user_model.GetEmailAddresses(ctx, committer.ID)
3435
if err != nil {
3536
log.Error("GetEmailAddresses: %v", err)
3637
}

models/asymkey/ssh_key_principals.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package asymkey
55

66
import (
7+
"context"
78
"fmt"
89
"strings"
910

@@ -63,7 +64,7 @@ func AddPrincipalKey(ownerID int64, content string, authSourceID int64) (*Public
6364
}
6465

6566
// CheckPrincipalKeyString strips spaces and returns an error if the given principal contains newlines
66-
func CheckPrincipalKeyString(user *user_model.User, content string) (_ string, err error) {
67+
func CheckPrincipalKeyString(ctx context.Context, user *user_model.User, content string) (_ string, err error) {
6768
if setting.SSH.Disabled {
6869
return "", db.ErrSSHDisabled{}
6970
}
@@ -80,7 +81,7 @@ func CheckPrincipalKeyString(user *user_model.User, content string) (_ string, e
8081
case "anything":
8182
return content, nil
8283
case "email":
83-
emails, err := user_model.GetEmailAddresses(user.ID)
84+
emails, err := user_model.GetEmailAddresses(ctx, user.ID)
8485
if err != nil {
8586
return "", err
8687
}

0 commit comments

Comments
 (0)