Skip to content

Commit 59e1cf6

Browse files
committed
Added project plugin APIs
1 parent 43d8827 commit 59e1cf6

File tree

6 files changed

+157
-4
lines changed

6 files changed

+157
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor/
22
coverage.out
3+
.env

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/go-errors/errors v1.1.1 // indirect
88
github.com/google/go-cmp v0.5.4 // indirect
99
github.com/pkg/errors v0.9.1 // indirect
10+
github.com/stretchr/testify v1.7.0
1011
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
1112
)
1213

go.sum

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8Nz
1313
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
1414
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
1515
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
16+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1617
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1718
github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4=
1819
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
@@ -29,7 +30,6 @@ github.com/getsentry/sentry-go v0.9.0/go.mod h1:kELm/9iCblqUYh+ZRML7PNdCvEuw24wB
2930
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
3031
github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
3132
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
32-
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
3333
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
3434
github.com/go-errors/errors v1.1.1 h1:ljK/pL5ltg3qoN+OtN6yCv9HWSfMwxSx90GJCZQxYNg=
3535
github.com/go-errors/errors v1.1.1/go.mod h1:psDX2osz5VnTOnFWbDeWwS7yejl+uV3FEWEp4lssFEs=
@@ -104,6 +104,7 @@ github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTw
104104
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
105105
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
106106
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
107+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
107108
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
108109
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
109110
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
@@ -122,6 +123,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
122123
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
123124
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
124125
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
126+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
127+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
125128
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
126129
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
127130
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
@@ -176,10 +179,10 @@ golang.org/x/tools v0.0.0-20181221001348-537d06c36207/go.mod h1:n7NCudcB/nEzxVGm
176179
golang.org/x/tools v0.0.0-20190327201419-c70d86f8b7cf/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
177180
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
178181
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
179-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
180182
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
181183
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
182184
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
185+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
183186
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
184187
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
185188
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
@@ -190,3 +193,5 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
190193
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
191194
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
192195
gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
196+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
197+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

plugin.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package sentry
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func (c *Client) touchPlugin(o Organization, p Project, pluginID string, enabled bool) error {
9+
method := http.MethodDelete
10+
if enabled {
11+
method = http.MethodPost
12+
}
13+
14+
return c.do(method, fmt.Sprintf("projects/%s/%s/plugins/%s/", *o.Slug, *p.Slug, pluginID), nil, nil)
15+
}
16+
17+
func (c *Client) EnablePlugin(o Organization, p Project, pluginID string) error {
18+
return c.touchPlugin(o, p, pluginID, true)
19+
}
20+
21+
func (c *Client) DisablePlugin(o Organization, p Project, pluginID string) error {
22+
return c.touchPlugin(o, p, pluginID, false)
23+
}
24+
25+
func (c *Client) GetPlugin(o Organization, p Project, pluginID string) (plugin Plugin, err error) {
26+
err = c.do(http.MethodGet, fmt.Sprintf("projects/%s/%s/plugins/%s/", *o.Slug, *p.Slug, pluginID), &plugin, nil)
27+
28+
return
29+
}
30+
31+
func (c *Client) SetPluginConfig(o Organization, p Project, pluginID string, config map[string]interface{}) (plugin Plugin, err error) {
32+
err = c.do(http.MethodPut, fmt.Sprintf("projects/%s/%s/plugins/%s/", *o.Slug, *p.Slug, pluginID), &plugin, config)
33+
34+
return
35+
}

