forked from jianyuan/go-sentry
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_filter.go
105 lines (86 loc) · 3.01 KB
/
project_filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package sentry
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// ProjectFilter represents inbounding filters applied to a project.
type ProjectFilter struct {
ID string `json:"id"`
Active json.RawMessage `json:"active"`
}
// ProjectOwnershipService provides methods for accessing Sentry project
// filters API endpoints.
type ProjectFilterService service
// Get the filters.
func (s *ProjectFilterService) Get(ctx context.Context, organizationSlug string, projectSlug string) ([]*ProjectFilter, *Response, error) {
url := fmt.Sprintf("0/projects/%v/%v/filters/", organizationSlug, projectSlug)
req, err := s.client.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, nil, err
}
var filters []*ProjectFilter
resp, err := s.client.Do(ctx, req, &filters)
if err != nil {
return nil, resp, err
}
return filters, resp, nil
}
// FilterConfig represents configuration for project filter
type FilterConfig struct {
BrowserExtension bool
LegacyBrowsers []string
}
// GetFilterConfig retrieves filter configuration.
func (s *ProjectFilterService) GetFilterConfig(ctx context.Context, organizationSlug string, projectSlug string) (*FilterConfig, *Response, error) {
filters, resp, err := s.Get(ctx, organizationSlug, projectSlug)
if err != nil {
return nil, resp, err
}
var filterConfig FilterConfig
for _, filter := range filters {
switch filter.ID {
case "browser-extensions":
if string(filter.Active) == "true" {
filterConfig.BrowserExtension = true
}
case "legacy-browsers":
if string(filter.Active) != "false" {
err = json.Unmarshal(filter.Active, &filterConfig.LegacyBrowsers)
if err != nil {
return nil, resp, err
}
}
}
}
return &filterConfig, resp, err
}
// BrowserExtensionParams defines parameters for browser extension request
type BrowserExtensionParams struct {
Active bool `json:"active"`
}
// UpdateBrowserExtensions updates configuration for browser extension filter
func (s *ProjectFilterService) UpdateBrowserExtensions(ctx context.Context, organizationSlug string, projectSlug string, active bool) (*Response, error) {
url := fmt.Sprintf("0/projects/%v/%v/filters/browser-extensions/", organizationSlug, projectSlug)
params := BrowserExtensionParams{active}
req, err := s.client.NewRequest(http.MethodPut, url, params)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// LegacyBrowserParams defines parameters for legacy browser request
type LegacyBrowserParams struct {
Browsers []string `json:"subfilters"`
}
// UpdateLegacyBrowser updates configuration for legacy browser filters
func (s *ProjectFilterService) UpdateLegacyBrowser(ctx context.Context, organizationSlug string, projectSlug string, browsers []string) (*Response, error) {
url := fmt.Sprintf("0/projects/%v/%v/filters/legacy-browsers/", organizationSlug, projectSlug)
params := LegacyBrowserParams{browsers}
req, err := s.client.NewRequest(http.MethodPut, url, params)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}