Skip to content

Commit

Permalink
ref: improve platform validation (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
jianyuan authored May 2, 2024
1 parent 3a0312e commit 130badf
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 47 deletions.
2 changes: 1 addition & 1 deletion docs/resources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ resource "sentry_project" "default" {
- `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.
- `digests_max_delay` (Number) The maximum amount of time (in seconds) to wait between scheduling digests for delivery.
- `digests_min_delay` (Number) The minimum amount of time (in seconds) to wait between scheduling digests for delivery after the initial scheduling.
- `platform` (String) The optional platform for this project.
- `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.
- `resolve_age` (Number) Hours in which an issue is automatically resolve if not seen after this amount of time.
- `slug` (String) The optional slug for this project.
- `team` (String, Deprecated) The slug of the team to create the project for. **Deprecated** Use `teams` instead.
Expand Down
7 changes: 7 additions & 0 deletions internal/sentryplatforms/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

set -euxo pipefail

curl -sSfL https://raw.githubusercontent.com/getsentry/sentry/master/src/sentry/models/project.py \
| awk '/GETTING_STARTED_DOCS_PLATFORMS = \[/ { platforms = 1; next } /\]/ { platforms = 0 } platforms { gsub(/[ ",]/, ""); print }' \
> platforms.txt
97 changes: 97 additions & 0 deletions internal/sentryplatforms/platforms.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
android
apple
apple-ios
apple-macos
bun
capacitor
cordova
dart
deno
dotnet
dotnet-aspnet
dotnet-aspnetcore
dotnet-awslambda
dotnet-gcpfunctions
dotnet-maui
dotnet-uwp
dotnet-winforms
dotnet-wpf
dotnet-xamarin
electron
elixir
flutter
go
go-echo
go-fasthttp
go-gin
go-http
go-iris
go-martini
go-negroni
ionic
java
java-log4j2
java-logback
java-spring
java-spring-boot
javascript
javascript-angular
javascript-astro
javascript-ember
javascript-gatsby
javascript-nextjs
javascript-react
javascript-remix
javascript-svelte
javascript-sveltekit
javascript-vue
kotlin
minidump
native
native-qt
nintendo-switch
node
node-awslambda
node-azurefunctions
node-connect
node-express
node-fastify
node-gcpfunctions
node-hapi
node-koa
node-nestjs
node-serverlesscloud
php
php-laravel
php-symfony
powershell
python
python-aiohttp
python-asgi
python-awslambda
python-bottle
python-celery
python-chalice
python-django
python-falcon
python-fastapi
python-flask
python-gcpfunctions
python-pylons
python-pymongo
python-pyramid
python-quart
python-rq
python-sanic
python-serverless
python-starlette
python-tornado
python-tryton
python-wsgi
react-native
ruby
ruby-rack
ruby-rails
rust
unity
unreal
28 changes: 28 additions & 0 deletions internal/sentryplatforms/sentryplatforms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package sentryplatforms

import (
_ "embed"
"strings"
)

//go:generate bash ./generate.sh

//go:embed platforms.txt
var rawPlatforms string
var platforms = strings.Split(strings.TrimSpace(rawPlatforms), "\n")

// Validate checks if a platform is valid from the list loaded from platforms.txt.
func Validate(platform string) bool {
// "other" is a special case that is always valid
if platform == "other" {
return true
}

for _, p := range platforms {
if p == platform {
return true
}
}

return false
}
27 changes: 27 additions & 0 deletions internal/sentryplatforms/sentryplatforms_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package sentryplatforms

import (
"fmt"
"testing"
)

func TestValidate(t *testing.T) {
testCases := []struct {
platform string
want bool
}{
{"android", true},
{"python", true},
{"javascript", true},
{"other", true},
{"bogus", false},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("platform=%s", tc.platform), func(t *testing.T) {
got := Validate(tc.platform)
if got != tc.want {
t.Errorf("got %v; want %v", got, tc.want)
}
})
}
}
30 changes: 3 additions & 27 deletions sentry/resource_sentry_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"errors"
"fmt"
"net/http"
"strings"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jianyuan/go-sentry/v2/sentry"
"github.com/jianyuan/terraform-provider-sentry/internal/sentryplatforms"
)

func resourceSentryProject() *schema.Resource {
Expand Down Expand Up @@ -62,7 +62,7 @@ func resourceSentryProject() *schema.Resource {
Computed: true,
},
"platform": {
Description: "The optional platform for this project.",
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.",
Type: schema.TypeString,
Optional: true,
Computed: true,
Expand Down Expand Up @@ -377,34 +377,10 @@ func validatePlatform(i interface{}, path cty.Path) diag.Diagnostics {
var diagnostics diag.Diagnostics

v := i.(string)
if v == "other" {
if sentryplatforms.Validate(v) {
return nil
}

urls := []string{
fmt.Sprintf("https://docs.sentry.io/_platforms/%s.json", v),
fmt.Sprintf(
"https://docs.sentry.io/_platforms/%s.json",
strings.Replace(v, "-", "/", 1),
),
}

for _, url := range urls {
resp, err := http.Get(url)

if err != nil {
msg := "could not validate the platform at this time"
diagnostics = append(diagnostics, diag.Diagnostic{
Severity: diag.Error,
Summary: msg,
Detail: msg,
AttributePath: path,
})
} else if resp.StatusCode == 200 {
return nil
}
}

msg := fmt.Sprintf("%s is not a valid platform", v)
diagnostics = append(diagnostics, diag.Diagnostic{
Severity: diag.Error,
Expand Down
19 changes: 0 additions & 19 deletions sentry/resource_sentry_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,3 @@ resource "sentry_team" "test" {
}
`, teamName)
}

func TestValidatePlatform(t *testing.T) {
for _, tc := range []string{
"javascript-react",
"other",
"python-aiohttp",
"python",
"react-native",
} {
tc := tc
t.Run(tc, func(t *testing.T) {
t.Parallel()
diag := validatePlatform(tc, nil)
if diag.HasError() {
t.Errorf("platform should be valid: %v", tc)
}
})
}
}

0 comments on commit 130badf

Please sign in to comment.