Skip to content

Commit cf2e82e

Browse files
committed
Add Approval Counts to pulls list
Add simple approvals counts to pulls lists
1 parent b5f28d1 commit cf2e82e

File tree

7 files changed

+135
-0
lines changed

7 files changed

+135
-0
lines changed

models/issue_list.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,36 @@ func (issues IssueList) LoadComments() error {
515515
func (issues IssueList) LoadDiscussComments() error {
516516
return issues.loadComments(x, builder.Eq{"comment.type": CommentTypeComment})
517517
}
518+
519+
// GetApprovalCounts returns a map of issue ID to slice of approval counts
520+
func (issues IssueList) GetApprovalCounts() (map[int64][]*ReviewCount, error) {
521+
return issues.getApprovalCounts(x)
522+
}
523+
524+
func (issues IssueList) getApprovalCounts(e Engine) (map[int64][]*ReviewCount, error) {
525+
rCounts := make([]*ReviewCount, 0, 6*len(issues))
526+
ids := make([]int64, len(issues))
527+
for i, issue := range issues {
528+
ids[i] = issue.ID
529+
}
530+
sess := e.In("issue_id", ids)
531+
err := sess.Select("issue_id, type, official, count(id) as `count`").GroupBy("issue_id, type, official").OrderBy("issue_id").Table("review").Find(&rCounts)
532+
if err != nil {
533+
return nil, err
534+
}
535+
536+
approvalCountMap := make(map[int64][]*ReviewCount, len(issues))
537+
if len(rCounts) > 0 {
538+
start := 0
539+
lastID := rCounts[0].IssueID
540+
for i, current := range rCounts[1:] {
541+
if lastID != current.IssueID {
542+
approvalCountMap[lastID] = rCounts[start:i]
543+
start = i
544+
lastID = current.IssueID
545+
}
546+
}
547+
approvalCountMap[lastID] = rCounts[start:]
548+
}
549+
return approvalCountMap, nil
550+
}

models/pull.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,25 @@ func (pr *PullRequest) GetCommitMessages() string {
330330
return stringBuilder.String()
331331
}
332332

333+
// ReviewCount represents a count of Reviews
334+
type ReviewCount struct {
335+
IssueID int64
336+
Type ReviewType
337+
Official bool
338+
Count int64
339+
}
340+
341+
// GetApprovalCounts returns the approval counts by type
342+
func (pr *PullRequest) GetApprovalCounts() ([]*ReviewCount, error) {
343+
return pr.getApprovalCounts(x)
344+
}
345+
346+
func (pr *PullRequest) getApprovalCounts(e Engine) ([]*ReviewCount, error) {
347+
rCounts := make([]*ReviewCount, 0, 6)
348+
sess := e.Where("issue_id = ?", pr.IssueID)
349+
return rCounts, sess.Select("issue_id, type, official, count(id) as `count`").GroupBy("issue_id, type, official").Table("review").Find(&rCounts)
350+
}
351+
333352
// GetApprovers returns the approvers of the pull request
334353
func (pr *PullRequest) GetApprovers() string {
335354

options/locale/locale_en-US.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,15 @@ pulls.cannot_auto_merge_desc = This pull request cannot be merged automatically
10711071
pulls.cannot_auto_merge_helper = Merge manually to resolve the conflicts.
10721072
pulls.num_conflicting_files_1 = "%d conflicting file"
10731073
pulls.num_conflicting_files_n = "%d conflicting files"
1074+
pulls.approve_count_1 = "%d approval"
1075+
pulls.approve_count_n = "%d approvals"
1076+
pulls.approve_non_official_count_1 = "+%d non-official"
1077+
pulls.approve_non_official_count_n = "+%d non-official"
1078+
pulls.reject_count_1 = "%d change request"
1079+
pulls.reject_count_n = "%d change requests"
1080+
pulls.reject_non_official_count_1 = "+%d non-official"
1081+
pulls.reject_non_official_count_n = "+%d non-official"
1082+
10741083
pulls.no_merge_desc = This pull request cannot be merged because all repository merge options are disabled.
10751084
pulls.no_merge_helper = Enable merge options in the repository settings or merge the pull request manually.
10761085
pulls.no_merge_wip = This pull request can not be merged because it is marked as being a work in progress.

routers/repo/issue.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalB
216216
}
217217
}
218218

