Skip to content

Commit 14d13d8

Browse files
committed
Replace max( id ) in GetLatestCommitStatusForPairs
1 parent d799457 commit 14d13d8

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

models/git/commit_status.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -291,22 +291,22 @@ func GetLatestCommitStatus(ctx context.Context, repoID int64, sha string, listOp
291291
// GetLatestCommitStatusForPairs returns all statuses with a unique context for a given list of repo-sha pairs
292292
func GetLatestCommitStatusForPairs(ctx context.Context, repoIDsToLatestCommitSHAs map[int64]string, listOptions db.ListOptions) (map[int64][]*CommitStatus, error) {
293293
type result struct {
294-
ID int64
294+
Index int64
295295
RepoID int64
296296
}
297297

298298
results := make([]result, 0, len(repoIDsToLatestCommitSHAs))
299299

300-
sess := db.GetEngine(ctx).Table(&CommitStatus{})
300+
base := db.GetEngine(ctx).Table(&CommitStatus{})
301301

302302
// Create a disjunction of conditions for each repoID and SHA pair
303303
conds := make([]builder.Cond, 0, len(repoIDsToLatestCommitSHAs))
304304
for repoID, sha := range repoIDsToLatestCommitSHAs {
305305
conds = append(conds, builder.Eq{"repo_id": repoID, "sha": sha})
306306
}
307-
sess = sess.Where(builder.Or(conds...)).
308-
Select("max( id ) as id, repo_id").
309-
GroupBy("context_hash, repo_id").OrderBy("max( id ) desc")
307+
sess := base.Where(builder.Or(conds...)).
308+
Select("max( `index` ) as `index`, repo_id").
309+
GroupBy("context_hash, repo_id").OrderBy("max( `index` ) desc")
310310

311311
if !listOptions.IsListAll() {
312312
sess = db.SetSessionPagination(sess, &listOptions)
@@ -317,15 +317,21 @@ func GetLatestCommitStatusForPairs(ctx context.Context, repoIDsToLatestCommitSHA
317317
return nil, err
318318
}
319319

320-
ids := make([]int64, 0, len(results))
321320
repoStatuses := make(map[int64][]*CommitStatus)
322-
for _, result := range results {
323-
ids = append(ids, result.ID)
324-
}
325321

326-
statuses := make([]*CommitStatus, 0, len(ids))
327-
if len(ids) > 0 {
328-
err = db.GetEngine(ctx).In("id", ids).Find(&statuses)
322+
if len(results) > 0 {
323+
statuses := make([]*CommitStatus, 0, len(results))
324+
325+
conds = make([]builder.Cond, 0, len(results))
326+
for _, result := range results {
327+
cond := builder.Eq{
328+
"`index`": result.Index,
329+
"repo_id": result.RepoID,
330+
"sha": repoIDsToLatestCommitSHAs[result.RepoID],
331+
}
332+
conds = append(conds, cond)
333+
}
334+
err = base.Where(builder.Or(conds...)).Find(&statuses)
329335
if err != nil {
330336
return nil, err
331337
}

0 commit comments

Comments
 (0)