Skip to content

Commit 82d47a5

Browse files
committed
Replace max( id ) in FindRepoRecentCommitStatusContexts
1 parent cba443c commit 82d47a5

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

models/git/commit_status.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -388,22 +388,33 @@ func GetLatestCommitStatusForRepoCommitIDs(ctx context.Context, repoID int64, co
388388

389389
// FindRepoRecentCommitStatusContexts returns repository's recent commit status contexts
390390
func FindRepoRecentCommitStatusContexts(ctx context.Context, repoID int64, before time.Duration) ([]string, error) {
391+
type result struct {
392+
Index int64
393+
SHA string
394+
}
391395
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 {
399405
return nil, err
400406
}
401407

402-
contexts := make([]string, 0, len(ids))
403-
if len(ids) == 0 {
408+
contexts := make([]string, 0, len(results))
409+
if len(results) == 0 {
404410
return contexts, nil
405411
}
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)
407418
}
408419

409420
// NewCommitStatusOptions holds options for creating a CommitStatus

0 commit comments

Comments
 (0)