Skip to content

Commit

Permalink
feat: project inbound data filters service
Browse files Browse the repository at this point in the history
  • Loading branch information
jianyuan committed Dec 9, 2023
1 parent 0c7dd3e commit 881b50c
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 18 deletions.
42 changes: 42 additions & 0 deletions sentry/project_inbound_data_filters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package sentry

import (
"context"
"net/http"
)

type ProjectInboundDataFilter struct {
ID string `json:"id"`
Active BoolOrStringSlice `json:"active"`
}

type ProjectInboundDataFiltersService service

func (s *ProjectInboundDataFiltersService) List(ctx context.Context, organizationSlug string, projectSlug string) ([]*ProjectInboundDataFilter, *Response, error) {
u := "0/projects/" + organizationSlug + "/" + projectSlug + "/filters/"
req, err := s.client.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}

filters := []*ProjectInboundDataFilter{}
resp, err := s.client.Do(ctx, req, &filters)
if err != nil {
return nil, resp, err
}
return filters, resp, nil
}

type UpdateProjectInboundDataFilterParams struct {
Active *bool `json:"active,omitempty"`
Subfilters []string `json:"subfilters,omitempty"`
}

func (s *ProjectInboundDataFiltersService) Update(ctx context.Context, organizationSlug string, projectSlug string, filterID string, params *UpdateProjectInboundDataFilterParams) (*Response, error) {
u := "0/projects/" + organizationSlug + "/" + projectSlug + "/filters/" + filterID + "/"
req, err := s.client.NewRequest(http.MethodPut, u, params)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
110 changes: 110 additions & 0 deletions sentry/project_inbound_data_filters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package sentry

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

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

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

mux.HandleFunc("/api/0/projects/organization_slug/project_slug/filters/", func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, http.MethodGet, r)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `[
{
"id": "browser-extensions",
"active": false
},
{
"id": "filtered-transaction",
"active": true
},
{
"id": "legacy-browsers",
"active": [
"ie_pre_9"
]
},
{
"id": "localhost",
"active": false
},
{
"id": "web-crawlers",
"active": false
}
]`)
})

ctx := context.Background()
filters, _, err := client.ProjectInboundDataFilters.List(ctx, "organization_slug", "project_slug")
assert.NoError(t, err)

expected := []*ProjectInboundDataFilter{
{
ID: "browser-extensions",
Active: BoolOrStringSlice{IsBool: true, BoolVal: false},
},
{
ID: "filtered-transaction",
Active: BoolOrStringSlice{IsBool: true, BoolVal: true},
},
{
ID: "legacy-browsers",
Active: BoolOrStringSlice{IsBool: false, SliceVal: []string{"ie_pre_9"}},
},
{
ID: "localhost",
Active: BoolOrStringSlice{IsBool: true, BoolVal: false},
},
{
ID: "web-crawlers",
Active: BoolOrStringSlice{IsBool: true, BoolVal: false},
},
}
assert.Equal(t, expected, filters)
}

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

mux.HandleFunc("/api/0/projects/organization_slug/project_slug/filters/filter_id/", func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, http.MethodPut, r)
assertPostJSON(t, map[string]interface{}{
"active": true,
}, r)
})

ctx := context.Background()
params := &UpdateProjectInboundDataFilterParams{
Active: Bool(true),
}
_, err := client.ProjectInboundDataFilters.Update(ctx, "organization_slug", "project_slug", "filter_id", params)
assert.NoError(t, err)
}

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

mux.HandleFunc("/api/0/projects/organization_slug/project_slug/filters/filter_id/", func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, http.MethodPut, r)
assertPostJSON(t, map[string]interface{}{
"subfilters": []interface{}{"ie_pre_9"},
}, r)
})

ctx := context.Background()
params := &UpdateProjectInboundDataFilterParams{
Subfilters: []string{"ie_pre_9"},
}
_, err := client.ProjectInboundDataFilters.Update(ctx, "organization_slug", "project_slug", "filter_id", params)
assert.NoError(t, err)
}
38 changes: 20 additions & 18 deletions sentry/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,25 @@ type Client struct {
common service

// Services
Dashboards *DashboardsService
DashboardWidgets *DashboardWidgetsService
IssueAlerts *IssueAlertsService
MetricAlerts *MetricAlertsService
OrganizationCodeMappings *OrganizationCodeMappingsService
OrganizationIntegrations *OrganizationIntegrationsService
OrganizationMembers *OrganizationMembersService
OrganizationRepositories *OrganizationRepositoriesService
Organizations *OrganizationsService
ProjectFilters *ProjectFiltersService
ProjectKeys *ProjectKeysService
ProjectOwnerships *ProjectOwnershipsService
ProjectPlugins *ProjectPluginsService
Projects *ProjectsService
ReleaseDeployments *ReleaseDeploymentsService
SpikeProtections *SpikeProtectionsService
TeamMembers *TeamMembersService
Teams *TeamsService
Dashboards *DashboardsService
DashboardWidgets *DashboardWidgetsService
IssueAlerts *IssueAlertsService
MetricAlerts *MetricAlertsService
OrganizationCodeMappings *OrganizationCodeMappingsService
OrganizationIntegrations *OrganizationIntegrationsService
OrganizationMembers *OrganizationMembersService
OrganizationRepositories *OrganizationRepositoriesService
Organizations *OrganizationsService
ProjectFilters *ProjectFiltersService
ProjectInboundDataFilters *ProjectInboundDataFiltersService
ProjectKeys *ProjectKeysService
ProjectOwnerships *ProjectOwnershipsService
ProjectPlugins *ProjectPluginsService
Projects *ProjectsService
ReleaseDeployments *ReleaseDeploymentsService
SpikeProtections *SpikeProtectionsService
TeamMembers *TeamMembersService
Teams *TeamsService
}

type service struct {
Expand Down Expand Up @@ -95,6 +96,7 @@ func NewClient(httpClient *http.Client) *Client {
c.OrganizationRepositories = (*OrganizationRepositoriesService)(&c.common)
c.Organizations = (*OrganizationsService)(&c.common)
c.ProjectFilters = (*ProjectFiltersService)(&c.common)
c.ProjectInboundDataFilters = (*ProjectInboundDataFiltersService)(&c.common)
c.ProjectKeys = (*ProjectKeysService)(&c.common)
c.ProjectOwnerships = (*ProjectOwnershipsService)(&c.common)
c.ProjectPlugins = (*ProjectPluginsService)(&c.common)
Expand Down
38 changes: 38 additions & 0 deletions sentry/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package sentry

import (
"encoding/json"
"fmt"
)

// BoolOrStringSlice is a type that can be unmarshaled from either a bool or a
// string slice.
type BoolOrStringSlice struct {
IsBool bool
BoolVal bool
SliceVal []string
}

var _ json.Unmarshaler = (*BoolOrStringSlice)(nil)

// UnmarshalJSON implements json.Unmarshaler.
func (bos *BoolOrStringSlice) UnmarshalJSON(data []byte) error {
// Try to unmarshal as a bool
var boolVal bool
if err := json.Unmarshal(data, &boolVal); err == nil {
bos.IsBool = true
bos.BoolVal = boolVal
return nil
}

// Try to unmarshal as a string slice
var sliceVal []string
if err := json.Unmarshal(data, &sliceVal); err == nil {
bos.IsBool = false
bos.SliceVal = sliceVal
return nil
}

// If neither worked, return an error
return fmt.Errorf("unable to unmarshal as bool or string slice: %s", string(data))
}

0 comments on commit 881b50c

Please sign in to comment.