-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Add commit status on repo and user pull request lists #2519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
deb88a4
f3a43b4
efe0220
3e0a87c
4824f72
15e49d6
8f54636
53c4f6f
bc4470a
b2ba574
008c6bc
809f6d8
207fccd
09aac6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // Copyright 2017 The Gitea Authors. All rights reserved. | ||
| // Use of this source code is governed by a MIT-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package integrations | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "path" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "code.gitea.io/gitea/models" | ||
| api "code.gitea.io/sdk/gitea" | ||
|
|
||
| "github.com/PuerkitoBio/goquery" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| var ( | ||
| statesIcons = map[models.CommitStatusState]string{ | ||
| models.CommitStatusPending: "circle icon yellow", | ||
| models.CommitStatusSuccess: "check icon green", | ||
| models.CommitStatusError: "warning icon red", | ||
| models.CommitStatusFailure: "remove icon red", | ||
| models.CommitStatusWarning: "warning sign icon yellow", | ||
| } | ||
| ) | ||
|
|
||
| func TestRepoPullsWithStatus(t *testing.T) { | ||
| prepareTestEnv(t) | ||
|
|
||
| session := loginUser(t, "user2") | ||
|
|
||
| var size = 5 | ||
| // create some pulls | ||
| for i := 0; i < size; i++ { | ||
| testEditFileToNewBranchAndSendPull(t, session, "user2", "repo16", "master", fmt.Sprintf("test%d", i), "readme.md", fmt.Sprintf("test%d", i)) | ||
| } | ||
|
|
||
| // look for repo's pulls page | ||
| req := NewRequest(t, "GET", "/user2/repo16/pulls") | ||
| resp := session.MakeRequest(t, req, http.StatusOK) | ||
| doc := NewHTMLParser(t, resp.Body) | ||
|
|
||
| var indexes = make([]string, 0, size) | ||
| doc.doc.Find("li.item").Each(func(idx int, s *goquery.Selection) { | ||
| indexes = append(indexes, strings.TrimLeft(s.Find("div").Eq(1).Text(), "#")) | ||
| }) | ||
|
|
||
| indexes = indexes[:5] | ||
|
|
||
| var status = make([]models.CommitStatusState, len(indexes)) | ||
| for i := 0; i < len(indexes); i++ { | ||
| switch i { | ||
| case 0: | ||
| status[i] = models.CommitStatusPending | ||
| case 1: | ||
| status[i] = models.CommitStatusSuccess | ||
| case 2: | ||
| status[i] = models.CommitStatusError | ||
| case 3: | ||
| status[i] = models.CommitStatusFailure | ||
| case 4: | ||
| status[i] = models.CommitStatusWarning | ||
| default: | ||
| status[i] = models.CommitStatusSuccess | ||
| } | ||
| } | ||
|
|
||
| for i, index := range indexes { | ||
| // Request repository commits page | ||
| req = NewRequestf(t, "GET", "/user2/repo16/pulls/%s/commits", index) | ||
| resp = session.MakeRequest(t, req, http.StatusOK) | ||
| doc = NewHTMLParser(t, resp.Body) | ||
|
|
||
| // Get first commit URL | ||
| commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href") | ||
| assert.True(t, exists) | ||
| assert.NotEmpty(t, commitURL) | ||
|
|
||
| commitID := path.Base(commitURL) | ||
| // Call API to add status for commit | ||
| req = NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo16/statuses/"+commitID, | ||
| api.CreateStatusOption{ | ||
| State: api.StatusState(status[i]), | ||
| TargetURL: "http://test.ci/", | ||
| Description: "", | ||
| Context: "testci", | ||
| }, | ||
| ) | ||
| session.MakeRequest(t, req, http.StatusCreated) | ||
|
|
||
| req = NewRequestf(t, "GET", "/user2/repo16/pulls/%s/commits", index) | ||
| resp = session.MakeRequest(t, req, http.StatusOK) | ||
| doc = NewHTMLParser(t, resp.Body) | ||
|
|
||
| commitURL, exists = doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href") | ||
| assert.True(t, exists) | ||
| assert.NotEmpty(t, commitURL) | ||
| assert.EqualValues(t, commitID, path.Base(commitURL)) | ||
|
|
||
| cls, ok := doc.doc.Find("#commits-table tbody tr td.message i.commit-status").Last().Attr("class") | ||
| assert.True(t, ok) | ||
| assert.EqualValues(t, "commit-status "+statesIcons[status[i]], cls) | ||
| } | ||
|
|
||
| req = NewRequest(t, "GET", "/user2/repo16/pulls") | ||
| resp = session.MakeRequest(t, req, http.StatusOK) | ||
| doc = NewHTMLParser(t, resp.Body) | ||
|
|
||
| doc.doc.Find("li.item").Each(func(i int, s *goquery.Selection) { | ||
| cls, ok := s.Find("i.commit-status").Attr("class") | ||
| assert.True(t, ok) | ||
| assert.EqualValues(t, "commit-status "+statesIcons[status[i]], cls) | ||
| }) | ||
|
|
||
| req = NewRequest(t, "GET", "/pulls?type=all&repo=16&sort=&state=open") | ||
| resp = session.MakeRequest(t, req, http.StatusOK) | ||
| doc = NewHTMLParser(t, resp.Body) | ||
|
|
||
| doc.doc.Find("li.item").Each(func(i int, s *goquery.Selection) { | ||
| cls, ok := s.Find("i.commit-status").Attr("class") | ||
| assert.True(t, ok) | ||
| assert.EqualValues(t, "commit-status "+statesIcons[status[i]], cls) | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -194,7 +194,7 @@ func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalB | |
| } | ||
|
|
||
| // Get posters. | ||
| for i := range issues { | ||
| for i := 0; i < len(issues); i++ { | ||
|
||
| // Check read status | ||
| if !ctx.IsSigned { | ||
| issues[i].IsRead = true | ||
|
|
@@ -203,6 +203,22 @@ func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalB | |
| return | ||
| } | ||
| } | ||
|
|
||
| if isPullOption == util.OptionalBoolTrue && len(issues) > 0 { | ||
| commitStatuses, err := models.GetIssuesLatestCommitStatuses(issues) | ||
| if err != nil { | ||
| ctx.ServerError("GetIssuesLatestCommitStatuses", err) | ||
| return | ||
| } | ||
|
|
||
| var issuesStates = make(map[int64]*models.CommitStatus, len(issues)) | ||
| for i, statuses := range commitStatuses { | ||
| issuesStates[issues[i].PullRequest.ID] = models.CalcCommitStatus(statuses) | ||
| } | ||
|
|
||
| ctx.Data["IssuesStates"] = issuesStates | ||
| } | ||
|
||
|
|
||
| ctx.Data["Issues"] = issues | ||
|
|
||
| // Get assignees. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,17 @@ | ||
| {{if eq .State "pending"}} | ||
| <a href="{{.TargetURL}}" target="_blank" rel="noopener noreferrer"><i class="commit-status circle icon yellow"></i></a> | ||
| {{end}} | ||
| {{if eq .State "success"}} | ||
| <a href="{{.TargetURL}}" target="_blank" rel="noopener noreferrer"><i class="commit-status check icon green"></i></a> | ||
| {{end}} | ||
| {{if eq .State "error"}} | ||
| <a href="{{.TargetURL}}" target="_blank" rel="noopener noreferrer"><i class="commit-status warning icon red"></i></a> | ||
| {{end}} | ||
| {{if eq .State "failure"}} | ||
| <a href="{{.TargetURL}}" target="_blank" rel="noopener noreferrer"><i class="commit-status remove icon red"></i></a> | ||
| {{end}} | ||
| {{if eq .State "warning"}} | ||
| <a href="{{.TargetURL}}" target="_blank" rel="noopener noreferrer"><i class="commit-status warning sign icon yellow"></i></a> | ||
| {{if .}} | ||
|
||
| {{if eq .State "pending"}} | ||
| <a href="{{.TargetURL}}" target="_blank" rel="noopener noreferrer"><i class="commit-status circle icon yellow"></i></a> | ||
| {{end}} | ||
| {{if eq .State "success"}} | ||
| <a href="{{.TargetURL}}" target="_blank" rel="noopener noreferrer"><i class="commit-status check icon green"></i></a> | ||
| {{end}} | ||
| {{if eq .State "error"}} | ||
| <a href="{{.TargetURL}}" target="_blank" rel="noopener noreferrer"><i class="commit-status warning icon red"></i></a> | ||
| {{end}} | ||
| {{if eq .State "failure"}} | ||
| <a href="{{.TargetURL}}" target="_blank" rel="noopener noreferrer"><i class="commit-status remove icon red"></i></a> | ||
| {{end}} | ||
| {{if eq .State "warning"}} | ||
| <a href="{{.TargetURL}}" target="_blank" rel="noopener noreferrer"><i class="commit-status warning sign icon yellow"></i></a> | ||
| {{end}} | ||
| {{end}} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use
GetLatestCommitStatus?If you need to fetch from multiple SHAs at the same time, just make a
GetLatestCommitsStatus(repo *Repository, shas []string, page int)and fetch theshasbefore that 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I would return a
map[string][]*CommitStatusto that you canret[shas[0]when using them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OR, you can have a
func (*Issue) PopulateLatestCommitStatus()that fetches them per Issue 🤔