diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-07-03 05:32:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-03 05:32:21 +0200 |
commit | 7735da1c66c5a1511a2eac04b2a7f5c60b214c83 (patch) | |
tree | 09ddccb962cf37026b4516f49457bffcb90777cc /models/git/commit_status.go | |
parent | use css on labels (#25626) (diff) | |
download | forgejo-7735da1c66c5a1511a2eac04b2a7f5c60b214c83.tar.xz forgejo-7735da1c66c5a1511a2eac04b2a7f5c60b214c83.zip |
Display branch commit status (#25608)
Fix #10388
This PR adds a status icon for every branch which has a status check for
the latest commit on branch list page.
<img width="1313" alt="图片"
src="https://github.com/go-gitea/gitea/assets/81045/727cd540-d03a-40c6-a7dd-e87c118af0ac">
Diffstat (limited to 'models/git/commit_status.go')
-rw-r--r-- | models/git/commit_status.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/models/git/commit_status.go b/models/git/commit_status.go index 49143a87e8..c418cd23eb 100644 --- a/models/git/commit_status.go +++ b/models/git/commit_status.go @@ -346,6 +346,53 @@ func GetLatestCommitStatusForPairs(ctx context.Context, repoIDsToLatestCommitSHA return repoStatuses, nil } +// GetLatestCommitStatusForRepoCommitIDs returns all statuses with a unique context for a given list of repo-sha pairs +func GetLatestCommitStatusForRepoCommitIDs(ctx context.Context, repoID int64, commitIDs []string) (map[string][]*CommitStatus, error) { + type result struct { + ID int64 + Sha string + } + + results := make([]result, 0, len(commitIDs)) + + sess := db.GetEngine(ctx).Table(&CommitStatus{}) + + // Create a disjunction of conditions for each repoID and SHA pair + conds := make([]builder.Cond, 0, len(commitIDs)) + for _, sha := range commitIDs { + conds = append(conds, builder.Eq{"sha": sha}) + } + sess = sess.Where(builder.Eq{"repo_id": repoID}.And(builder.Or(conds...))). + Select("max( id ) as id, sha"). + GroupBy("context_hash, sha").OrderBy("max( id ) desc") + + err := sess.Find(&results) + if err != nil { + return nil, err + } + + ids := make([]int64, 0, len(results)) + repoStatuses := make(map[string][]*CommitStatus) + for _, result := range results { + ids = append(ids, result.ID) + } + + statuses := make([]*CommitStatus, 0, len(ids)) + if len(ids) > 0 { + err = db.GetEngine(ctx).In("id", ids).Find(&statuses) + if err != nil { + return nil, err + } + + // Group the statuses by repo ID + for _, status := range statuses { + repoStatuses[status.SHA] = append(repoStatuses[status.SHA], status) + } + } + + return repoStatuses, nil +} + // FindRepoRecentCommitStatusContexts returns repository's recent commit status contexts func FindRepoRecentCommitStatusContexts(ctx context.Context, repoID int64, before time.Duration) ([]string, error) { start := timeutil.TimeStampNow().AddDuration(-before) |