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 11, 2023
1 parent 881b50c commit 2d6456c
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
87 changes: 87 additions & 0 deletions sentry/notification_actions.go
Original file line number Diff line number Diff line change
@@ -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)
}
115 changes: 115 additions & 0 deletions sentry/notification_actions_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
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 2d6456c

Please sign in to comment.