219+
approvalCounts, err := models.IssueList(issues).GetApprovalCounts()
220+
if err != nil {
221+
ctx.ServerError("ApprovalCounts", err)
222+
return
223+
}
224+
219225
var commitStatus = make(map[int64]*models.CommitStatus, len(issues))
220226

221227
// Get posters.
@@ -263,6 +269,22 @@ func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalB
263269
assigneeID = 0 // Reset ID to prevent unexpected selection of assignee.
264270
}
265271

272+
ctx.Data["ApprovalCounts"] = func(issueID int64, typ string, official bool) int64 {
273+
counts, ok := approvalCounts[issueID]
274+
if !ok || len(counts) == 0 {
275+
return 0
276+
}
277+
reviewTyp := models.ReviewTypeApprove
278+
if typ == "reject" {
279+
reviewTyp = models.ReviewTypeReject
280+
}
281+
for _, count := range counts {
282+
if count.Type == reviewTyp && count.Official == official {
283+
return count.Count
284+
}
285+
}
286+
return 0
287+
}
266288
ctx.Data["IssueStats"] = issueStats
267289
ctx.Data["SelLabelIDs"] = labelIDs
268290
ctx.Data["SelectLabels"] = selectLabels

routers/user/home.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,12 @@ func Issues(ctx *context.Context) {
494494
return
495495
}
496496

