aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-07-03 11:32:21 +0800
committerGitHub <noreply@github.com>2023-07-03 03:32:21 +0000
commit7735da1c66c5a1511a2eac04b2a7f5c60b214c83 (patch)
tree09ddccb962cf37026b4516f49457bffcb90777cc
parent5b79eeabd1630b58315c4df09c63485f04e3eb45 (diff)
downloadgitea-7735da1c66c5a1511a2eac04b2a7f5c60b214c83.tar.gz
gitea-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">
-rw-r--r--models/git/commit_status.go47
-rw-r--r--routers/web/repo/branch.go18
-rw-r--r--templates/repo/branch/list.tmpl2
3 files changed, 67 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)
diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go
index f0282a71b8..8e3383848f 100644
--- a/routers/web/repo/branch.go
+++ b/routers/web/repo/branch.go
@@ -57,7 +57,25 @@ func Branches(ctx *context.Context) {
return
}
+ commitIDs := []string{defaultBranch.DBBranch.CommitID}
+ for _, branch := range branches {
+ commitIDs = append(commitIDs, branch.DBBranch.CommitID)
+ }
+
+ commitStatuses, err := git_model.GetLatestCommitStatusForRepoCommitIDs(ctx, ctx.Repo.Repository.ID, commitIDs)
+ if err != nil {
+ ctx.ServerError("LoadBranches", err)
+ return
+ }
+
+ commitStatus := make(map[string]*git_model.CommitStatus)
+ for commitID, cs := range commitStatuses {
+ commitStatus[commitID] = git_model.CalcCommitStatus(cs)
+ }
+
ctx.Data["Branches"] = branches
+ ctx.Data["CommitStatus"] = commitStatus
+ ctx.Data["CommitStatuses"] = commitStatuses
ctx.Data["DefaultBranchBranch"] = defaultBranch
pager := context.NewPagination(int(branchesCount), pageSize, page, 5)
pager.SetDefaultParams(ctx)
diff --git a/templates/repo/branch/list.tmpl b/templates/repo/branch/list.tmpl
index b03c8aa566..298d854381 100644
--- a/templates/repo/branch/list.tmpl
+++ b/templates/repo/branch/list.tmpl
@@ -25,6 +25,7 @@
<div class="gt-df gt-ac">
<a class="gt-ellipsis" href="{{.RepoLink}}/src/branch/{{PathEscapeSegments .DefaultBranchBranch.DBBranch.Name}}">{{.DefaultBranchBranch.DBBranch.Name}}</a>
<button class="btn interact-fg gt-p-3" data-clipboard-text="{{.DefaultBranchBranch.DBBranch.Name}}">{{svg "octicon-copy" 14}}</button>
+ {{template "repo/commit_statuses" dict "Status" (index $.CommitStatus .DefaultBranchBranch.DBBranch.CommitID) "Statuses" (index $.CommitStatuses .DefaultBranchBranch.DBBranch.CommitID)}}
</div>
<p class="info gt-df gt-ac gt-my-2">{{svg "octicon-git-commit" 16 "gt-mr-2"}}<a href="{{.RepoLink}}/commit/{{PathEscape .DefaultBranchBranch.DBBranch.CommitID}}">{{ShortSha .DefaultBranchBranch.DBBranch.CommitID}}</a> · <span class="commit-message">{{RenderCommitMessage $.Context .DefaultBranchBranch.DBBranch.CommitMessage .RepoLink .Repository.ComposeMetas}}</span> · {{.locale.Tr "org.repo_updated"}} {{TimeSince .DefaultBranchBranch.DBBranch.CommitTime.AsTime .locale}}{{if .DefaultBranchBranch.DBBranch.Pusher}} &nbsp;{{template "shared/user/avatarlink" dict "Context" $.Context "user" .DefaultBranchBranch.DBBranch.Pusher}}{{template "shared/user/namelink" .DefaultBranchBranch.DBBranch.Pusher}}{{end}}</p>
</td>
@@ -91,6 +92,7 @@
<div class="gt-df gt-ac">
<a class="gt-ellipsis" href="{{$.RepoLink}}/src/branch/{{PathEscapeSegments .DBBranch.Name}}">{{.DBBranch.Name}}</a>
<button class="btn interact-fg gt-p-3" data-clipboard-text="{{.DBBranch.Name}}">{{svg "octicon-copy" 14}}</button>
+ {{template "repo/commit_statuses" dict "Status" (index $.CommitStatus .DBBranch.CommitID) "Statuses" (index $.CommitStatuses .DBBranch.CommitID)}}
</div>
<p class="info gt-df gt-ac gt-my-2">{{svg "octicon-git-commit" 16 "gt-mr-2"}}<a href="{{$.RepoLink}}/commit/{{PathEscape .DBBranch.CommitID}}">{{ShortSha .DBBranch.CommitID}}</a> · <span class="commit-message">{{RenderCommitMessage $.Context .DBBranch.CommitMessage $.RepoLink $.Repository.ComposeMetas}}</span> · {{$.locale.Tr "org.repo_updated"}} {{TimeSince .DBBranch.CommitTime.AsTime $.locale}}{{if .DBBranch.Pusher}} &nbsp;{{template "shared/user/avatarlink" dict "Context" $.Context "user" .DBBranch.Pusher}} &nbsp;{{template "shared/user/namelink" .DBBranch.Pusher}}{{end}}</p>
{{end}}