Skip to content

Commit

Permalink
feat(notify): jinja templating on params
Browse files Browse the repository at this point in the history
allows use of things like {{ version }} and {{ service_id }} in notify params
  • Loading branch information
JosephKav committed Jun 23, 2022
1 parent 89a0706 commit da0e9f3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
9 changes: 7 additions & 2 deletions notifiers/shoutrrr/shoutrrr.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/release-argus/Argus/web/metrics"
)

func (s *Shoutrrr) GetParams() (params *shoutrrr_types.Params) {
func (s *Shoutrrr) GetParams(context *utils.ServiceInfo) (params *shoutrrr_types.Params) {
p := make(shoutrrr_types.Params)
params = &p

Expand Down Expand Up @@ -61,6 +61,11 @@ func (s *Shoutrrr) GetParams() (params *shoutrrr_types.Params) {
}
}

// Apply Jinja templating
for key := range *params {
(*params)[key] = utils.TemplateString((*params)[key], *context)
}

return
}

Expand Down Expand Up @@ -229,7 +234,7 @@ func (s *Slice) Send(
errs <- err
return
}
params := shoutrrr.GetParams()
params := shoutrrr.GetParams(serviceInfo)
if params == nil {
p := make(shoutrrr_types.Params)
params = &p
Expand Down
5 changes: 3 additions & 2 deletions notifiers/shoutrrr/shoutrrr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

shoutrrr_types "github.com/containrrr/shoutrrr/pkg/types"
"github.com/release-argus/Argus/utils"
)

func TestShoutrrr(t *testing.T) {
Expand All @@ -39,7 +40,7 @@ func TestShoutrrr(t *testing.T) {
}

test.initParams()
gotParams := test.GetParams()
gotParams := test.GetParams(&utils.ServiceInfo{})
for key := range *gotParams {
if (*gotParams)[key] != wantedParams[key] {
t.Fatalf(`Shoutrrr, GetParams - Got %v, want match for %q`, (*gotParams)[key], wantedParams[key])
Expand All @@ -65,7 +66,7 @@ func TestShoutrrr(t *testing.T) {
"icon": "github",
}
test.initParams()
gotParams = test.GetParams()
gotParams = test.GetParams(&utils.ServiceInfo{})
for key := range *gotParams {
if (*gotParams)[key] != wantedParams[key] {
t.Fatalf(`Shoutrrr, GetParams - Got %v, want match for %q`, (*gotParams)[key], wantedParams[key])
Expand Down
7 changes: 6 additions & 1 deletion testing/shoutrrr.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ func TestNotify(flag *string, cfg *config.Config) {

title := (*shoutrrr)["test"].GetTitle(&utils.ServiceInfo{ID: "Test"})
message := "TEST - " + (*shoutrrr)["test"].GetMessage(&utils.ServiceInfo{ID: "NAME_OF_SERVICE", URL: "QUERY_URL", WebURL: "WEB_URL", LatestVersion: "MAJOR.MINOR.PATCH"})
err := shoutrrr.Send(title, message, &utils.ServiceInfo{})
err := shoutrrr.Send(title, message, &utils.ServiceInfo{
ID: "ID",
URL: "URL",
WebURL: "WebURL",
LatestVersion: "MAJOR.MINOR.PATCH",
})
jLog.Info(fmt.Sprintf("Message sent successfully with %q config\n", *flag), logFrom, err == nil)
jLog.Fatal(fmt.Sprintf("Message failed to send with %q config\n%s\n", *flag, utils.ErrorToString(err)), logFrom, err != nil)
os.Exit(0)
Expand Down
11 changes: 9 additions & 2 deletions utils/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
package utils

import (
"strings"

"github.com/flosch/pongo2/v5"
)

// TemplateString with pongo2 and `context`.
func TemplateString(tmpl string, context ServiceInfo) string {
func TemplateString(template string, context ServiceInfo) string {
// If the string isn't a Jinja template
if !strings.Contains(template, "{") {
return template
}

// Compile the template.
tpl, err := pongo2.FromString(tmpl)
tpl, err := pongo2.FromString(template)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit da0e9f3

Please sign in to comment.