Skip to content

Commit

Permalink
fix: pass config to template
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Sukhin <[email protected]>
  • Loading branch information
vsukhin committed Dec 24, 2024
1 parent 12f5057 commit f5933aa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
5 changes: 4 additions & 1 deletion pkg/event/kind/webhook/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -50,6 +51,7 @@ func NewWebhookListener(name, uri, selector string, events []testkube.EventType,
metrics: metrics,
proContext: proContext,
envs: envs,
config: config,
}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/event/kind/webhook/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion pkg/event/kind/webhook/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/event/kind/webhook/templatevars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f5933aa

Please sign in to comment.