Skip to content

Commit 22686cd

Browse files
committed
some improvements
1 parent 2217213 commit 22686cd

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

models/user/user.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,24 +1187,27 @@ func GetUsersByEmails(ctx context.Context, emails []string) (map[string]*User, e
11871187
for _, email := range emailAddresses {
11881188
userIDs.Add(email.UID)
11891189
}
1190-
users, err := GetUsersMapByIDs(ctx, userIDs.Values())
1191-
if err != nil {
1192-
return nil, err
1193-
}
1194-
11951190
results := make(map[string]*User, len(emails))
1196-
for _, email := range emailAddresses {
1197-
user := users[email.UID]
1198-
if user != nil {
1199-
if user.KeepEmailPrivate {
1200-
results[user.LowerName+"@"+setting.Service.NoReplyAddress] = user
1201-
} else {
1202-
results[email.Email] = user
1191+
1192+
if len(userIDs) > 0 {
1193+
users, err := GetUsersMapByIDs(ctx, userIDs.Values())
1194+
if err != nil {
1195+
return nil, err
1196+
}
1197+
1198+
for _, email := range emailAddresses {
1199+
user := users[email.UID]
1200+
if user != nil {
1201+
if user.KeepEmailPrivate {
1202+
results[user.LowerName+"@"+setting.Service.NoReplyAddress] = user
1203+
} else {
1204+
results[email.Email] = user
1205+
}
12031206
}
12041207
}
12051208
}
12061209

1207-
users = make(map[int64]*User, len(needCheckUserNames))
1210+
users := make(map[int64]*User, len(needCheckUserNames))
12081211
if err := db.GetEngine(ctx).In("lower_name", needCheckUserNames.Values()).Find(&users); err != nil {
12091212
return nil, err
12101213
}

services/asymkey/commit.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *asymkey_model
4848
return ParseCommitWithSignatureCommitter(ctx, c, committer)
4949
}
5050

51-
const cacheUserEmailAddressKey = "gpg_user_email_address"
51+
const (
52+
cacheUserEmailAddressKey = "gpg_user_email_address"
53+
cacheUserKey = "gpg_user"
54+
cacheGPGListKey = "gpg_key_list"
55+
)
5256

5357
func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, committer *user_model.User) *asymkey_model.CommitVerification {
5458
// If no signature just report the committer
@@ -214,10 +218,13 @@ func checkKeyEmails(ctx context.Context, email string, keys ...*asymkey_model.GP
214218
}
215219
if key.Verified && key.OwnerID != 0 {
216220
if uid != key.OwnerID {
217-
userEmails, _ = user_model.GetEmailAddresses(ctx, key.OwnerID)
221+
userEmails, _ = cache.GetWithContextCache(ctx, cacheUserEmailAddressKey, key.OwnerID, func() ([]*user_model.EmailAddress, error) {
222+
return user_model.GetEmailAddresses(ctx, key.OwnerID)
223+
})
218224
uid = key.OwnerID
219-
user = &user_model.User{ID: uid}
220-
_, _ = user_model.GetUser(ctx, user)
225+
user, _ = cache.GetWithContextCache(ctx, cacheUserKey, uid, func() (*user_model.User, error) {
226+
return user_model.GetUserByID(ctx, uid)
227+
})
221228
}
222229
for _, e := range userEmails {
223230
if e.IsActivated && (email == "" || strings.EqualFold(e.Email, email)) {
@@ -232,13 +239,11 @@ func checkKeyEmails(ctx context.Context, email string, keys ...*asymkey_model.GP
232239
return false, email
233240
}
234241

235-
const cacheGroupKey = "gpg_key_id"
236-
237242
func HashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload string, committer *user_model.User, keyID, name, email string) *asymkey_model.CommitVerification {
238243
if keyID == "" {
239244
return nil
240245
}
241-
keys, err := cache.GetWithContextCache(ctx, cacheGroupKey, keyID, func() ([]*asymkey_model.GPGKey, error) {
246+
keys, err := cache.GetWithContextCache(ctx, cacheGPGListKey, keyID, func() ([]*asymkey_model.GPGKey, error) {
242247
return db.Find[asymkey_model.GPGKey](ctx, asymkey_model.FindGPGKeyOptions{
243248
KeyID: keyID,
244249
IncludeSubKeys: true,
@@ -258,7 +263,7 @@ func HashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload s
258263
for _, key := range keys {
259264
var primaryKeys []*asymkey_model.GPGKey
260265
if key.PrimaryKeyID != "" {
261-
primaryKeys, err = cache.GetWithContextCache(ctx, cacheGroupKey, key.PrimaryKeyID, func() ([]*asymkey_model.GPGKey, error) {
266+
primaryKeys, err = cache.GetWithContextCache(ctx, cacheGPGListKey, key.PrimaryKeyID, func() ([]*asymkey_model.GPGKey, error) {
262267
return db.Find[asymkey_model.GPGKey](ctx, asymkey_model.FindGPGKeyOptions{
263268
KeyID: key.PrimaryKeyID,
264269
IncludeSubKeys: true,
@@ -283,8 +288,10 @@ func HashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload s
283288
Name: name,
284289
Email: email,
285290
}
286-
if key.OwnerID != 0 {
287-
owner, err := user_model.GetUserByID(ctx, key.OwnerID)
291+
if key.OwnerID > 0 {
292+
owner, err := cache.GetWithContextCache(ctx, cacheUserKey, committer.ID, func() (*user_model.User, error) {
293+
return user_model.GetUserByID(ctx, key.OwnerID)
294+
})
288295
if err == nil {
289296
signer = owner
290297
} else if !user_model.IsErrUserNotExist(err) {
@@ -392,7 +399,9 @@ func ParseCommitWithSSHSignature(ctx context.Context, c *git.Commit, committer *
392399
}
393400
}
394401

395-
committerEmailAddresses, err := user_model.GetEmailAddresses(ctx, committer.ID)
402+
committerEmailAddresses, err := cache.GetWithContextCache(ctx, cacheUserEmailAddressKey, committer.ID, func() ([]*user_model.EmailAddress, error) {
403+
return user_model.GetEmailAddresses(ctx, committer.ID)
404+
})
396405
if err != nil {
397406
log.Error("GetEmailAddresses: %v", err)
398407
}

services/git/commit.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
// ParseCommitsWithSignature checks if signaute of commits are corresponding to users gpg keys.
21-
func ParseCommitsWithSignature(ctx context.Context, oldCommits []*user_model.UserCommit, repoTrustModel repo_model.TrustModelType, isOwnerMemberCollaborator func(*user_model.User) (bool, error)) ([]*asymkey_model.SignCommit, error) {
21+
func ParseCommitsWithSignature(ctx context.Context, repo *repo_model.Repository, oldCommits []*user_model.UserCommit, repoTrustModel repo_model.TrustModelType) ([]*asymkey_model.SignCommit, error) {
2222
newCommits := make([]*asymkey_model.SignCommit, 0, len(oldCommits))
2323
keyMap := map[string]bool{}
2424

@@ -50,6 +50,10 @@ func ParseCommitsWithSignature(ctx context.Context, oldCommits []*user_model.Use
5050
Verification: asymkey_service.ParseCommitWithSignatureCommitter(ctx, c.Commit, committer),
5151
}
5252

53+
isOwnerMemberCollaborator := func(user *user_model.User) (bool, error) {
54+
return repo_model.IsOwnerMemberCollaborator(ctx, repo, user.ID)
55+
}
56+
5357
_ = asymkey_model.CalculateTrustStatus(signCommit.Verification, repoTrustModel, isOwnerMemberCollaborator, &keyMap)
5458

5559
newCommits = append(newCommits, signCommit)
@@ -65,11 +69,9 @@ func ConvertFromGitCommit(ctx context.Context, commits []*git.Commit, repo *repo
6569
}
6670
signedCommits, err := ParseCommitsWithSignature(
6771
ctx,
72+
repo,
6873
validatedCommits,
6974
repo.GetTrustModel(),
70-
func(user *user_model.User) (bool, error) {
71-
return repo_model.IsOwnerMemberCollaborator(ctx, repo, user.ID)
72-
},
7375
)
7476
if err != nil {
7577
return nil, err

0 commit comments

Comments
 (0)