From 2ac6bd136615ad2633bafaeb0520ec9269bb72ec Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 15 Apr 2019 20:39:24 +0200 Subject: [PATCH] Add has_topic filter for conditional templates Signed-off-by: Knut Ahlers --- template.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/template.go b/template.go index e3817b6..cf55886 100644 --- a/template.go +++ b/template.go @@ -4,10 +4,12 @@ import ( "strings" "github.com/flosch/pongo2" + "github.com/google/go-github/github" "github.com/gosimple/slug" ) func init() { + pongo2.RegisterFilter("has_topic", tplRepoHasTopic) pongo2.RegisterFilter("groovy_save", tplGroovyFileSave) pongo2.RegisterFilter("slugify", tplSlugify) } @@ -19,3 +21,23 @@ func tplSlugify(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { func tplGroovyFileSave(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { return pongo2.AsValue(strings.Replace(in.String(), "-", "_", -1)), nil } + +func tplRepoHasTopic(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { + repo, ok := in.Interface().(*github.Repository) + if !ok { + return nil, &pongo2.Error{ + Sender: "filter:has_topic", + ErrorMsg: "Input was no github.Repository", + } + } + + topic := param.String() + + for _, t := range repo.Topics { + if t == topic { + return pongo2.AsValue(true), nil + } + } + + return pongo2.AsValue(false), nil +}