Skip to content

Commit

Permalink
Display commit status on landing page of repo (#1784)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DblK authored and lunny committed Sep 14, 2017
1 parent a89692d commit be3319b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 28 deletions.
34 changes: 23 additions & 11 deletions models/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,26 @@ func (status *CommitStatus) APIFormat() *api.Status {
return apiStatus
}

// CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc
func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
var lastStatus *CommitStatus
var state CommitStatusState
for _, status := range statuses {
if status.State.IsWorseThan(state) {
state = status.State
lastStatus = status
}
}
if lastStatus == nil {
if len(statuses) > 0 {
lastStatus = statuses[0]
} else {
lastStatus = &CommitStatus{}
}
}
return lastStatus
}

// GetCommitStatuses returns all statuses for a given commit.
func GetCommitStatuses(repo *Repository, sha string, page int) ([]*CommitStatus, error) {
statuses := make([]*CommitStatus, 0, 10)
Expand Down Expand Up @@ -255,8 +275,7 @@ func NewCommitStatus(repo *Repository, creator *User, sha string, status *Commit

// SignCommitWithStatuses represents a commit with validation of signature and status state.
type SignCommitWithStatuses struct {
Statuses []*CommitStatus
State CommitStatusState
Status *CommitStatus
*SignCommit
}

Expand All @@ -265,25 +284,18 @@ func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List
var (
newCommits = list.New()
e = oldCommits.Front()
err error
)

for e != nil {
c := e.Value.(SignCommit)
commit := SignCommitWithStatuses{
SignCommit: &c,
State: "",
Statuses: make([]*CommitStatus, 0),
}
commit.Statuses, err = GetLatestCommitStatus(repo, commit.ID.String(), 0)
statuses, err := GetLatestCommitStatus(repo, commit.ID.String(), 0)
if err != nil {
log.Error(3, "GetLatestCommitStatus: %v", err)
} else {
for _, status := range commit.Statuses {
if status.State.IsWorseThan(commit.State) {
commit.State = status.State
}
}
commit.Status = CalcCommitStatus(statuses)
}

newCommits.PushBack(commit)
Expand Down
10 changes: 10 additions & 0 deletions routers/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"

"github.com/Unknwon/paginater"
)

Expand Down Expand Up @@ -208,6 +210,14 @@ func Diff(ctx *context.Context) {
if len(commitID) != 40 {
commitID = commit.ID.String()
}

statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), 0)
if err != nil {
log.Error(3, "GetLatestCommitStatus: %v", err)
}

ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)

diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
commitID, setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
Expand Down
8 changes: 8 additions & 0 deletions routers/repo/view.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
Expand Down Expand Up @@ -120,6 +121,13 @@ func renderDirectory(ctx *context.Context, treeLink string) {
ctx.Data["LatestCommitVerification"] = models.ParseCommitWithSignature(latestCommit)
ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit)

statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), 0)
if err != nil {
log.Error(3, "GetLatestCommitStatus: %v", err)
}

ctx.Data["LatestCommitStatus"] = models.CalcCommitStatus(statuses)

// Check permission to add or upload new file.
if ctx.Repo.IsWriter() && ctx.Repo.IsViewBranch {
ctx.Data["CanAddFile"] = true
Expand Down
15 changes: 15 additions & 0 deletions templates/repo/commit_status.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{if eq .State "pending"}}
<a href="{{.TargetURL}}" target=_blank><i class="commit-status circle icon yellow"></i></a>
{{end}}
{{if eq .State "success"}}
<a href="{{.TargetURL}}" target=_blank><i class="commit-status check icon green"></i></a>
{{end}}
{{if eq .State "error"}}
<a href="{{.TargetURL}}" target=_blank><i class="commit-status warning icon red"></i></a>
{{end}}
{{if eq .State "failure"}}
<a href="{{.TargetURL}}" target=_blank><i class="commit-status remove icon red"></i></a>
{{end}}
{{if eq .State "warning"}}
<a href="{{.TargetURL}}" target=_blank><i class="commit-status warning sign icon yellow"></i></a>
{{end}}
16 changes: 1 addition & 15 deletions templates/repo/commits_table.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,7 @@
</td>
<td class="message collapsing">
<span class="has-emoji{{if gt .ParentCount 1}} grey text{{end}}">{{RenderCommitMessage false .Summary $.RepoLink $.Repository.ComposeMetas}}</span>
{{if eq .State "pending"}}
<i class="commit-status circle icon yellow"></i>
{{end}}
{{if eq .State "success"}}
<i class="commit-status check icon green"></i>
{{end}}
{{if eq .State "error"}}
<i class="commit-status warning icon red"></i>
{{end}}
{{if eq .State "failure"}}
<i class="commit-status remove icon red"></i>
{{end}}
{{if eq .State "warning"}}
<i class="commit-status warning sign icon yellow"></i>
{{end}}
{{template "repo/commit_status" .Status}}
</td>
<td class="grey text right aligned">{{TimeSince .Author.When $.Lang}}</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/diff/page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<a class="ui floated right blue tiny button" href="{{EscapePound .SourcePath}}">
{{.i18n.Tr "repo.diff.browse_source"}}
</a>
{{RenderCommitMessage true .Commit.Message $.RepoLink $.Repository.ComposeMetas}}
<h3>{{RenderCommitMessage false .Commit.Message $.RepoLink $.Repository.ComposeMetas}}{{template "repo/commit_status" .CommitStatus}}</h3>
</div>
<div class="ui attached info segment {{if .Commit.Signature}} isSigned {{if .Verification.Verified }} isVerified {{end}}{{end}}">
{{if .Author}}
Expand Down
3 changes: 2 additions & 1 deletion templates/repo/view_list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
</div>
{{end}}
</a>
<span class="grey has-emoji">{{RenderCommitMessage false .LatestCommit.Summary .RepoLink $.Repository.ComposeMetas}}</span>
<span class="grey has-emoji">{{RenderCommitMessage false .LatestCommit.Summary .RepoLink $.Repository.ComposeMetas}}
{{template "repo/commit_status" .LatestCommitStatus}}</span>
</th>
<th class="nine wide">
</th>
Expand Down

0 comments on commit be3319b

Please sign in to comment.