Skip to content

Commit 56022f7

Browse files
committed
Added alert rule APIs
1 parent 59e1cf6 commit 56022f7

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed

alert_rule.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package sentry
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"time"
7+
)
8+
9+
type alertRuleMatchPolicy string
10+
11+
var (
12+
AlertRuleMatchAll = alertRuleMatchPolicy("all")
13+
AlertRuleMatchAny = alertRuleMatchPolicy("any")
14+
AlertRuleMatchNone = alertRuleMatchPolicy("none")
15+
)
16+
17+
// AlertRuleCondition represents alert rule condition.
18+
// Refer to https://github.com/getsentry/sentry/tree/master/src/sentry/rules/conditions or GUI
19+
// to get detailed information.
20+
type AlertRuleCondition struct {
21+
ID string `json:"id,omitempty"`
22+
Name string `json:"name,omitempty"`
23+
Interval string `json:"interval,omitempty"` // 1m, 1w, 30d etc
24+
Value interface{} `json:"value,omitempty"`
25+
Attribute string `json:"attribute,omitempty"`
26+
Key string `json:"key,omitempty"`
27+
}
28+
29+
// AlertRuleAction represents alert rule action.
30+
// Refer to https://github.com/getsentry/sentry/tree/master/src/sentry/rules/actions or GUI
31+
// to get detailed information.
32+
type AlertRuleAction struct {
33+
ID string `json:"id,omitempty"`
34+
Name string `json:"name,omitempty"`
35+
Service string `json:"service,omitempty"`
36+
}
37+
38+
type AlertRule struct {
39+
ID string `json:"id,omitempty"`
40+
Name string `json:"name,omitempty"`
41+
DateCreated *time.Time `json:"dateCreated,omitempty"`
42+
Environment *string `json:"environment,omitempty"`
43+
ActionMatch alertRuleMatchPolicy `json:"actionMatch,omitempty"`
44+
Frequency uint `json:"frequency,omitempty"` // run actions at most once every Frequency minutes
45+
Conditions []AlertRuleCondition `json:"conditions,omitempty"`
46+
Actions []AlertRuleAction `json:"actions,omitempty"`
47+
}
48+
49+
func (c *Client) GetAlertRules(o Organization, p Project) ([]AlertRule, *Link, error) {
50+
var rules []AlertRule
51+
link, err := c.doWithPagination(http.MethodGet, fmt.Sprintf("projects/%s/%s/rules/", *o.Slug, *p.Slug), &rules, nil)
52+
53+
return rules, link, err
54+
}
55+
56+
func (c *Client) AddAlertRule(o Organization, p Project, r AlertRule) (AlertRule, error) {
57+
err := c.do(http.MethodPost, fmt.Sprintf("projects/%s/%s/rules/", *o.Slug, *p.Slug), &r, r)
58+
59+
return r, err
60+
}
61+
62+
func (c *Client) UpdateAlertRule(o Organization, p Project, r AlertRule) (AlertRule, error) {
63+
err := c.do(http.MethodPut, fmt.Sprintf("projects/%s/%s/rules/%s/", *o.Slug, *p.Slug, r.ID), &r, r)
64+
65+
return r, err
66+
}
67+
68+
func (c *Client) DeleteAlertRule(o Organization, p Project, r AlertRule) error {
69+
return c.do(http.MethodDelete, fmt.Sprintf("projects/%s/%s/rules/%s/", *o.Slug, *p.Slug, r.ID), nil, nil)
70+
}

