Browse Source

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
Rémy Boulanouar 1 year ago
parent
commit
be3319b3d5

+ 23 - 11
models/status.go

@@ -126,6 +126,26 @@ func (status *CommitStatus) APIFormat() *api.Status {
126 126
 	return apiStatus
127 127
 }
128 128
 
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
+
129 149
 // GetCommitStatuses returns all statuses for a given commit.
130 150
 func GetCommitStatuses(repo *Repository, sha string, page int) ([]*CommitStatus, error) {
131 151
 	statuses := make([]*CommitStatus, 0, 10)
@@ -255,8 +275,7 @@ func NewCommitStatus(repo *Repository, creator *User, sha string, status *Commit
255 275
 
256 276
 // SignCommitWithStatuses represents a commit with validation of signature and status state.
257 277
 type SignCommitWithStatuses struct {
258
-	Statuses []*CommitStatus
259
-	State    CommitStatusState
278
+	Status *CommitStatus
260 279
 	*SignCommit
261 280
 }
262 281
 
@@ -265,25 +284,18 @@ func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List
265 284
 	var (
266 285
 		newCommits = list.New()
267 286
 		e          = oldCommits.Front()
268
-		err        error
269 287
 	)
270 288
 
271 289
 	for e != nil {
272 290
 		c := e.Value.(SignCommit)
273 291
 		commit := SignCommitWithStatuses{
274 292
 			SignCommit: &c,
275
-			State:      "",
276
-			Statuses:   make([]*CommitStatus, 0),
277 293
 		}
278
-		commit.Statuses, err = GetLatestCommitStatus(repo, commit.ID.String(), 0)
294
+		statuses, err := GetLatestCommitStatus(repo, commit.ID.String(), 0)
279 295
 		if err != nil {
280 296
 			log.Error(3, "GetLatestCommitStatus: %v", err)
281 297
 		} 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)
287 299
 		}
288 300
 
289 301
 		newCommits.PushBack(commit)

+ 10 - 0
routers/repo/commit.go

@@ -13,7 +13,9 @@ import (
13 13
 	"code.gitea.io/gitea/models"
14 14
 	"code.gitea.io/gitea/modules/base"
15 15
 	"code.gitea.io/gitea/modules/context"
16
+	"code.gitea.io/gitea/modules/log"
16 17
 	"code.gitea.io/gitea/modules/setting"
18
+
17 19
 	"github.com/Unknwon/paginater"
18 20
 )
19 21
 
@@ -208,6 +210,14 @@ func Diff(ctx *context.Context) {
208 210
 	if len(commitID) != 40 {
209 211
 		commitID = commit.ID.String()
210 212
 	}
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
+
211 221
 	diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
212 222
 		commitID, setting.Git.MaxGitDiffLines,
213 223
 		setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)

+ 8 - 0
routers/repo/view.go

@@ -1,3 +1,4 @@
1
+// Copyright 2017 The Gitea Authors. All rights reserved.
1 2
 // Copyright 2014 The Gogs Authors. All rights reserved.
2 3
 // Use of this source code is governed by a MIT-style
3 4
 // license that can be found in the LICENSE file.
@@ -120,6 +121,13 @@ func renderDirectory(ctx *context.Context, treeLink string) {
120 121
 	ctx.Data["LatestCommitVerification"] = models.ParseCommitWithSignature(latestCommit)
121 122
 	ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit)
122 123
 
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
+
123 131
 	// Check permission to add or upload new file.
124 132
 	if ctx.Repo.IsWriter() && ctx.Repo.IsViewBranch {
125 133
 		ctx.Data["CanAddFile"] = true

+ 15 - 0
templates/repo/commit_status.tmpl

@@ -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}}

+ 1 - 15
templates/repo/commits_table.tmpl

@@ -61,21 +61,7 @@
61 61
 						</td>
62 62
 						<td class="message collapsing">
63 63
 							<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}}
79 65
 						</td>
80 66
 						<td class="grey text right aligned">{{TimeSince .Author.When $.Lang}}</td>
81 67
 					</tr>

+ 1 - 1
templates/repo/diff/page.tmpl

@@ -9,7 +9,7 @@
9 9
 				<a class="ui floated right blue tiny button" href="{{EscapePound .SourcePath}}">
10 10
 					{{.i18n.Tr "repo.diff.browse_source"}}
11 11
 				</a>
12
-				{{RenderCommitMessage true .Commit.Message $.RepoLink $.Repository.ComposeMetas}}
12
+				<h3>{{RenderCommitMessage false .Commit.Message $.RepoLink $.Repository.ComposeMetas}}{{template "repo/commit_status" .CommitStatus}}</h3>
13 13
 			</div>
14 14
 			<div class="ui attached info segment {{if .Commit.Signature}} isSigned {{if .Verification.Verified }} isVerified {{end}}{{end}}">
15 15
 				{{if .Author}}

+ 2 - 1
templates/repo/view_list.tmpl

@@ -25,7 +25,8 @@
25 25
 							</div>
26 26
 						{{end}}
27 27
 				</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>
29 30
 			</th>
30 31
 			<th class="nine wide">
31 32
 			</th>