Skip to content

Commit

Permalink
fix(notifiers): added customWebhook type
Browse files Browse the repository at this point in the history
  • Loading branch information
Rambatino committed Jul 26, 2024
1 parent 28edcc1 commit c2c5bdc
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
1 change: 0 additions & 1 deletion axiom/monitors_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,5 @@ func (s *MonitorsTestSuite) TestCreateMatchMonitor() {
})
s.Require().NoError(err)
s.Require().NotNil(monitor)

s.Equal(axiom.MonitorTypeMatchEvent.String(), monitor.Type.String())
}
11 changes: 11 additions & 0 deletions axiom/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type NotifierProperties struct {
Webhook *WebhookConfig `json:"webhook,omitempty"`
// MicrosoftTeams configuration.
MicrosoftTeams *MicrosoftTeams `json:"microsoftTeams,omitempty"`
// CustomWebhook configuration.
CustomWebhook *CustomWebhook `json:"customWebhook,omitempty"`
}

type DiscordConfig struct {
Expand Down Expand Up @@ -90,6 +92,15 @@ type MicrosoftTeams struct {
URL string `json:"microsoftTeamsUrl,omitempty"`
}

type CustomWebhook struct {
// URL is the destination URL for the webhook.
URL string
// Headers is a map of header keys and values to include in the webhook request.
Headers map[string]string
// Body is the body of the webhook request.
Body string
}

// Axiom API Reference: /v2/notifiers
type NotifiersService service

Expand Down
60 changes: 60 additions & 0 deletions axiom/notifiers_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,63 @@ func (s *NotifiersTestSuite) Test() {

s.Contains(notifiers, s.notifier)
}

func (s *NotifiersTestSuite) TestCreateCustomWebhookNotifier() {
// Create a custom webhook notifier.
notifier, err := s.client.Notifiers.Create(s.ctx, axiom.Notifier{
Name: "Custom Webhook Notifier",
Properties: axiom.NotifierProperties{
CustomWebhook: &axiom.CustomWebhook{
URL: "http://example.com/webhook",
Headers: map[string]string{
"Authorization": "Bearer token",
},
Body: "{\"key\":\"value\"}",
},
},
})
s.Require().NoError(err)
s.Require().NotNil(notifier)

s.notifier = notifier

// Get the notifier and make sure it matches what we have created.
notifier, err = s.client.Notifiers.Get(s.ctx, s.notifier.ID)
s.Require().NoError(err)
s.Require().NotNil(notifier)

s.Equal(s.notifier, notifier)

// Update the custom webhook notifier.
notifier, err = s.client.Notifiers.Update(s.ctx, s.notifier.ID, axiom.Notifier{
Name: "Updated Custom Webhook Notifier",
Properties: axiom.NotifierProperties{
CustomWebhook: &axiom.CustomWebhook{
URL: "http://example.com/updated-webhook",
Headers: map[string]string{
"Authorization": "Bearer new-token",
},
Body: "{\"key\":\"new-value\"}",
},
},
})
s.Require().NoError(err)
s.Require().NotNil(notifier)

s.notifier = notifier

// Get the notifier and make sure it matches what we have updated it to.
notifier, err = s.client.Notifiers.Get(s.ctx, s.notifier.ID)
s.Require().NoError(err)
s.Require().NotNil(notifier)

s.Equal(s.notifier, notifier)

// List all notifiers and make sure the created notifier is part of that
// list.
notifiers, err := s.client.Notifiers.List(s.ctx)
s.Require().NoError(err)
s.Require().NotEmpty(notifiers)

s.Contains(notifiers, s.notifier)
}
53 changes: 53 additions & 0 deletions axiom/notifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,56 @@ func TestNotifiersService_Delete(t *testing.T) {
err := client.Notifiers.Delete(context.Background(), "testID")
require.NoError(t, err)
}

func TestNotifiersService_Create_CustomWebhook(t *testing.T) {
exp := &Notifier{
ID: "test",
Name: "test",
Properties: NotifierProperties{
CustomWebhook: &CustomWebhook{
URL: "http://example.com/webhook",
Headers: map[string]string{
"Authorization": "Bearer token",
},
Body: "{\"key\":\"value\"}",
},
},
}
hf := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
assert.Equal(t, mediaTypeJSON, r.Header.Get("Content-Type"))

w.Header().Set("Content-Type", mediaTypeJSON)
_, err := fmt.Fprint(w, `{
"id": "test",
"name": "test",
"properties": {
"customWebhook": {
"url": "http://example.com/webhook",
"headers": {
"Authorization": "Bearer token"
},
"body": "{\"key\":\"value\"}"
}
}
}`)
assert.NoError(t, err)
}
client := setup(t, "/v2/notifiers", hf)

res, err := client.Notifiers.Create(context.Background(), Notifier{
Name: "test",
Properties: NotifierProperties{
CustomWebhook: &CustomWebhook{
URL: "http://example.com/webhook",
Headers: map[string]string{
"Authorization": "Bearer token",
},
Body: "{\"key\":\"value\"}",
},
},
})

require.NoError(t, err)
assert.Equal(t, exp, res)
}

0 comments on commit c2c5bdc

Please sign in to comment.