@@ -298,27 +298,37 @@ type CommitStatusIndex struct {
298298 MaxIndex int64 `xorm:"index"`
299299}
300300
301+ func makeRepoCommitQuery (ctx context.Context , repoID int64 , sha string ) * xorm.Session {
302+ return db .GetEngine (ctx ).Table (& CommitStatus {}).
303+ Where ("repo_id = ?" , repoID ).And ("sha = ?" , sha )
304+ }
305+
301306// GetLatestCommitStatus returns all statuses with a unique context for a given commit.
302- func GetLatestCommitStatus (ctx context.Context , repoID int64 , sha string , listOptions db.ListOptions ) ([]* CommitStatus , int64 , error ) {
303- getBase := func () * xorm.Session {
304- return db .GetEngine (ctx ).Table (& CommitStatus {}).
305- Where ("repo_id = ?" , repoID ).And ("sha = ?" , sha )
306- }
307+ func GetLatestCommitStatus (ctx context.Context , repoID int64 , sha string , listOptions db.ListOptions ) ([]* CommitStatus , error ) {
307308 indices := make ([]int64 , 0 , 10 )
308- sess := getBase ().Select ("max( `index` ) as `index`" ).
309- GroupBy ("context_hash" ).OrderBy ("max( `index` ) desc" )
309+ sess := makeRepoCommitQuery (ctx , repoID , sha ).
310+ Select ("max( `index` ) as `index`" ).
311+ GroupBy ("context_hash" ).
312+ OrderBy ("max( `index` ) desc" )
310313 if ! listOptions .IsListAll () {
311314 sess = db .SetSessionPagination (sess , & listOptions )
312315 }
313- count , err := sess .FindAndCount (& indices )
314- if err != nil {
315- return nil , count , err
316+ if err := sess .Find (& indices ); err != nil {
317+ return nil , err
316318 }
317319 statuses := make ([]* CommitStatus , 0 , len (indices ))
318320 if len (indices ) == 0 {
319- return statuses , count , nil
321+ return statuses , nil
320322 }
321- return statuses , count , getBase ().And (builder .In ("`index`" , indices )).Find (& statuses )
323+ err := makeRepoCommitQuery (ctx , repoID , sha ).And (builder .In ("`index`" , indices )).Find (& statuses )
324+ return statuses , err
325+ }
326+
327+ func CountLatestCommitStatus (ctx context.Context , repoID int64 , sha string ) (int64 , error ) {
328+ return makeRepoCommitQuery (ctx , repoID , sha ).
329+ Select ("count(context_hash)" ).
330+ GroupBy ("context_hash" ).
331+ Count ()
322332}
323333
324334// GetLatestCommitStatusForPairs returns all statuses with a unique context for a given list of repo-sha pairs
0 commit comments