plugin_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package sentry
2+
3+
import (
4+
"github.com/stretchr/testify/require"
5+
"testing"
6+
)
7+
8+
const testPlugin = "webhooks"
9+
10+
func pluginEnabled(t *testing.T, client *Client, org Organization, project Project, pluginID string) bool {
11+
project, err := client.GetProject(org, *project.Slug)
12+
require.NoError(t, err, "failed to get project details")
13+
14+
require.NotNil(t, project.Plugins, "project details missing plugins section")
15+
require.Greater(t, len(*project.Plugins), 0, "no plugins installed")
16+
17+
for _, p := range *project.Plugins {
18+
if p.ID == pluginID {
19+
return p.Enabled
20+
}
21+
}
22+
require.Fail(t, "plugin not found")
23+
24+
return false
25+
}
26+
27+
func TestClient_EnablePlugin(t *testing.T) {
28+
client := newTestClient(t)
29+
org, err := client.GetOrganization(getDefaultOrg())
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
34+
team, cleanup := createTeamHelper(t)
35+
defer cleanup()
36+
37+
project, cleanupproj := createProjectHelper(t, team)
38+
defer cleanupproj()
39+
40+
require.False(t, pluginEnabled(t, client, org, project, testPlugin), "plugin already enabled")
41+
42+
err = client.EnablePlugin(org, project, testPlugin)
43+
require.NoError(t, err, "unable to enable plugin")
44+
45+
require.True(t, pluginEnabled(t, client, org, project, testPlugin), "plugin wasn't enabled")
46+
47+
err = client.EnablePlugin(org, project, testPlugin)
48+
require.NoError(t, err, "unable to enable plugin")
49+
50+
require.True(t, pluginEnabled(t, client, org, project, testPlugin), "plugin wasn't enabled")
51+
52+
err = client.DisablePlugin(org, project, testPlugin)
53+
require.NoError(t, err, "unable to disable plugin")
54+
55+
require.False(t, pluginEnabled(t, client, org, project, testPlugin), "plugin wasn't disabled")
56+
}
57+
58+
func TestClient_GetPlugin(t *testing.T) {
59+
client := newTestClient(t)
60+
org, err := client.GetOrganization(getDefaultOrg())
61+
if err != nil {
62+
t.Fatal(err)
63+
}
64+
65+
team, cleanup := createTeamHelper(t)
66+
defer cleanup()
67+
68+
project, cleanupproj := createProjectHelper(t, team)
69+
defer cleanupproj()
70+
71+
plugin, err := client.GetPlugin(org, project, testPlugin)
72+
require.NoError(t, err, "unable to get plugin")
73+
74+
require.Greater(t, len(plugin.Config), 0, "plugin config is missing")
75+
}
76+
77+
func TestClient_SetPluginConfig(t *testing.T) {
78+
client := newTestClient(t)
79+
org, err := client.GetOrganization(getDefaultOrg())
80+
if err != nil {
81+
t.Fatal(err)
82+
}
83+
84+
team, cleanup := createTeamHelper(t)
85+
defer cleanup()
86+
87+
project, cleanupproj := createProjectHelper(t, team)
88+
defer cleanupproj()
89+
90+
plugin, err := client.SetPluginConfig(org, project, testPlugin, map[string]interface{}{
91+
"urls": "https://test.com/",
92+
})
93+
require.NoError(t, err, "unable to get plugin")
94+
require.NotEmpty(t, plugin.Config, "unable to get plugin config")
95+
require.Equal(t, "https://test.com/", plugin.Config[0].Value)
96+
}

project.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,26 @@ import (
55
"time"
66
)
77

8-
// Asset is used from a plugin. Things like js/html
8+
// Asset is used from a testPlugin. Things like js/html
99
type Asset struct {
1010
URL string `json:"url,omitempty"`
1111
}
1212

13-
// Plugin is a type of project plugin
13+
// PluginConfigItem describes testPlugin option
14+
type PluginConfigItem struct {
15+
ReadOnly bool `json:"readonly,omitempty"`
16+
Choices []interface{} `json:"choices,omitempty"`
17+
Placeholder string `json:"placeholder,omitempty"`
18+
Name string `json:"name,omitempty"`
19+
Help string `json:"help,omitempty"`
20+
DefaultValue interface{} `json:"defaultValue,omitempty"`
21+
Required bool `json:"required,omitempty"`
22+
Type string `json:"type,omitempty"`
23+
Value interface{} `json:"value,omitempty"`
24+
Label string `json:"label,omitempty"`
25+
}
26+
27+
// Plugin is a type of project testPlugin
1428
type Plugin struct {
1529
Assets []Asset `json:"assets,omitempty"`
1630
IsTestable bool `json:"isTestable,omitempty"`
@@ -20,6 +34,7 @@ type Plugin struct {
2034
Type string `json:"type,omitempty"`
2135
ID string `json:"id,omitempty"`
2236
Metadata map[string]interface{} `json:"metadata,omitempty"`
37+
Config []PluginConfigItem `json:"config,omitempty"`
2338
}
2439

2540
// Project is your project in sentry

0 commit comments

Comments
 (0)