Skip to content

Commit 130badf

Browse files
authored
ref: improve platform validation (#420)
1 parent 3a0312e commit 130badf

File tree

7 files changed

+163
-47
lines changed

7 files changed

+163
-47
lines changed

docs/resources/project.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ resource "sentry_project" "default" {
4242
- `default_rules` (Boolean) Whether to create a default issue alert. Defaults to true where the behavior is to alert the user on every new issue.
4343
- `digests_max_delay` (Number) The maximum amount of time (in seconds) to wait between scheduling digests for delivery.
4444
- `digests_min_delay` (Number) The minimum amount of time (in seconds) to wait between scheduling digests for delivery after the initial scheduling.
45-
- `platform` (String) The optional platform for this project.
45+
- `platform` (String) The platform for this project. For a list of valid values, [see this page](https://github.com/jianyuan/terraform-provider-sentry/blob/main/internal/sentryplatforms/platforms.txt). Use `other` for platforms not listed.
4646
- `resolve_age` (Number) Hours in which an issue is automatically resolve if not seen after this amount of time.
4747
- `slug` (String) The optional slug for this project.
4848
- `team` (String, Deprecated) The slug of the team to create the project for. **Deprecated** Use `teams` instead.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -euxo pipefail
4+
5+
curl -sSfL https://raw.githubusercontent.com/getsentry/sentry/master/src/sentry/models/project.py \
6+
| awk '/GETTING_STARTED_DOCS_PLATFORMS = \[/ { platforms = 1; next } /\]/ { platforms = 0 } platforms { gsub(/[ ",]/, ""); print }' \
7+
> platforms.txt
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
android
2+
apple
3+
apple-ios
4+
apple-macos
5+
bun
6+
capacitor
7+
cordova
8+
dart
9+
deno
10+
dotnet
11+
dotnet-aspnet
12+
dotnet-aspnetcore
13+
dotnet-awslambda
14+
dotnet-gcpfunctions
15+
dotnet-maui
16+
dotnet-uwp
17+
dotnet-winforms
18+
dotnet-wpf
19+
dotnet-xamarin
20+
electron
21+
elixir
22+
flutter
23+
go
24+
go-echo
25+
go-fasthttp
26+
go-gin
27+
go-http
28+
go-iris
29+
go-martini
30+
go-negroni
31+
ionic
32+
java
33+
java-log4j2
34+
java-logback
35+
java-spring
36+
java-spring-boot
37+
javascript
38+
javascript-angular
39+
javascript-astro
40+
javascript-ember
41+
javascript-gatsby
42+
javascript-nextjs
43+
javascript-react
44+
javascript-remix
45+
javascript-svelte
46+
javascript-sveltekit
47+
javascript-vue
48+
kotlin
49+
minidump
50+
native
51+
native-qt
52+
nintendo-switch
53+
node
54+
node-awslambda
55+
node-azurefunctions
56+
node-connect
57+
node-express
58+
node-fastify
59+
node-gcpfunctions
60+
node-hapi
61+
node-koa
62+
node-nestjs
63+
node-serverlesscloud
64+
php
65+
php-laravel
66+
php-symfony
67+
powershell
68+
python
69+
python-aiohttp
70+
python-asgi
71+
python-awslambda
72+
python-bottle
73+
python-celery
74+
python-chalice
75+
python-django
76+
python-falcon
77+
python-fastapi
78+
python-flask
79+
python-gcpfunctions
80+
python-pylons
81+
python-pymongo
82+
python-pyramid
83+
python-quart
84+
python-rq
85+
python-sanic
86+
python-serverless
87+
python-starlette
88+
python-tornado
89+
python-tryton
90+
python-wsgi
91+
react-native
92+
ruby
93+
ruby-rack
94+
ruby-rails
95+
rust
96+
unity
97+
unreal
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sentryplatforms
2+
3+
import (
4+
_ "embed"
5+
"strings"
6+
)
7+
8+
//go:generate bash ./generate.sh
9+
10+
//go:embed platforms.txt
11+
var rawPlatforms string
12+
var platforms = strings.Split(strings.TrimSpace(rawPlatforms), "\n")
13+
14+
// Validate checks if a platform is valid from the list loaded from platforms.txt.
15+
func Validate(platform string) bool {
16+
// "other" is a special case that is always valid
17+
if platform == "other" {
18+
return true
19+
}
20+
21+
for _, p := range platforms {
22+
if p == platform {
23+
return true
24+
}
25+
}
26+
27+
return false
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package sentryplatforms
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestValidate(t *testing.T) {
9+
testCases := []struct {
10+
platform string
11+
want bool
12+
}{
13+
{"android", true},
14+
{"python", true},
15+
{"javascript", true},
16+
{"other", true},
17+
{"bogus", false},
18+
}
19+
for _, tc := range testCases {
20+
t.Run(fmt.Sprintf("platform=%s", tc.platform), func(t *testing.T) {
21+
got := Validate(tc.platform)
22+
if got != tc.want {
23+
t.Errorf("got %v; want %v", got, tc.want)
24+
}
25+
})
26+
}
27+
}

sentry/resource_sentry_project.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8-
"strings"
98

109
"github.com/hashicorp/go-cty/cty"
1110
"github.com/hashicorp/go-multierror"
1211
"github.com/hashicorp/terraform-plugin-log/tflog"
1312
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1413
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1514
"github.com/jianyuan/go-sentry/v2/sentry"
15+
"github.com/jianyuan/terraform-provider-sentry/internal/sentryplatforms"
1616
)
1717

1818
func resourceSentryProject() *schema.Resource {
@@ -62,7 +62,7 @@ func resourceSentryProject() *schema.Resource {
6262
Computed: true,
6363
},
6464
"platform": {
65-
Description: "The optional platform for this project.",
65+
Description: "The platform for this project. For a list of valid values, [see this page](https://github.com/jianyuan/terraform-provider-sentry/blob/main/internal/sentryplatforms/platforms.txt). Use `other` for platforms not listed.",
6666
Type: schema.TypeString,
6767
Optional: true,
6868
Computed: true,
@@ -377,34 +377,10 @@ func validatePlatform(i interface{}, path cty.Path) diag.Diagnostics {
377377
var diagnostics diag.Diagnostics
378378

379379
v := i.(string)
380-
if v == "other" {
380+
if sentryplatforms.Validate(v) {
381381
return nil
382382
}
383383

384-
urls := []string{
385-
fmt.Sprintf("https://docs.sentry.io/_platforms/%s.json", v),
386-
fmt.Sprintf(
387-
"https://docs.sentry.io/_platforms/%s.json",
388-
strings.Replace(v, "-", "/", 1),
389-
),
390-
}
391-
392-
for _, url := range urls {
393-
resp, err := http.Get(url)
394-
395-
if err != nil {
396-
msg := "could not validate the platform at this time"
397-
diagnostics = append(diagnostics, diag.Diagnostic{
398-
Severity: diag.Error,
399-
Summary: msg,
400-
Detail: msg,
401-
AttributePath: path,
402-
})
403-
} else if resp.StatusCode == 200 {
404-
return nil
405-
}
406-
}
407-
408384
msg := fmt.Sprintf("%s is not a valid platform", v)
409385
diagnostics = append(diagnostics, diag.Diagnostic{
410386
Severity: diag.Error,

sentry/resource_sentry_team_test.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,3 @@ resource "sentry_team" "test" {
125125
}
126126
`, teamName)
127127
}
128-
129-
func TestValidatePlatform(t *testing.T) {
130-
for _, tc := range []string{
131-
"javascript-react",
132-
"other",
133-
"python-aiohttp",
134-
"python",
135-
"react-native",
136-
} {
137-
tc := tc
138-
t.Run(tc, func(t *testing.T) {
139-
t.Parallel()
140-
diag := validatePlatform(tc, nil)
141-
if diag.HasError() {
142-
t.Errorf("platform should be valid: %v", tc)
143-
}
144-
})
145-
}
146-
}

0 commit comments

Comments
 (0)