Skip to content

Commit be3319b

Browse files
DblKlunny
authored andcommitted
Display commit status on landing page of repo (#1784)
* Display commit status on landing page of repo * improve last status of commits and add link to ci * fix last commit status since the order of ids are desc
1 parent a89692d commit be3319b

File tree

7 files changed

+60
-28
lines changed

7 files changed

+60
-28
lines changed

models/status.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,26 @@ func (status *CommitStatus) APIFormat() *api.Status {
126126
return apiStatus
127127
}
128128

129+
// CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc
130+
func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
131+
var lastStatus *CommitStatus
132+
var state CommitStatusState
133+
for _, status := range statuses {
134+
if status.State.IsWorseThan(state) {
135+
state = status.State
136+
lastStatus = status
137+
}
138+
}
139+
if lastStatus == nil {
140+
if len(statuses) > 0 {
141+
lastStatus = statuses[0]
142+
} else {
143+
lastStatus = &CommitStatus{}
144+
}
145+
}
146+
return lastStatus
147+
}
148+
129149
// GetCommitStatuses returns all statuses for a given commit.
130150
func GetCommitStatuses(repo *Repository, sha string, page int) ([]*CommitStatus, error) {
131151
statuses := make([]*CommitStatus, 0, 10)
@@ -255,8 +275,7 @@ func NewCommitStatus(repo *Repository, creator *User, sha string, status *Commit
255275

256276
// SignCommitWithStatuses represents a commit with validation of signature and status state.
257277
type SignCommitWithStatuses struct {
258-
Statuses []*CommitStatus
259-
State CommitStatusState
278+
Status *CommitStatus
260279
*SignCommit
261280
}
262281

@@ -265,25 +284,18 @@ func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List
265284
var (
266285
newCommits = list.New()
267286
e = oldCommits.Front()
268-
err error
269287
)
270288

271289
for e != nil {
272290
c := e.Value.(SignCommit)
273291
commit := SignCommitWithStatuses{
274292
SignCommit: &c,
275-
State: "",
276-
Statuses: make([]*CommitStatus, 0),
277293
}
278-
commit.Statuses, err = GetLatestCommitStatus(repo, commit.ID.String(), 0)
294+
statuses, err := GetLatestCommitStatus(repo, commit.ID.String(), 0)
279295
if err != nil {
280296
log.Error(3, "GetLatestCommitStatus: %v", err)
281297
} else {
282-
for _, status := range commit.Statuses {
283-
if status.State.IsWorseThan(commit.State) {
284-
commit.State = status.State
285-
}
286-
}
298+
commit.Status = CalcCommitStatus(statuses)
287299
}
288300

289301
newCommits.PushBack(commit)

routers/repo/commit.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
"code.gitea.io/gitea/models"
1414
"code.gitea.io/gitea/modules/base"
1515
"code.gitea.io/gitea/modules/context"
16+
"code.gitea.io/gitea/modules/log"
1617
"code.gitea.io/gitea/modules/setting"
18+
1719
"github.com/Unknwon/paginater"
1820
)
1921

@@ -208,6 +210,14 @@ func Diff(ctx *context.Context) {
208210
if len(commitID) != 40 {
209211
commitID = commit.ID.String()
210212
}
213+
214+
statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), 0)
215+
if err != nil {
216+
log.Error(3, "GetLatestCommitStatus: %v", err)
217+
}
218+
219+
ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
220+
211221
diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
212222
commitID, setting.Git.MaxGitDiffLines,
213223
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)

routers/repo/view.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
12
// Copyright 2014 The Gogs Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
@@ -120,6 +121,13 @@ func renderDirectory(ctx *context.Context, treeLink string) {
120121
ctx.Data["LatestCommitVerification"] = models.ParseCommitWithSignature(latestCommit)
121122
ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit)
122123

124+
statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), 0)
125+
if err != nil {
126+
log.Error(3, "GetLatestCommitStatus: %v", err)
127+
}
128+
129+
ctx.Data["LatestCommitStatus"] = models.CalcCommitStatus(statuses)
130+
123131
// Check permission to add or upload new file.
124132
if ctx.Repo.IsWriter() && ctx.Repo.IsViewBranch {
125133
ctx.Data["CanAddFile"] = true

templates/repo/commit_status.tmpl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{{if eq .State "pending"}}
2+
<a href="{{.TargetURL}}" target=_blank><i class="commit-status circle icon yellow"></i></a>
3+
{{end}}
4+
{{if eq .State "success"}}
5+
<a href="{{.TargetURL}}" target=_blank><i class="commit-status check icon green"></i></a>
6+
{{end}}
7+
{{if eq .State "error"}}
8+
<a href="{{.TargetURL}}" target=_blank><i class="commit-status warning icon red"></i></a>
9+
{{end}}
10+
{{if eq .State "failure"}}
11+
<a href="{{.TargetURL}}" target=_blank><i class="commit-status remove icon red"></i></a>
12+
{{end}}
13+
{{if eq .State "warning"}}
14+
<a href="{{.TargetURL}}" target=_blank><i class="commit-status warning sign icon yellow"></i></a>
15+
{{end}}

templates/repo/commits_table.tmpl

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,7 @@
6161
</td>
6262
<td class="message collapsing">
6363
<span class="has-emoji{{if gt .ParentCount 1}} grey text{{end}}">{{RenderCommitMessage false .Summary $.RepoLink $.Repository.ComposeMetas}}</span>
64-
{{if eq .State "pending"}}
65-
<i class="commit-status circle icon yellow"></i>
66-
{{end}}
67-
{{if eq .State "success"}}
68-
<i class="commit-status check icon green"></i>
69-
{{end}}
70-
{{if eq .State "error"}}
71-
<i class="commit-status warning icon red"></i>
72-
{{end}}
73-
{{if eq .State "failure"}}
74-
<i class="commit-status remove icon red"></i>
75-
{{end}}
76-
{{if eq .State "warning"}}
77-
<i class="commit-status warning sign icon yellow"></i>
78-
{{end}}
64+
{{template "repo/commit_status" .Status}}
7965
</td>
8066
<td class="grey text right aligned">{{TimeSince .Author.When $.Lang}}</td>
8167
</tr>

templates/repo/diff/page.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<a class="ui floated right blue tiny button" href="{{EscapePound .SourcePath}}">
1010
{{.i18n.Tr "repo.diff.browse_source"}}
1111
</a>
12-
{{RenderCommitMessage true .Commit.Message $.RepoLink $.Repository.ComposeMetas}}
12+
<h3>{{RenderCommitMessage false .Commit.Message $.RepoLink $.Repository.ComposeMetas}}{{template "repo/commit_status" .CommitStatus}}</h3>
1313
</div>
1414
<div class="ui attached info segment {{if .Commit.Signature}} isSigned {{if .Verification.Verified }} isVerified {{end}}{{end}}">
1515
{{if .Author}}

templates/repo/view_list.tmpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
</div>
2626
{{end}}
2727
</a>
28-
<span class="grey has-emoji">{{RenderCommitMessage false .LatestCommit.Summary .RepoLink $.Repository.ComposeMetas}}</span>
28+
<span class="grey has-emoji">{{RenderCommitMessage false .LatestCommit.Summary .RepoLink $.Repository.ComposeMetas}}
29+
{{template "repo/commit_status" .LatestCommitStatus}}</span>
2930
</th>
3031
<th class="nine wide">
3132
</th>

0 commit comments

Comments
 (0)