Skip to content

Commit

Permalink
Upgrade to templatetree to 0.5.0 (#655)
Browse files Browse the repository at this point in the history
This includes some minor improvements to error messages and enables us
to embed templates in the policy-bot binary in the future.
  • Loading branch information
bluekeyes authored Dec 7, 2023
1 parent cbf9119 commit ed2b4ca
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 231 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
github.com/alexedwards/scs v1.4.1
github.com/bluekeyes/hatpear v0.1.1
github.com/bluekeyes/templatetree v0.1.0
github.com/bluekeyes/templatetree v0.5.0
github.com/c2h5oh/datasize v0.0.0-20171227191756-4eba002a5eae
github.com/die-net/lrucache v0.0.0-20181227122439-19a39ef22a11
github.com/google/go-github/v56 v56.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNg
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/bluekeyes/hatpear v0.1.1 h1:FA5diKynoYJi6YVTJPEDbe4MG6eA8h+7LYHUlm8bppc=
github.com/bluekeyes/hatpear v0.1.1/go.mod h1:2bh+rl4wLhqzzL0hT7Q4SVGXIivrE8oKgH2WYM3ubt0=
github.com/bluekeyes/templatetree v0.1.0 h1:XKehBOS8+QciDUgjQJbqOxOJkZTpXWvZVcPAXDZf2TQ=
github.com/bluekeyes/templatetree v0.1.0/go.mod h1:F0jp7CTgfIsYPrplxgA43Ipv/lQG0c75DtpFO98fvAQ=
github.com/bluekeyes/templatetree v0.5.0 h1:bJ5W/D/SzB9GWzUG0G+Js8duOAwasE1vEWndxxQRoO0=
github.com/bluekeyes/templatetree v0.5.0/go.mod h1:doLcw8f06r6e1+9R30QkqYeq6/dDwxeQAF0FHjiOrIs=
github.com/bradleyfalzon/ghinstallation/v2 v2.8.0 h1:yUmoVv70H3J4UOqxqsee39+KlXxNEDfTbAp8c/qULKk=
github.com/bradleyfalzon/ghinstallation/v2 v2.8.0/go.mod h1:fmPmvCiBWhJla3zDv9ZTQSZc8AbwyRnGW1yg5ep1Pcs=
github.com/c2h5oh/datasize v0.0.0-20171227191756-4eba002a5eae h1:2Zmk+8cNvAGuY8AyvZuWpUdpQUAXwfom4ReVMe/CTIo=
Expand Down
3 changes: 2 additions & 1 deletion server/handler/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package handler

import (
"fmt"
"html/template"
"net/http"
"net/url"
"path"
Expand All @@ -35,7 +36,7 @@ import (
type Details struct {
Base
Sessions *scs.Manager
Templates templatetree.HTMLTree
Templates templatetree.Tree[*template.Template]
}

func (h *Details) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
Expand Down
68 changes: 34 additions & 34 deletions server/handler/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,49 +41,49 @@ type Membership struct {
Link string
}

func LoadTemplates(c *FilesConfig, basePath string, githubURL string) (templatetree.HTMLTree, error) {
func LoadTemplates(c *FilesConfig, basePath string, githubURL string) (templatetree.Tree[*template.Template], error) {
if basePath == "" {
basePath = "/"
}

root := template.New("root").Funcs(template.FuncMap{
"resource": func(r string) string {
return path.Join(basePath, "static", r)
},
"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
},
"hasActors": func(requires *common.Requires) bool {
return len(requires.Actors.Users) > 0 || len(requires.Actors.Teams) > 0 || len(requires.Actors.Organizations) > 0
},
"getMethods": func(results *common.Result) map[string][]string {
return getMethods(results)
},
"getActors": func(results *common.Result) map[string][]Membership {
return getActors(results, strings.TrimSuffix(githubURL, "/"))
},
"hasActorsPermissions": func(requires *common.Requires) bool {
return len(requires.Actors.GetPermissions()) > 0
},
"getPermissions": func(results *common.Result) []string {
return getPermissions(results)
},
})

dir := c.Templates
if dir == "" {
dir = DefaultTemplatesDir
}

return templatetree.LoadHTML(dir, "*.html.tmpl", root)
return templatetree.Parse(dir, "*.html.tmpl", func(name string) templatetree.Template[*template.Template] {
return template.New(name).Funcs(template.FuncMap{
"resource": func(r string) string {
return path.Join(basePath, "static", r)
},
"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
},
"hasActors": func(requires *common.Requires) bool {
return len(requires.Actors.Users) > 0 || len(requires.Actors.Teams) > 0 || len(requires.Actors.Organizations) > 0
},
"getMethods": func(results *common.Result) map[string][]string {
return getMethods(results)
},
"getActors": func(results *common.Result) map[string][]Membership {
return getActors(results, strings.TrimSuffix(githubURL, "/"))
},
"hasActorsPermissions": func(requires *common.Requires) bool {
return len(requires.Actors.GetPermissions()) > 0
},
"getPermissions": func(results *common.Result) []string {
return getPermissions(results)
},
})
})
}

func Static(prefix string, c *FilesConfig) http.Handler {
Expand Down
3 changes: 2 additions & 1 deletion server/handler/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package handler

import (
"html/template"
"net/http"

"github.com/bluekeyes/templatetree"
Expand All @@ -26,7 +27,7 @@ type Index struct {
Base

GithubConfig *githubapp.Config
Templates templatetree.HTMLTree
Templates templatetree.Tree[*template.Template]
}

func (h *Index) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
Expand Down
23 changes: 23 additions & 0 deletions vendor/github.com/bluekeyes/templatetree/.golangci.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 18 additions & 14 deletions vendor/github.com/bluekeyes/templatetree/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 10 additions & 36 deletions vendor/github.com/bluekeyes/templatetree/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ed2b4ca

Please sign in to comment.