@@ -11,6 +11,7 @@ import (
11
11
12
12
"code.gitea.io/gitea/models/avatars"
13
13
user_model "code.gitea.io/gitea/models/user"
14
+ "code.gitea.io/gitea/modules/cache"
14
15
"code.gitea.io/gitea/modules/git"
15
16
"code.gitea.io/gitea/modules/log"
16
17
"code.gitea.io/gitea/modules/setting"
@@ -34,42 +35,36 @@ type PushCommits struct {
34
35
HeadCommit * PushCommit
35
36
CompareURL string
36
37
Len int
37
-
38
- avatars map [string ]string
39
- emailUsers map [string ]* user_model.User
40
38
}
41
39
42
40
// NewPushCommits creates a new PushCommits object.
43
41
func NewPushCommits () * PushCommits {
44
- return & PushCommits {
45
- avatars : make (map [string ]string ),
46
- emailUsers : make (map [string ]* user_model.User ),
47
- }
42
+ return & PushCommits {}
48
43
}
49
44
50
45
// toAPIPayloadCommit converts a single PushCommit to an api.PayloadCommit object.
51
- func (pc * PushCommits ) toAPIPayloadCommit (ctx context.Context , repoPath , repoLink string , commit * PushCommit ) (* api.PayloadCommit , error ) {
46
+ func (pc * PushCommits ) toAPIPayloadCommit (ctx context.Context , emailUsers map [ string ] * user_model. User , repoPath , repoLink string , commit * PushCommit ) (* api.PayloadCommit , error ) {
52
47
var err error
53
48
authorUsername := ""
54
- author , ok := pc . emailUsers [commit .AuthorEmail ]
49
+ author , ok := emailUsers [commit .AuthorEmail ]
55
50
if ! ok {
56
51
author , err = user_model .GetUserByEmail (ctx , commit .AuthorEmail )
57
52
if err == nil {
58
53
authorUsername = author .Name
59
- pc . emailUsers [commit .AuthorEmail ] = author
54
+ emailUsers [commit .AuthorEmail ] = author
60
55
}
61
56
} else {
62
57
authorUsername = author .Name
63
58
}
64
59
65
60
committerUsername := ""
66
- committer , ok := pc . emailUsers [commit .CommitterEmail ]
61
+ committer , ok := emailUsers [commit .CommitterEmail ]
67
62
if ! ok {
68
63
committer , err = user_model .GetUserByEmail (ctx , commit .CommitterEmail )
69
64
if err == nil {
70
65
// TODO: check errors other than email not found.
71
66
committerUsername = committer .Name
72
- pc . emailUsers [commit .CommitterEmail ] = committer
67
+ emailUsers [commit .CommitterEmail ] = committer
73
68
}
74
69
} else {
75
70
committerUsername = committer .Name
@@ -107,11 +102,10 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
107
102
commits := make ([]* api.PayloadCommit , len (pc .Commits ))
108
103
var headCommit * api.PayloadCommit
109
104
110
- if pc .emailUsers == nil {
111
- pc .emailUsers = make (map [string ]* user_model.User )
112
- }
105
+ emailUsers := make (map [string ]* user_model.User )
106
+
113
107
for i , commit := range pc .Commits {
114
- apiCommit , err := pc .toAPIPayloadCommit (ctx , repoPath , repoLink , commit )
108
+ apiCommit , err := pc .toAPIPayloadCommit (ctx , emailUsers , repoPath , repoLink , commit )
115
109
if err != nil {
116
110
return nil , nil , err
117
111
}
@@ -123,7 +117,7 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
123
117
}
124
118
if pc .HeadCommit != nil && headCommit == nil {
125
119
var err error
126
- headCommit , err = pc .toAPIPayloadCommit (ctx , repoPath , repoLink , pc .HeadCommit )
120
+ headCommit , err = pc .toAPIPayloadCommit (ctx , emailUsers , repoPath , repoLink , pc .HeadCommit )
127
121
if err != nil {
128
122
return nil , nil , err
129
123
}
@@ -134,35 +128,21 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
134
128
// AvatarLink tries to match user in database with e-mail
135
129
// in order to show custom avatar, and falls back to general avatar link.
136
130
func (pc * PushCommits ) AvatarLink (ctx context.Context , email string ) string {
137
- if pc .avatars == nil {
138
- pc .avatars = make (map [string ]string )
139
- }
140
- avatar , ok := pc .avatars [email ]
141
- if ok {
142
- return avatar
143
- }
144
-
145
131
size := avatars .DefaultAvatarPixelSize * setting .Avatar .RenderedSizeFactor
146
132
147
- u , ok := pc .emailUsers [email ]
148
- if ! ok {
149
- var err error
150
- u , err = user_model .GetUserByEmail (ctx , email )
133
+ v , _ := cache .GetWithContextCache (ctx , "push_commits" , email , func () (string , error ) {
134
+ u , err := user_model .GetUserByEmail (ctx , email )
151
135
if err != nil {
152
- pc .avatars [email ] = avatars .GenerateEmailAvatarFastLink (ctx , email , size )
153
136
if ! user_model .IsErrUserNotExist (err ) {
154
137
log .Error ("GetUserByEmail: %v" , err )
155
- return ""
138
+ return "" , err
156
139
}
157
- } else {
158
- pc .emailUsers [email ] = u
140
+ return avatars .GenerateEmailAvatarFastLink (ctx , email , size ), nil
159
141
}
160
- }
161
- if u != nil {
162
- pc .avatars [email ] = u .AvatarLinkWithSize (ctx , size )
163
- }
142
+ return u .AvatarLinkWithSize (ctx , size ), nil
143
+ })
164
144
165
- return pc . avatars [ email ]
145
+ return v
166
146
}
167
147
168
148
// CommitToPushCommit transforms a git.Commit to PushCommit type.
@@ -189,7 +169,5 @@ func GitToPushCommits(gitCommits []*git.Commit) *PushCommits {
189
169
HeadCommit : nil ,
190
170
CompareURL : "" ,
191
171
Len : len (commits ),
192
- avatars : make (map [string ]string ),
193
- emailUsers : make (map [string ]* user_model.User ),
194
172
}
195
173
}
0 commit comments