@@ -388,22 +388,33 @@ func GetLatestCommitStatusForRepoCommitIDs(ctx context.Context, repoID int64, co
388
388
389
389
// FindRepoRecentCommitStatusContexts returns repository's recent commit status contexts
390
390
func FindRepoRecentCommitStatusContexts (ctx context.Context , repoID int64 , before time.Duration ) ([]string , error ) {
391
+ type result struct {
392
+ Index int64
393
+ SHA string
394
+ }
391
395
start := timeutil .TimeStampNow ().AddDuration (- before )
392
- ids := make ([]int64 , 0 , 10 )
393
- if err := db .GetEngine (ctx ).Table ("commit_status" ).
394
- Where ("repo_id = ?" , repoID ).
395
- And ("updated_unix >= ?" , start ).
396
- Select ("max( id ) as id" ).
397
- GroupBy ("context_hash" ).OrderBy ("max( id ) desc" ).
398
- Find (& ids ); err != nil {
396
+ base := db .GetEngine (ctx ).Table (& CommitStatus {}).Where ("repo_id = ?" , repoID )
397
+ results := make ([]result , 0 , 10 )
398
+
399
+ sess := base .And ("updated_unix >= ?" , start ).
400
+ Select ("max( `index` ) as `index`, sha" ).
401
+ GroupBy ("context_hash, sha" ).OrderBy ("max( `index` ) desc" )
402
+
403
+ err := sess .Find (& results )
404
+ if err != nil {
399
405
return nil , err
400
406
}
401
407
402
- contexts := make ([]string , 0 , len (ids ))
403
- if len (ids ) == 0 {
408
+ contexts := make ([]string , 0 , len (results ))
409
+ if len (results ) == 0 {
404
410
return contexts , nil
405
411
}
406
- return contexts , db .GetEngine (ctx ).Select ("context" ).Table ("commit_status" ).In ("id" , ids ).Find (& contexts )
412
+
413
+ conds := make ([]builder.Cond , 0 , len (results ))
414
+ for _ , result := range results {
415
+ conds = append (conds , builder.Eq {"`index`" : result .Index , "sha" : result .SHA })
416
+ }
417
+ return contexts , base .And (builder .Or (conds ... )).Select ("context" ).Find (& contexts )
407
418
}
408
419
409
420
// NewCommitStatusOptions holds options for creating a CommitStatus
0 commit comments