Skip to content

Commit

Permalink
Order rules by status on the details page (#96)
Browse files Browse the repository at this point in the history
Show approved and pending rules first and skipped rules last. When
multiple rules have the same status, preserve the order in which they
are defined by the policy.
  • Loading branch information
bluekeyes authored Jul 8, 2019
1 parent d1026ed commit f7969f9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion policy/common/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package common
type EvaluationStatus int

const (
StatusSkipped EvaluationStatus = iota
StatusSkipped EvaluationStatus = iota // note: values used for ordering
StatusPending
StatusApproved
StatusDisapproved
Expand Down
13 changes: 13 additions & 0 deletions server/handler/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ package handler
import (
"html/template"
"net/http"
"sort"
"strings"

"github.com/bluekeyes/templatetree"

"github.com/palantir/policy-bot/policy/common"
)

const (
Expand All @@ -35,6 +38,16 @@ type FilesConfig struct {
func LoadTemplates(c *FilesConfig) (templatetree.HTMLTree, error) {
root := template.New("root").Funcs(template.FuncMap{
"titlecase": strings.Title,
"sortByStatus": func(results []*common.Result) []*common.Result {
r := make([]*common.Result, len(results))
copy(r, results)

sort.SliceStable(r, func(i, j int) bool {
return r[i].Status > r[j].Status
})

return r
},
})

dir := c.Templates
Expand Down
4 changes: 2 additions & 2 deletions server/templates/details.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</div>
<div class="pl-8 overflow-auto flex-grow">
<ul class="tree px-4 pb-4">
{{range .Result.Children}}{{template "result" .}}{{end}}
{{range .Result.Children | sortByStatus}}{{template "result" .}}{{end}}
</ul>
</div>
{{end}}
Expand All @@ -44,7 +44,7 @@
</div>
{{if .Children}}
<ul class="tree">
{{range .Children}}{{template "result" .}}{{end}}
{{range .Children | sortByStatus}}{{template "result" .}}{{end}}
</ul>
{{end}}
</li>
Expand Down

0 comments on commit f7969f9

Please sign in to comment.