Skip to content

Commit

Permalink
feat: notification actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jianyuan committed Dec 10, 2023
1 parent 881b50c commit e3efa78
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
71 changes: 71 additions & 0 deletions sentry/notification_actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package sentry

import (
"context"
"fmt"
"net/http"
)

type NotificationActionsService service

type CreateNotificationActionParams struct {
TriggerType *string `json:"triggerType"`
ServiceType *string `json:"serviceType"`
IntegrationId *int `json:"integrationId,omitempty"`
TargetIdentifier *string `json:"targetIdentifier,omitempty"`
TargetDisplay *string `json:"targetDisplay,omitempty"`
TargetType *string `json:"targetType,omitempty"`
Projects []string `json:"projects"`
}

type NotificationAction struct {
ID *int `json:"id"`
OrganizationId *int `json:"organizationId"`
TriggerType *string `json:"triggerType"`
ServiceType *string `json:"serviceType"`
IntegrationId *int `json:"integrationId"`
TargetIdentifier interface{} `json:"targetIdentifier"`
TargetDisplay *string `json:"targetDisplay"`
TargetType *string `json:"targetType"`
Projects []int `json:"projects"`
}

func (s *NotificationActionsService) Create(ctx context.Context, organizationSlug string, params *CreateNotificationActionParams) (*NotificationAction, *Response, error) {
u := fmt.Sprintf("0/organizations/%v/notifications/actions/", organizationSlug)
req, err := s.client.NewRequest(http.MethodPost, u, params)
if err != nil {
return nil, nil, err
}

action := &NotificationAction{}
resp, err := s.client.Do(ctx, req, action)
if err != nil {
return nil, resp, err
}
return action, resp, nil
}

func (s *NotificationActionsService) Update(ctx context.Context, organizationSlug string, actionId string, params *CreateNotificationActionParams) (*NotificationAction, *Response, error) {
u := fmt.Sprintf("0/organizations/%v/notification/actions/%v/", organizationSlug, actionId)
req, err := s.client.NewRequest(http.MethodPut, u, params)
if err != nil {
return nil, nil, err
}

action := &NotificationAction{}
resp, err := s.client.Do(ctx, req, action)
if err != nil {
return nil, resp, err
}
return action, resp, nil
}

func (s *NotificationActionsService) Delete(ctx context.Context, organizationSlug string, actionId string) (*Response, error) {
u := fmt.Sprintf("0/organizations/%v/notifications/actions/%v/", organizationSlug, actionId)
req, err := s.client.NewRequest(http.MethodDelete, u, nil)
if err != nil {
return nil, err
}

return s.client.Do(ctx, req, nil)
}
78 changes: 78 additions & 0 deletions sentry/notification_actions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package sentry

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNotificationActionsService_Create(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/api/0/organizations/organization_slug/notifications/actions/", func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, http.MethodPost, r)
assertPostJSON(t, map[string]interface{}{
"projects": []interface{}{"go"},
"serviceType": "sentry_notification",
"targetDisplay": "default",
"targetIdentifier": "default",
"targetType": "specific",
"triggerType": "spike-protection",
}, r)
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `{
"id": 1,
"organizationId": null,
"integrationId": null,
"sentryAppId": null,
"projects": [
1
],
"serviceType": "sentry_notification",
"triggerType": "spike-protection",
"targetType": "specific",
"targetIdentifier": "default",
"targetDisplay": "default"
}`)
})

params := &CreateNotificationActionParams{
TriggerType: String("spike-protection"),
ServiceType: String("sentry_notification"),
TargetIdentifier: String("default"),
TargetDisplay: String("default"),
TargetType: String("specific"),
Projects: []string{"go"},
}
ctx := context.Background()
action, _, err := client.NotificationActions.Create(ctx, "organization_slug", params)
assert.NoError(t, err)

expected := &NotificationAction{
ID: Int(1),
TriggerType: String("spike-protection"),
ServiceType: String("sentry_notification"),
TargetIdentifier: "default",
TargetDisplay: String("default"),
TargetType: String("specific"),
Projects: []int{1},
}
assert.Equal(t, expected, action)
}

func TestNotificationActionsService_Delete(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/api/0/organizations/organization_slug/notifications/actions/action_id/", func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, http.MethodDelete, r)
})

ctx := context.Background()
_, err := client.NotificationActions.Delete(ctx, "organization_slug", "action_id")
assert.NoError(t, err)
}
2 changes: 2 additions & 0 deletions sentry/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Client struct {
DashboardWidgets *DashboardWidgetsService
IssueAlerts *IssueAlertsService
MetricAlerts *MetricAlertsService
NotificationActions *NotificationActionsService
OrganizationCodeMappings *OrganizationCodeMappingsService
OrganizationIntegrations *OrganizationIntegrationsService
OrganizationMembers *OrganizationMembersService
Expand Down Expand Up @@ -90,6 +91,7 @@ func NewClient(httpClient *http.Client) *Client {
c.DashboardWidgets = (*DashboardWidgetsService)(&c.common)
c.IssueAlerts = (*IssueAlertsService)(&c.common)
c.MetricAlerts = (*MetricAlertsService)(&c.common)
c.NotificationActions = (*NotificationActionsService)(&c.common)
c.OrganizationCodeMappings = (*OrganizationCodeMappingsService)(&c.common)
c.OrganizationIntegrations = (*OrganizationIntegrationsService)(&c.common)
c.OrganizationMembers = (*OrganizationMembersService)(&c.common)
Expand Down

0 comments on commit e3efa78

Please sign in to comment.