alert_rule_test.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package sentry
2+
3+
import (
4+
"github.com/stretchr/testify/require"
5+
"testing"
6+
)
7+
8+
func TestClient_GetAlertRules(t *testing.T) {
9+
client := newTestClient(t)
10+
org, err := client.GetOrganization(getDefaultOrg())
11+
if err != nil {
12+
t.Fatal(err)
13+
}
14+
15+
team, cleanup := createTeamHelper(t)
16+
defer cleanup()
17+
18+
project, cleanupproj := createProjectHelper(t, team)
19+
defer cleanupproj()
20+
21+
rules, _, err := client.GetAlertRules(org, project)
22+
require.NoError(t, err, "unable to get alert rules")
23+
24+
require.Greater(t, len(rules), 0, "no alert rules defined")
25+
require.Greater(t, len(rules[0].Conditions), 0, "no conditions for rule")
26+
require.Greater(t, len(rules[0].Actions), 0, "no actions for rule")
27+
}
28+
29+
func TestClient_AddAlertRule(t *testing.T) {
30+
client := newTestClient(t)
31+
org, err := client.GetOrganization(getDefaultOrg())
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
36+
team, cleanup := createTeamHelper(t)
37+
defer cleanup()
38+
39+
project, cleanupproj := createProjectHelper(t, team)
40+
defer cleanupproj()
41+
42+
rule, err := client.AddAlertRule(org, project, AlertRule{
43+
Name: "Test alert rule",
44+
ActionMatch: AlertRuleMatchAll,
45+
Frequency: 30,
46+
Conditions: []AlertRuleCondition{
47+
{ID: "sentry.rules.conditions.regression_event.RegressionEventCondition"},
48+
},
49+
Actions: []AlertRuleAction{
50+
{ID: "sentry.rules.actions.notify_event_service.NotifyEventServiceAction", Service: "mail"},
51+
},
52+
})
53+
require.NoError(t, err, "unable to create rule")
54+
require.NotEqual(t, 0, rule.ID, "missing rule ID")
55+
require.Equal(t, 1, len(rule.Conditions), "wrong condition count")
56+
require.Equal(t, 1, len(rule.Actions), "wrong action count")
57+
58+
rules, _, err := client.GetAlertRules(org, project)
59+
require.NoError(t, err, "unable to get alert rules")
60+
require.Equal(t, len(rules), 2, "rule wasn't created")
61+
for _, r := range rules {
62+
if r.ID == rule.ID {
63+
require.Equal(t, rule, r, "rules do not match")
64+
break
65+
}
66+
}
67+
}
68+
69+
func TestClient_UpdateAlertRule(t *testing.T) {
70+
client := newTestClient(t)
71+
org, err := client.GetOrganization(getDefaultOrg())
72+
if err != nil {
73+
t.Fatal(err)
74+
}
75+
76+
team, cleanup := createTeamHelper(t)
77+
defer cleanup()
78+
79+
project, cleanupproj := createProjectHelper(t, team)
80+
defer cleanupproj()
81+
82+
rules, _, err := client.GetAlertRules(org, project)
83+
require.NoError(t, err, "unable to get alert rules")
84+
85+
require.Greater(t, len(rules), 0, "no alert rules defined")
86+
87+
rule := rules[0]
88+
require.Equal(t, 1, len(rule.Conditions), "unexpected condition count for rule")
89+
90+
rule.Conditions = append(
91+
rule.Conditions,
92+
AlertRuleCondition{ID: "sentry.rules.conditions.regression_event.RegressionEventCondition"},
93+
)
94+
95+
rule, err = client.UpdateAlertRule(org, project, rule)
96+
require.NoError(t, err, "unable to get update rule")
97+
require.Equal(t, 2, len(rule.Conditions), "conditions weren't updated")
98+
}
99+
100+
func TestClient_DeleteAlertRule(t *testing.T) {
101+
client := newTestClient(t)
102+
org, err := client.GetOrganization(getDefaultOrg())
103+
if err != nil {
104+
t.Fatal(err)
105+
}
106+
107+
team, cleanup := createTeamHelper(t)
108+
defer cleanup()
109+
110+
project, cleanupproj := createProjectHelper(t, team)
111+
defer cleanupproj()
112+
113+
rules, _, err := client.GetAlertRules(org, project)
114+
require.NoError(t, err, "unable to get alert rules")
115+
116+
require.Greater(t, len(rules), 0, "no alert rules defined")
117+
118+
err = client.DeleteAlertRule(org, project, rules[0])
119+
require.NoError(t, err, "unable to get delete rule")
120+
121+
rules, _, err = client.GetAlertRules(org, project)
122+
require.NoError(t, err, "unable to get alert rules")
123+
require.Equal(t, 0, len(rules), "rule wasn't deleted")
124+
125+
}

0 commit comments

Comments
 (0)