From f5933aa7ff559b1642cb67c6d2e8f6d507d932f3 Mon Sep 17 00:00:00 2001 From: Vladislav Sukhin Date: Tue, 24 Dec 2024 18:30:18 +0300 Subject: [PATCH] fix: pass config to template Signed-off-by: Vladislav Sukhin --- pkg/event/kind/webhook/listener.go | 5 ++++- pkg/event/kind/webhook/listener_test.go | 12 ++++++------ pkg/event/kind/webhook/loader.go | 2 +- pkg/event/kind/webhook/templatevars.go | 6 ++++-- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/pkg/event/kind/webhook/listener.go b/pkg/event/kind/webhook/listener.go index 41615aa62f1..ea628ab78de 100644 --- a/pkg/event/kind/webhook/listener.go +++ b/pkg/event/kind/webhook/listener.go @@ -33,6 +33,7 @@ func NewWebhookListener(name, uri, selector string, events []testkube.EventType, metrics v1.Metrics, proContext *config.ProContext, envs map[string]string, + config map[string]string, ) *WebhookListener { return &WebhookListener{ name: name, @@ -50,6 +51,7 @@ func NewWebhookListener(name, uri, selector string, events []testkube.EventType, metrics: metrics, proContext: proContext, envs: envs, + config: config, } } @@ -69,6 +71,7 @@ type WebhookListener struct { metrics v1.Metrics proContext *config.ProContext envs map[string]string + config map[string]string } func (l *WebhookListener) Name() string { @@ -267,7 +270,7 @@ func (l *WebhookListener) processTemplate(field, body string, event testkube.Eve } var buffer bytes.Buffer - if err = tmpl.ExecuteTemplate(&buffer, field, NewTemplateVars(event, l.proContext)); err != nil { + if err = tmpl.ExecuteTemplate(&buffer, field, NewTemplateVars(event, l.proContext, l.config)); err != nil { log.Errorw(fmt.Sprintf("executing webhook %s error", field), "error", err) return nil, err } diff --git a/pkg/event/kind/webhook/listener_test.go b/pkg/event/kind/webhook/listener_test.go index 386424cb748..c60d73d641b 100644 --- a/pkg/event/kind/webhook/listener_test.go +++ b/pkg/event/kind/webhook/listener_test.go @@ -34,7 +34,7 @@ func TestWebhookListener_Notify(t *testing.T) { svr := httptest.NewServer(testHandler) defer svr.Close() - l := NewWebhookListener("l1", svr.URL, "", testEventTypes, "", "", nil, false, nil, nil, v1.NewMetrics(), nil, nil) + l := NewWebhookListener("l1", svr.URL, "", testEventTypes, "", "", nil, false, nil, nil, v1.NewMetrics(), nil, nil, nil) // when r := l.Notify(testkube.Event{ @@ -56,7 +56,7 @@ func TestWebhookListener_Notify(t *testing.T) { svr := httptest.NewServer(testHandler) defer svr.Close() - l := NewWebhookListener("l1", svr.URL, "", testEventTypes, "", "", nil, false, nil, nil, v1.NewMetrics(), nil, nil) + l := NewWebhookListener("l1", svr.URL, "", testEventTypes, "", "", nil, false, nil, nil, v1.NewMetrics(), nil, nil, nil) // when r := l.Notify(testkube.Event{ @@ -73,7 +73,7 @@ func TestWebhookListener_Notify(t *testing.T) { t.Parallel() // given - s := NewWebhookListener("l1", "http://baduri.badbadbad", "", testEventTypes, "", "", nil, false, nil, nil, v1.NewMetrics(), nil, nil) + s := NewWebhookListener("l1", "http://baduri.badbadbad", "", testEventTypes, "", "", nil, false, nil, nil, v1.NewMetrics(), nil, nil, nil) // when r := s.Notify(testkube.Event{ @@ -106,7 +106,7 @@ func TestWebhookListener_Notify(t *testing.T) { svr := httptest.NewServer(testHandler) defer svr.Close() - l := NewWebhookListener("l1", svr.URL, "", testEventTypes, "field", "", nil, false, nil, nil, v1.NewMetrics(), nil, nil) + l := NewWebhookListener("l1", svr.URL, "", testEventTypes, "field", "", nil, false, nil, nil, v1.NewMetrics(), nil, nil, nil) // when r := l.Notify(testkube.Event{ @@ -133,7 +133,7 @@ func TestWebhookListener_Notify(t *testing.T) { defer svr.Close() l := NewWebhookListener("l1", svr.URL, "", testEventTypes, "", "{\"id\": \"{{ .Id }}\"}", - map[string]string{"Content-Type": "application/json"}, false, nil, nil, v1.NewMetrics(), nil, nil) + map[string]string{"Content-Type": "application/json"}, false, nil, nil, v1.NewMetrics(), nil, nil, nil) // when r := l.Notify(testkube.Event{ @@ -150,7 +150,7 @@ func TestWebhookListener_Notify(t *testing.T) { t.Parallel() // given - s := NewWebhookListener("l1", "http://baduri.badbadbad", "", testEventTypes, "", "", nil, true, nil, nil, v1.NewMetrics(), nil, nil) + s := NewWebhookListener("l1", "http://baduri.badbadbad", "", testEventTypes, "", "", nil, true, nil, nil, v1.NewMetrics(), nil, nil, nil) // when r := s.Notify(testkube.Event{ diff --git a/pkg/event/kind/webhook/loader.go b/pkg/event/kind/webhook/loader.go index 42af64984ef..1efc0f3d0db 100644 --- a/pkg/event/kind/webhook/loader.go +++ b/pkg/event/kind/webhook/loader.go @@ -98,7 +98,7 @@ func (r WebhooksLoader) Load() (listeners common.Listeners, err error) { name, webhook.Spec.Uri, webhook.Spec.Selector, types, webhook.Spec.PayloadObjectField, payloadTemplate, webhook.Spec.Headers, webhook.Spec.Disabled, r.deprecatedRepositories, r.testWorkflowExecutionResults, - r.metrics, r.proContext, r.envs, + r.metrics, r.proContext, r.envs, nil, ), ) } diff --git a/pkg/event/kind/webhook/templatevars.go b/pkg/event/kind/webhook/templatevars.go index 391244f62af..66c61d90325 100644 --- a/pkg/event/kind/webhook/templatevars.go +++ b/pkg/event/kind/webhook/templatevars.go @@ -15,11 +15,13 @@ type TemplateVars struct { ArtifactCommand string LogsURL string LogsCommand string + Config map[string]string } -func NewTemplateVars(event testkube.Event, proContext *config.ProContext) TemplateVars { +func NewTemplateVars(event testkube.Event, proContext *config.ProContext, config map[string]string) TemplateVars { vars := TemplateVars{ - Event: event, + Event: event, + Config: config, } switch {