@@ -230,18 +230,24 @@ func (status *CommitStatus) HideActionsURL(ctx context.Context) {
230230
231231// CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc
232232func CalcCommitStatus (statuses []* CommitStatus ) * CommitStatus {
233+ // This function is widely used, but it is not quite right.
234+ // Ideally it should return something like "CommitStatusSummary" with properly aggregated state.
235+ // GitHub's behavior: if all statuses are "skipped", GitHub will return "success" as the combined status.
233236 var lastStatus * CommitStatus
234237 state := api .CommitStatusSuccess
235238 for _ , status := range statuses {
236- if status .State . NoBetterThan (state ) {
239+ if state == status .State || status . State . HasHigherPriorityThan (state ) {
237240 state = status .State
238241 lastStatus = status
239242 }
240243 }
241244 if lastStatus == nil {
242245 if len (statuses ) > 0 {
246+ // FIXME: a bad case: Gitea just returns the first commit status, its status is "skipped" in this case.
243247 lastStatus = statuses [0 ]
244248 } else {
249+ // FIXME: another bad case: if the "statuses" slice is empty, the returned value is an invalid CommitStatus, all its fields are empty.
250+ // Frontend code (tmpl&vue) sometimes depend on the empty fields to skip rendering commit status elements (need to double check in the future)
245251 lastStatus = & CommitStatus {}
246252 }
247253 }
@@ -298,27 +304,37 @@ type CommitStatusIndex struct {
298304 MaxIndex int64 `xorm:"index"`
299305}
300306
307+ func makeRepoCommitQuery (ctx context.Context , repoID int64 , sha string ) * xorm.Session {
308+ return db .GetEngine (ctx ).Table (& CommitStatus {}).
309+ Where ("repo_id = ?" , repoID ).And ("sha = ?" , sha )
310+ }
311+
301312// 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- }
313+ func GetLatestCommitStatus (ctx context.Context , repoID int64 , sha string , listOptions db.ListOptions ) ([]* CommitStatus , error ) {
307314 indices := make ([]int64 , 0 , 10 )
308- sess := getBase ().Select ("max( `index` ) as `index`" ).
309- GroupBy ("context_hash" ).OrderBy ("max( `index` ) desc" )
315+ sess := makeRepoCommitQuery (ctx , repoID , sha ).
316+ Select ("max( `index` ) as `index`" ).
317+ GroupBy ("context_hash" ).
318+ OrderBy ("max( `index` ) desc" )
310319 if ! listOptions .IsListAll () {
311320 sess = db .SetSessionPagination (sess , & listOptions )
312321 }
313- count , err := sess .FindAndCount (& indices )
314- if err != nil {
315- return nil , count , err
322+ if err := sess .Find (& indices ); err != nil {
323+ return nil , err
316324 }
317325 statuses := make ([]* CommitStatus , 0 , len (indices ))
318326 if len (indices ) == 0 {
319- return statuses , count , nil
327+ return statuses , nil
320328 }
321- return statuses , count , getBase ().And (builder .In ("`index`" , indices )).Find (& statuses )
329+ err := makeRepoCommitQuery (ctx , repoID , sha ).And (builder .In ("`index`" , indices )).Find (& statuses )
330+ return statuses , err
331+ }
332+
333+ func CountLatestCommitStatus (ctx context.Context , repoID int64 , sha string ) (int64 , error ) {
334+ return makeRepoCommitQuery (ctx , repoID , sha ).
335+ Select ("count(context_hash)" ).
336+ GroupBy ("context_hash" ).
337+ Count ()
322338}
323339
324340// GetLatestCommitStatusForPairs returns all statuses with a unique context for a given list of repo-sha pairs
0 commit comments