497+
approvalCounts, err := models.IssueList(issues).GetApprovalCounts()
498+
if err != nil {
499+
ctx.ServerError("ApprovalCounts", err)
500+
return
501+
}
502+
497503
showReposMap := make(map[int64]*models.Repository, len(counts))
498504
for repoID := range counts {
499505
if repoID > 0 {
@@ -577,6 +583,22 @@ func Issues(ctx *context.Context) {
577583
}
578584

579585
ctx.Data["Issues"] = issues
586+
ctx.Data["ApprovalCounts"] = func(issueID int64, typ string, official bool) int64 {
587+
counts, ok := approvalCounts[issueID]
588+
if !ok || len(counts) == 0 {
589+
return 0
590+
}
591+
reviewTyp := models.ReviewTypeApprove
592+
if typ == "reject" {
593+
reviewTyp = models.ReviewTypeReject
594+
}
595+
for _, count := range counts {
596+
if count.Type == reviewTyp && count.Official == official {
597+
return count.Count
598+
}
599+
}
600+
return 0
601+
}
580602
ctx.Data["CommitStatus"] = commitStatus
581603
ctx.Data["Repos"] = showRepos
582604
ctx.Data["Counts"] = counts

templates/repo/issue/list.tmpl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
</div>
203203

204204
<div class="issue list">
205+
{{ $approvalCounts := .ApprovalCounts}}
205206
{{range .Issues}}
206207
<li class="item">
207208
{{if $.CanWriteIssuesOrPulls}}
@@ -268,6 +269,20 @@
268269
</a>
269270
{{end}}
270271
{{if .IsPull}}
272+
{{$approveOfficial := call $approvalCounts .ID "approve" true}}
273+
{{$approveNonOfficial := call $approvalCounts .ID "approve" false}}
274+
{{$rejectOfficial := call $approvalCounts .ID "reject" true}}
275+
{{$rejectNonOfficial := call $approvalCounts .ID "reject" false}}
276+
{{if or (gt $approveOfficial 0) (gt $approveNonOfficial 0) (gt $rejectOfficial 0) (gt $rejectNonOfficial 0)}}
277+
<span class="approvals">{{svg "octicon-check" 16}}
278+
{{$.i18n.Tr (TrN $.i18n.Lang $approveOfficial "repo.pulls.approve_count_1" "repo.pulls.approve_count_n") $approveOfficial}}
279+
{{if gt $approveNonOfficial 0}}({{$.i18n.Tr (TrN $.i18n.Lang $approveNonOfficial "repo.pulls.approve_non_official_count_1" "repo.pulls.approve_non_official_count_n") $approveNonOfficial}}){{end}}</span>
280+
{{if or (gt $rejectOfficial 0) (gt $rejectNonOfficial 0)}}
281+
<span class="rejects">{{svg "octicon-x" 16}}
282+
{{$.i18n.Tr (TrN $.i18n.Lang $rejectOfficial "repo.pulls.reject_count_1" "repo.pulls.reject_count_n") $rejectOfficial}}
283+
{{if gt $rejectNonOfficial 0}}({{$.i18n.Tr (TrN $.i18n.Lang $rejectNonOfficial "repo.pulls.reject_non_official_count_1" "repo.pulls.reject_non_official_count_n") $rejectNonOfficial}}){{end}}</span>
284+
{{end}}
285+
{{end}}
271286
{{if and (not .PullRequest.HasMerged) ((len .PullRequest.ConflictedFiles) gt 0)}}
272287
<span class="conflicting">{{svg "octicon-mirror" 16}} {{$.i18n.Tr (TrN $.i18n.Lang (len .PullRequest.ConflictedFiles) "repo.pulls.num_conflicting_files_1" "repo.pulls.num_conflicting_files_n") (len .PullRequest.ConflictedFiles)}}</span>
273288
{{end}}

templates/user/dashboard/issues.tmpl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
</div>
8484

8585
<div class="issue list">
86+
{{ $approvalCounts := .ApprovalCounts}}
8687
{{range .Issues}}
8788

8889
{{ $timeStr:= TimeSinceUnix .CreatedUnix $.Lang }}
@@ -152,6 +153,20 @@
152153
</span>
153154
{{end}}
154155
{{if .IsPull}}
156+
{{$approveOfficial := call $approvalCounts .ID "approve" true}}
157+
{{$approveNonOfficial := call $approvalCounts .ID "approve" false}}
158+
{{$rejectOfficial := call $approvalCounts .ID "reject" true}}
159+
{{$rejectNonOfficial := call $approvalCounts .ID "reject" false}}
160+
{{if or (gt $approveOfficial 0) (gt $approveNonOfficial 0) (gt $rejectOfficial 0) (gt $rejectNonOfficial 0)}}
161+
<span class="approvals">{{svg "octicon-check" 16}}
162+
{{$.i18n.Tr (TrN $.i18n.Lang $approveOfficial "repo.pulls.approve_count_1" "repo.pulls.approve_count_n") $approveOfficial}}
163+
{{if gt $approveNonOfficial 0}}({{$.i18n.Tr (TrN $.i18n.Lang $approveNonOfficial "repo.pulls.approve_non_official_count_1" "repo.pulls.approve_non_official_count_n") $approveNonOfficial}}){{end}}</span>
164+
{{if or (gt $rejectOfficial 0) (gt $rejectNonOfficial 0)}}
165+
<span class="rejects">{{svg "octicon-x" 16}}
166+
{{$.i18n.Tr (TrN $.i18n.Lang $rejectOfficial "repo.pulls.reject_count_1" "repo.pulls.reject_count_n") $rejectOfficial}}
167+
{{if gt $rejectNonOfficial 0}}({{$.i18n.Tr (TrN $.i18n.Lang $rejectNonOfficial "repo.pulls.reject_non_official_count_1" "repo.pulls.reject_non_official_count_n") $rejectNonOfficial}}){{end}}</span>
168+
{{end}}
169+
{{end}}
155170
{{if and (not .PullRequest.HasMerged) ((len .PullRequest.ConflictedFiles) gt 0)}}
156171
<span class="conflicting">{{svg "octicon-mirror" 16}} {{$.i18n.Tr (TrN $.i18n.Lang (len .PullRequest.ConflictedFiles) "repo.pulls.num_conflicting_files_1" "repo.pulls.num_conflicting_files_n") (len .PullRequest.ConflictedFiles)}}</span>
157172
{{end}}

0 commit comments

Comments
 (0)