@@ -11,6 +11,7 @@ import (
1111
1212 "code.gitea.io/gitea/models/avatars"
1313 user_model "code.gitea.io/gitea/models/user"
14+ "code.gitea.io/gitea/modules/cache"
1415 "code.gitea.io/gitea/modules/git"
1516 "code.gitea.io/gitea/modules/log"
1617 "code.gitea.io/gitea/modules/setting"
@@ -34,42 +35,36 @@ type PushCommits struct {
3435 HeadCommit * PushCommit
3536 CompareURL string
3637 Len int
37-
38- avatars map [string ]string
39- emailUsers map [string ]* user_model.User
4038}
4139
4240// NewPushCommits creates a new PushCommits object.
4341func NewPushCommits () * PushCommits {
44- return & PushCommits {
45- avatars : make (map [string ]string ),
46- emailUsers : make (map [string ]* user_model.User ),
47- }
42+ return & PushCommits {}
4843}
4944
5045// 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 ) {
5247 var err error
5348 authorUsername := ""
54- author , ok := pc . emailUsers [commit .AuthorEmail ]
49+ author , ok := emailUsers [commit .AuthorEmail ]
5550 if ! ok {
5651 author , err = user_model .GetUserByEmail (ctx , commit .AuthorEmail )
5752 if err == nil {
5853 authorUsername = author .Name
59- pc . emailUsers [commit .AuthorEmail ] = author
54+ emailUsers [commit .AuthorEmail ] = author
6055 }
6156 } else {
6257 authorUsername = author .Name
6358 }
6459
6560 committerUsername := ""
66- committer , ok := pc . emailUsers [commit .CommitterEmail ]
61+ committer , ok := emailUsers [commit .CommitterEmail ]
6762 if ! ok {
6863 committer , err = user_model .GetUserByEmail (ctx , commit .CommitterEmail )
6964 if err == nil {
7065 // TODO: check errors other than email not found.
7166 committerUsername = committer .Name
72- pc . emailUsers [commit .CommitterEmail ] = committer
67+ emailUsers [commit .CommitterEmail ] = committer
7368 }
7469 } else {
7570 committerUsername = committer .Name
@@ -107,11 +102,10 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
107102 commits := make ([]* api.PayloadCommit , len (pc .Commits ))
108103 var headCommit * api.PayloadCommit
109104
110- if pc .emailUsers == nil {
111- pc .emailUsers = make (map [string ]* user_model.User )
112- }
105+ emailUsers := make (map [string ]* user_model.User )
106+
113107 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 )
115109 if err != nil {
116110 return nil , nil , err
117111 }
@@ -123,7 +117,7 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
123117 }
124118 if pc .HeadCommit != nil && headCommit == nil {
125119 var err error
126- headCommit , err = pc .toAPIPayloadCommit (ctx , repoPath , repoLink , pc .HeadCommit )
120+ headCommit , err = pc .toAPIPayloadCommit (ctx , emailUsers , repoPath , repoLink , pc .HeadCommit )
127121 if err != nil {
128122 return nil , nil , err
129123 }
@@ -134,35 +128,21 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
134128// AvatarLink tries to match user in database with e-mail
135129// in order to show custom avatar, and falls back to general avatar link.
136130func (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-
145131 size := avatars .DefaultAvatarPixelSize * setting .Avatar .RenderedSizeFactor
146132
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 )
151135 if err != nil {
152- pc .avatars [email ] = avatars .GenerateEmailAvatarFastLink (ctx , email , size )
153136 if ! user_model .IsErrUserNotExist (err ) {
154137 log .Error ("GetUserByEmail: %v" , err )
155- return ""
138+ return "" , err
156139 }
157- } else {
158- pc .emailUsers [email ] = u
140+ return avatars .GenerateEmailAvatarFastLink (ctx , email , size ), nil
159141 }
160- }
161- if u != nil {
162- pc .avatars [email ] = u .AvatarLinkWithSize (ctx , size )
163- }
142+ return u .AvatarLinkWithSize (ctx , size ), nil
143+ })
164144
165- return pc . avatars [ email ]
145+ return v
166146}
167147
168148// CommitToPushCommit transforms a git.Commit to PushCommit type.
@@ -189,7 +169,5 @@ func GitToPushCommits(gitCommits []*git.Commit) *PushCommits {
189169 HeadCommit : nil ,
190170 CompareURL : "" ,
191171 Len : len (commits ),
192- avatars : make (map [string ]string ),
193- emailUsers : make (map [string ]* user_model.User ),
194172 }
195173}
0 commit comments