@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models/avatars"
"code.gitea.io/gitea/models/avatars"
user_model "code.gitea.io/gitea/models/user"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/setting"
@ -34,42 +35,36 @@ type PushCommits struct {
HeadCommit * PushCommit
HeadCommit * PushCommit
CompareURL string
CompareURL string
Len int
Len int
avatars map [ string ] string
emailUsers map [ string ] * user_model . User
}
}
// NewPushCommits creates a new PushCommits object.
// NewPushCommits creates a new PushCommits object.
func NewPushCommits ( ) * PushCommits {
func NewPushCommits ( ) * PushCommits {
return & PushCommits {
return & PushCommits { }
avatars : make ( map [ string ] string ) ,
emailUsers : make ( map [ string ] * user_model . User ) ,
}
}
}
// toAPIPayloadCommit converts a single PushCommit to an api.PayloadCommit object.
// toAPIPayloadCommit converts a single PushCommit to an api.PayloadCommit object.
func ( pc * PushCommits ) toAPIPayloadCommit ( ctx context . Context , repoPath , repoLink string , commit * PushCommit ) ( * api . PayloadCommit , error ) {
func ( pc * PushCommits ) toAPIPayloadCommit ( ctx context . Context , emailUsers map [ string ] * user_model . User , repoPath , repoLink string , commit * PushCommit ) ( * api . PayloadCommit , error ) {
var err error
var err error
authorUsername := ""
authorUsername := ""
author , ok := pc . emailUsers [ commit . AuthorEmail ]
author , ok := emailUsers [ commit . AuthorEmail ]
if ! ok {
if ! ok {
author , err = user_model . GetUserByEmail ( ctx , commit . AuthorEmail )
author , err = user_model . GetUserByEmail ( ctx , commit . AuthorEmail )
if err == nil {
if err == nil {
authorUsername = author . Name
authorUsername = author . Name
pc . emailUsers [ commit . AuthorEmail ] = author
emailUsers [ commit . AuthorEmail ] = author
}
}
} else {
} else {
authorUsername = author . Name
authorUsername = author . Name
}
}
committerUsername := ""
committerUsername := ""
committer , ok := pc . emailUsers [ commit . CommitterEmail ]
committer , ok := emailUsers [ commit . CommitterEmail ]
if ! ok {
if ! ok {
committer , err = user_model . GetUserByEmail ( ctx , commit . CommitterEmail )
committer , err = user_model . GetUserByEmail ( ctx , commit . CommitterEmail )
if err == nil {
if err == nil {
// TODO: check errors other than email not found.
// TODO: check errors other than email not found.
committerUsername = committer . Name
committerUsername = committer . Name
pc . emailUsers [ commit . CommitterEmail ] = committer
emailUsers [ commit . CommitterEmail ] = committer
}
}
} else {
} else {
committerUsername = committer . Name
committerUsername = committer . Name
@ -107,11 +102,10 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
commits := make ( [ ] * api . PayloadCommit , len ( pc . Commits ) )
commits := make ( [ ] * api . PayloadCommit , len ( pc . Commits ) )
var headCommit * api . PayloadCommit
var headCommit * api . PayloadCommit
if pc . emailUsers == nil {
emailUsers := make ( map [ string ] * user_model . User )
pc . emailUsers = make ( map [ string ] * user_model . User )
}
for i , commit := range pc . Commits {
for i , commit := range pc . Commits {
apiCommit , err := pc . toAPIPayloadCommit ( ctx , repoPath , repoLink , commit )
apiCommit , err := pc . toAPIPayloadCommit ( ctx , emailUsers , repoPath , repoLink , commit )
if err != nil {
if err != nil {
return nil , nil , err
return nil , nil , err
}
}
@ -123,7 +117,7 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
}
}
if pc . HeadCommit != nil && headCommit == nil {
if pc . HeadCommit != nil && headCommit == nil {
var err error
var err error
headCommit , err = pc . toAPIPayloadCommit ( ctx , repoPath , repoLink , pc . HeadCommit )
headCommit , err = pc . toAPIPayloadCommit ( ctx , emailUsers , repoPath , repoLink , pc . HeadCommit )
if err != nil {
if err != nil {
return nil , nil , err
return nil , nil , err
}
}
@ -134,35 +128,21 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
// AvatarLink tries to match user in database with e-mail
// AvatarLink tries to match user in database with e-mail
// in order to show custom avatar, and falls back to general avatar link.
// in order to show custom avatar, and falls back to general avatar link.
func ( pc * PushCommits ) AvatarLink ( ctx context . Context , email string ) string {
func ( pc * PushCommits ) AvatarLink ( ctx context . Context , email string ) string {
if pc . avatars == nil {
pc . avatars = make ( map [ string ] string )
}
avatar , ok := pc . avatars [ email ]
if ok {
return avatar
}
size := avatars . DefaultAvatarPixelSize * setting . Avatar . RenderedSizeFactor
size := avatars . DefaultAvatarPixelSize * setting . Avatar . RenderedSizeFactor
u , ok := pc . emailUsers [ email ]
v , _ := cache . GetWithContextCache ( ctx , "push_commits" , email , func ( ) ( string , error ) {
if ! ok {
u , err := user_model . GetUserByEmail ( ctx , email )
var err error
u , err = user_model . GetUserByEmail ( ctx , email )
if err != nil {
if err != nil {
pc . avatars [ email ] = avatars . GenerateEmailAvatarFastLink ( ctx , email , size )
if ! user_model . IsErrUserNotExist ( err ) {
if ! user_model . IsErrUserNotExist ( err ) {
log . Error ( "GetUserByEmail: %v" , err )
log . Error ( "GetUserByEmail: %v" , err )
return ""
return "" , err
}
} else {
pc . emailUsers [ email ] = u
}
}
}
if u != nil {
return avatars . GenerateEmailAvatarFastLink ( ctx , email , size ) , nil
pc . avatars [ email ] = u . AvatarLinkWithSize ( ctx , size )
}
}
return u . AvatarLinkWithSize ( ctx , size ) , nil
} )
return pc . avatars [ email ]
return v
}
}
// CommitToPushCommit transforms a git.Commit to PushCommit type.
// CommitToPushCommit transforms a git.Commit to PushCommit type.
@ -189,7 +169,5 @@ func GitToPushCommits(gitCommits []*git.Commit) *PushCommits {
HeadCommit : nil ,
HeadCommit : nil ,
CompareURL : "" ,
CompareURL : "" ,
Len : len ( commits ) ,
Len : len ( commits ) ,
avatars : make ( map [ string ] string ) ,
emailUsers : make ( map [ string ] * user_model . User ) ,
}
}
}
}