diff --git a/sentry/notification_actions.go b/sentry/notification_actions.go new file mode 100644 index 0000000..ec6e748 --- /dev/null +++ b/sentry/notification_actions.go @@ -0,0 +1,87 @@ +package sentry + +import ( + "context" + "encoding/json" + "fmt" + "net/http" +) + +type NotificationActionsService service + +type CreateNotificationActionParams struct { + TriggerType *string `json:"triggerType"` + ServiceType *string `json:"serviceType"` + IntegrationId json.Number `json:"integrationId,omitempty"` + TargetIdentifier interface{} `json:"targetIdentifier,omitempty"` + TargetDisplay *string `json:"targetDisplay,omitempty"` + TargetType *string `json:"targetType,omitempty"` + Projects []string `json:"projects"` +} + +type NotificationAction struct { + ID json.Number `json:"id"` + OrganizationId *string `json:"organizationId"` + TriggerType *string `json:"triggerType"` + ServiceType *string `json:"serviceType"` + IntegrationId json.Number `json:"integrationId"` + TargetIdentifier interface{} `json:"targetIdentifier"` + TargetDisplay *string `json:"targetDisplay"` + TargetType *string `json:"targetType"` + Projects []int `json:"projects"` +} + +func (s *NotificationActionsService) Get(ctx context.Context, organizationSlug string, actionId string) (*NotificationAction, *Response, error) { + u := fmt.Sprintf("0/organizations/%v/notifications/actions/%v/", organizationSlug, actionId) + req, err := s.client.NewRequest(http.MethodGet, u, nil) + 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) 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) +} diff --git a/sentry/notification_actions_test.go b/sentry/notification_actions_test.go new file mode 100644 index 0000000..5f29b42 --- /dev/null +++ b/sentry/notification_actions_test.go @@ -0,0 +1,115 @@ +package sentry + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNotificationActionsService_Get(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.MethodGet, r) + fmt.Fprintf(w, `{ + "id": "836501735", + "organizationId": "62848264", + "serviceType": "sentry_notification", + "targetDisplay": "default", + "targetIdentifier": "default", + "targetType": "specific", + "triggerType": "spike-protection", + "projects": [ + 4505321021243392 + ] + }`) + }) + + ctx := context.Background() + action, _, err := client.NotificationActions.Get(ctx, "organization_slug", "action_id") + assert.NoError(t, err) + + expected := &NotificationAction{ + ID: json.Number("836501735"), + OrganizationId: String("62848264"), + TriggerType: String("spike-protection"), + ServiceType: String("sentry_notification"), + TargetIdentifier: "default", + TargetDisplay: String("default"), + TargetType: String("specific"), + Projects: []int{4505321021243392}, + } + assert.Equal(t, expected, action) +} + +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": "836501735", + "organizationId": "62848264", + "serviceType": "sentry_notification", + "targetDisplay": "default", + "targetIdentifier": "default", + "targetType": "specific", + "triggerType": "spike-protection", + "projects": [ + 4505321021243392 + ] + }`) + }) + + 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: json.Number("836501735"), + OrganizationId: String("62848264"), + TriggerType: String("spike-protection"), + ServiceType: String("sentry_notification"), + TargetIdentifier: "default", + TargetDisplay: String("default"), + TargetType: String("specific"), + Projects: []int{4505321021243392}, + } + 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) +} diff --git a/sentry/sentry.go b/sentry/sentry.go index 50de5e8..cd5a5fe 100644 --- a/sentry/sentry.go +++ b/sentry/sentry.go @@ -51,6 +51,7 @@ type Client struct { DashboardWidgets *DashboardWidgetsService IssueAlerts *IssueAlertsService MetricAlerts *MetricAlertsService + NotificationActions *NotificationActionsService OrganizationCodeMappings *OrganizationCodeMappingsService OrganizationIntegrations *OrganizationIntegrationsService OrganizationMembers *OrganizationMembersService @@ -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)