@@ -1129,28 +1129,85 @@ func ValidateCommitWithEmail(ctx context.Context, c *git.Commit) *User {
11291129}
11301130
11311131// ValidateCommitsWithEmails checks if authors' e-mails of commits are corresponding to users.
1132- func ValidateCommitsWithEmails (ctx context.Context , oldCommits []* git.Commit ) []* UserCommit {
1132+ func ValidateCommitsWithEmails (ctx context.Context , oldCommits []* git.Commit ) ( []* UserCommit , error ) {
11331133 var (
1134- emails = make (map [string ]* User )
11351134 newCommits = make ([]* UserCommit , 0 , len (oldCommits ))
1135+ emailSet = make (container.Set [string ])
11361136 )
11371137 for _ , c := range oldCommits {
1138- var u * User
11391138 if c .Author != nil {
1140- if v , ok := emails [c .Author .Email ]; ! ok {
1141- u , _ = GetUserByEmail (ctx , c .Author .Email )
1142- emails [c .Author .Email ] = u
1143- } else {
1144- u = v
1145- }
1139+ emailSet .Add (c .Author .Email )
11461140 }
1141+ }
1142+
1143+ emailUserMap , err := GetUsersByEmails (ctx , emailSet .Values ())
1144+ if err != nil {
1145+ return nil , err
1146+ }
11471147
1148+ for _ , c := range oldCommits {
1149+ user , ok := emailUserMap [c .Author .Email ]
1150+ if ! ok {
1151+ user = & User {
1152+ Name : c .Author .Name ,
1153+ Email : c .Author .Email ,
1154+ }
1155+ }
11481156 newCommits = append (newCommits , & UserCommit {
1149- User : u ,
1157+ User : user ,
11501158 Commit : c ,
11511159 })
11521160 }
1153- return newCommits
1161+ return newCommits , nil
1162+ }
1163+
1164+ func GetUsersByEmails (ctx context.Context , emails []string ) (map [string ]* User , error ) {
1165+ if len (emails ) == 0 {
1166+ return nil , nil
1167+ }
1168+
1169+ needCheckEmails := make (container.Set [string ])
1170+ needCheckUserNames := make (container.Set [string ])
1171+ for _ , email := range emails {
1172+ if strings .HasSuffix (email , fmt .Sprintf ("@%s" , setting .Service .NoReplyAddress )) {
1173+ username := strings .TrimSuffix (email , fmt .Sprintf ("@%s" , setting .Service .NoReplyAddress ))
1174+ needCheckUserNames .Add (username )
1175+ } else {
1176+ needCheckEmails .Add (strings .ToLower (email ))
1177+ }
1178+ }
1179+
1180+ emailAddresses := make ([]* EmailAddress , 0 , len (needCheckEmails ))
1181+ if err := db .GetEngine (ctx ).In ("lower_email" , needCheckEmails .Values ()).
1182+ And ("is_activated=?" , true ).
1183+ Find (& emailAddresses ); err != nil {
1184+ return nil , err
1185+ }
1186+ userIDs := make (container.Set [int64 ])
1187+ for _ , email := range emailAddresses {
1188+ userIDs .Add (email .UID )
1189+ }
1190+ users , err := GetUsersByIDs (ctx , userIDs .Values ())
1191+ if err != nil {
1192+ return nil , err
1193+ }
1194+
1195+ results := make (map [string ]* User , len (emails ))
1196+ for _ , user := range users {
1197+ if user .KeepEmailPrivate {
1198+ results [user .LowerName + "@" + setting .Service .NoReplyAddress ] = user
1199+ } else {
1200+ results [user .Email ] = user
1201+ }
1202+ }
1203+ users = make ([]* User , 0 , len (needCheckUserNames ))
1204+ if err := db .GetEngine (ctx ).In ("lower_name" , needCheckUserNames .Values ()).Find (& users ); err != nil {
1205+ return nil , err
1206+ }
1207+ for _ , user := range users {
1208+ results [user .LowerName + "@" + setting .Service .NoReplyAddress ] = user
1209+ }
1210+ return results , nil
11541211}
11551212
11561213// GetUserByEmail returns the user object by given e-mail if exists.
0 commit comments