Skip to content

Commit 3be4456

Browse files
committed
fix
1 parent 328477e commit 3be4456

File tree

8 files changed

+44
-48
lines changed

8 files changed

+44
-48
lines changed

models/asymkey/gpg_key.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,10 @@ func DeleteGPGKey(ctx context.Context, doer *user_model.User, id int64) (err err
240240

241241
return committer.Commit()
242242
}
243+
244+
func FindGPGKeyWithSubKeys(ctx context.Context, keyID string) ([]*GPGKey, error) {
245+
return db.Find[GPGKey](ctx, FindGPGKeyOptions{
246+
KeyID: keyID,
247+
IncludeSubKeys: true,
248+
})
249+
}

modules/cache/context.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ func RemoveContextData(ctx context.Context, tp, key any) {
166166
}
167167

168168
// GetWithContextCache returns the cache value of the given key in the given context.
169-
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error) {
170-
v := GetContextData(ctx, cacheGroupKey, cacheTargetID)
169+
func GetWithContextCache[T, K any](ctx context.Context, groupKey string, targetKey K, f func(context.Context, K) (T, error)) (T, error) {
170+
v := GetContextData(ctx, groupKey, targetKey)
171171
if vv, ok := v.(T); ok {
172172
return vv, nil
173173
}
174-
t, err := f()
174+
t, err := f(ctx, targetKey)
175175
if err != nil {
176176
return t, err
177177
}
178-
SetContextData(ctx, cacheGroupKey, cacheTargetID, t)
178+
SetContextData(ctx, groupKey, targetKey, t)
179179
return t, nil
180180
}

modules/cache/context_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package cache
55

66
import (
7+
"context"
78
"testing"
89
"time"
910

@@ -30,7 +31,7 @@ func TestWithCacheContext(t *testing.T) {
3031
v = GetContextData(ctx, field, "my_config1")
3132
assert.Nil(t, v)
3233

33-
vInt, err := GetWithContextCache(ctx, field, "my_config1", func() (int, error) {
34+
vInt, err := GetWithContextCache(ctx, field, "my_config1", func(context.Context, string) (int, error) {
3435
return 1, nil
3536
})
3637
assert.NoError(t, err)

modules/cachegroup/cachegroup.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package cachegroup
5+
6+
const (
7+
User = "user"
8+
EmailAvatarLink = "email_avatar_link"
9+
UserEmailAddresses = "user_email_addresses"
10+
GPGKeyWithSubKeys = "gpg_key_with_subkeys"
11+
RepoUserPermission = "repo_user_permission"
12+
)

modules/repository/commits.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
repo_model "code.gitea.io/gitea/models/repo"
1414
user_model "code.gitea.io/gitea/models/user"
1515
"code.gitea.io/gitea/modules/cache"
16+
"code.gitea.io/gitea/modules/cachegroup"
1617
"code.gitea.io/gitea/modules/git"
1718
"code.gitea.io/gitea/modules/log"
1819
"code.gitea.io/gitea/modules/setting"
@@ -131,7 +132,7 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repo *repo_model
131132
func (pc *PushCommits) AvatarLink(ctx context.Context, email string) string {
132133
size := avatars.DefaultAvatarPixelSize * setting.Avatar.RenderedSizeFactor
133134

134-
v, _ := cache.GetWithContextCache(ctx, "push_commits", email, func() (string, error) {
135+
v, _ := cache.GetWithContextCache(ctx, cachegroup.EmailAvatarLink, email, func(ctx context.Context, email string) (string, error) {
135136
u, err := user_model.GetUserByEmail(ctx, email)
136137
if err != nil {
137138
if !user_model.IsErrUserNotExist(err) {

routers/private/hook_post_receive.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
repo_model "code.gitea.io/gitea/models/repo"
1515
user_model "code.gitea.io/gitea/models/user"
1616
"code.gitea.io/gitea/modules/cache"
17+
"code.gitea.io/gitea/modules/cachegroup"
1718
"code.gitea.io/gitea/modules/git"
1819
"code.gitea.io/gitea/modules/gitrepo"
1920
"code.gitea.io/gitea/modules/log"
@@ -326,9 +327,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
326327
}
327328

328329
func loadContextCacheUser(ctx context.Context, id int64) (*user_model.User, error) {
329-
return cache.GetWithContextCache(ctx, "hook_post_receive_user", id, func() (*user_model.User, error) {
330-
return user_model.GetUserByID(ctx, id)
331-
})
330+
return cache.GetWithContextCache(ctx, cachegroup.User, id, user_model.GetUserByID)
332331
}
333332

334333
// handlePullRequestMerging handle pull request merging, a pull request action should push at least 1 commit

services/asymkey/commit.go

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/models/db"
1313
user_model "code.gitea.io/gitea/models/user"
1414
"code.gitea.io/gitea/modules/cache"
15+
"code.gitea.io/gitea/modules/cachegroup"
1516
"code.gitea.io/gitea/modules/git"
1617
"code.gitea.io/gitea/modules/log"
1718
"code.gitea.io/gitea/modules/setting"
@@ -48,12 +49,6 @@ func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *asymkey_model
4849
return ParseCommitWithSignatureCommitter(ctx, c, committer)
4950
}
5051

51-
const (
52-
cacheUserEmailAddressKey = "gpg_user_email_address"
53-
cacheUserKey = "gpg_user"
54-
cacheGPGListKey = "gpg_key_list"
55-
)
56-
5752
func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, committer *user_model.User) *asymkey_model.CommitVerification {
5853
// If no signature just report the committer
5954
if c.Signature == nil {
@@ -122,9 +117,7 @@ func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, commi
122117
}
123118
}
124119

125-
committerEmailAddresses, _ := cache.GetWithContextCache(ctx, cacheUserEmailAddressKey, committer.ID, func() ([]*user_model.EmailAddress, error) {
126-
return user_model.GetEmailAddresses(ctx, committer.ID)
127-
})
120+
committerEmailAddresses, _ := cache.GetWithContextCache(ctx, cachegroup.UserEmailAddresses, committer.ID, user_model.GetEmailAddresses)
128121
activated := false
129122
for _, e := range committerEmailAddresses {
130123
if e.IsActivated && strings.EqualFold(e.Email, c.Committer.Email) {
@@ -218,13 +211,9 @@ func checkKeyEmails(ctx context.Context, email string, keys ...*asymkey_model.GP
218211
}
219212
if key.Verified && key.OwnerID != 0 {
220213
if uid != key.OwnerID {
221-
userEmails, _ = cache.GetWithContextCache(ctx, cacheUserEmailAddressKey, key.OwnerID, func() ([]*user_model.EmailAddress, error) {
222-
return user_model.GetEmailAddresses(ctx, key.OwnerID)
223-
})
214+
userEmails, _ = cache.GetWithContextCache(ctx, cachegroup.UserEmailAddresses, key.OwnerID, user_model.GetEmailAddresses)
224215
uid = key.OwnerID
225-
user, _ = cache.GetWithContextCache(ctx, cacheUserKey, uid, func() (*user_model.User, error) {
226-
return user_model.GetUserByID(ctx, uid)
227-
})
216+
user, _ = cache.GetWithContextCache(ctx, cachegroup.User, uid, user_model.GetUserByID)
228217
}
229218
for _, e := range userEmails {
230219
if e.IsActivated && (email == "" || strings.EqualFold(e.Email, email)) {
@@ -243,12 +232,7 @@ func HashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload s
243232
if keyID == "" {
244233
return nil
245234
}
246-
keys, err := cache.GetWithContextCache(ctx, cacheGPGListKey, keyID, func() ([]*asymkey_model.GPGKey, error) {
247-
return db.Find[asymkey_model.GPGKey](ctx, asymkey_model.FindGPGKeyOptions{
248-
KeyID: keyID,
249-
IncludeSubKeys: true,
250-
})
251-
})
235+
keys, err := cache.GetWithContextCache(ctx, cachegroup.GPGKeyWithSubKeys, keyID, asymkey_model.FindGPGKeyWithSubKeys)
252236
if err != nil {
253237
log.Error("GetGPGKeysByKeyID: %v", err)
254238
return &asymkey_model.CommitVerification{
@@ -263,12 +247,7 @@ func HashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload s
263247
for _, key := range keys {
264248
var primaryKeys []*asymkey_model.GPGKey
265249
if key.PrimaryKeyID != "" {
266-
primaryKeys, err = cache.GetWithContextCache(ctx, cacheGPGListKey, key.PrimaryKeyID, func() ([]*asymkey_model.GPGKey, error) {
267-
return db.Find[asymkey_model.GPGKey](ctx, asymkey_model.FindGPGKeyOptions{
268-
KeyID: key.PrimaryKeyID,
269-
IncludeSubKeys: true,
270-
})
271-
})
250+
primaryKeys, err = cache.GetWithContextCache(ctx, cachegroup.GPGKeyWithSubKeys, key.PrimaryKeyID, asymkey_model.FindGPGKeyWithSubKeys)
272251
if err != nil {
273252
log.Error("GetGPGKeysByKeyID: %v", err)
274253
return &asymkey_model.CommitVerification{
@@ -289,9 +268,7 @@ func HashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload s
289268
Email: email,
290269
}
291270
if key.OwnerID > 0 {
292-
owner, err := cache.GetWithContextCache(ctx, cacheUserKey, key.OwnerID, func() (*user_model.User, error) {
293-
return user_model.GetUserByID(ctx, key.OwnerID)
294-
})
271+
owner, err := cache.GetWithContextCache(ctx, cachegroup.User, key.OwnerID, user_model.GetUserByID)
295272
if err == nil {
296273
signer = owner
297274
} else if !user_model.IsErrUserNotExist(err) {
@@ -399,9 +376,7 @@ func ParseCommitWithSSHSignature(ctx context.Context, c *git.Commit, committer *
399376
}
400377
}
401378

402-
committerEmailAddresses, err := cache.GetWithContextCache(ctx, cacheUserEmailAddressKey, committer.ID, func() ([]*user_model.EmailAddress, error) {
403-
return user_model.GetEmailAddresses(ctx, committer.ID)
404-
})
379+
committerEmailAddresses, err := cache.GetWithContextCache(ctx, cachegroup.UserEmailAddresses, committer.ID, user_model.GetEmailAddresses)
405380
if err != nil {
406381
log.Error("GetEmailAddresses: %v", err)
407382
}

services/convert/pull.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
repo_model "code.gitea.io/gitea/models/repo"
1515
user_model "code.gitea.io/gitea/models/user"
1616
"code.gitea.io/gitea/modules/cache"
17+
"code.gitea.io/gitea/modules/cachegroup"
1718
"code.gitea.io/gitea/modules/git"
1819
"code.gitea.io/gitea/modules/gitrepo"
1920
"code.gitea.io/gitea/modules/log"
@@ -60,14 +61,14 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
6061
doerID = doer.ID
6162
}
6263

63-
const repoDoerPermCacheKey = "repo_doer_perm_cache"
64-
p, err := cache.GetWithContextCache(ctx, repoDoerPermCacheKey, fmt.Sprintf("%d_%d", pr.BaseRepoID, doerID),
65-
func() (access_model.Permission, error) {
64+
repoUserPerm, err := cache.GetWithContextCache(ctx, cachegroup.RepoUserPermission, fmt.Sprintf("%d-%d", pr.BaseRepoID, doerID),
65+
func(ctx context.Context, _ string) (access_model.Permission, error) {
6666
return access_model.GetUserRepoPermission(ctx, pr.BaseRepo, doer)
67-
})
67+
},
68+
)
6869
if err != nil {
6970
log.Error("GetUserRepoPermission[%d]: %v", pr.BaseRepoID, err)
70-
p.AccessMode = perm.AccessModeNone
71+
repoUserPerm.AccessMode = perm.AccessModeNone
7172
}
7273

7374
apiPullRequest := &api.PullRequest{
@@ -107,7 +108,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
107108
Name: pr.BaseBranch,
108109
Ref: pr.BaseBranch,
109110
RepoID: pr.BaseRepoID,
110-
Repository: ToRepo(ctx, pr.BaseRepo, p),
111+
Repository: ToRepo(ctx, pr.BaseRepo, repoUserPerm),
111112
},
112113
Head: &api.PRBranchInfo{
113114
Name: pr.HeadBranch,

0 commit comments

Comments
 (0)