feat: improve python actions (#1083)

This commit is contained in:
Jonas Plum
2024-07-21 02:56:43 +02:00
committed by GitHub
parent 81bfbb2072
commit 91429effe2
55 changed files with 1143 additions and 585 deletions

View File

@@ -5,38 +5,40 @@ import (
"strings"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
"github.com/SecurityBrewery/catalyst/migrations"
"github.com/SecurityBrewery/catalyst/reaction"
"github.com/SecurityBrewery/catalyst/webhook"
)
func init() {
func init() { //nolint:gochecknoinits
migrations.Register()
}
func App(dir string) *pocketbase.PocketBase {
func App(dir string, test bool) (*pocketbase.PocketBase, error) {
app := pocketbase.NewWithConfig(pocketbase.Config{
DefaultDev: dev(),
DefaultDev: test || dev(),
DefaultDataDir: dir,
})
BindHooks(app)
webhook.BindHooks(app)
reaction.BindHooks(app, test)
app.OnBeforeServe().Add(addRoutes())
// Register additional commands
app.RootCmd.AddCommand(bootstrapCmd(app))
app.RootCmd.AddCommand(fakeDataCmd(app))
app.RootCmd.AddCommand(setFeatureFlagsCmd(app))
return app
}
if err := app.Bootstrap(); err != nil {
return nil, err
}
func BindHooks(app core.App) {
webhook.BindHooks(app)
reaction.BindHooks(app)
if err := MigrateDBs(app); err != nil {
return nil, err
}
app.OnBeforeServe().Add(addRoutes())
return app, nil
}
func dev() bool {

View File

@@ -1,25 +0,0 @@
package app
import (
"github.com/pocketbase/pocketbase/core"
"github.com/spf13/cobra"
)
func Bootstrap(app core.App) error {
if err := app.Bootstrap(); err != nil {
return err
}
return MigrateDBs(app)
}
func bootstrapCmd(app core.App) *cobra.Command {
return &cobra.Command{
Use: "bootstrap",
Run: func(_ *cobra.Command, _ []string) {
if err := Bootstrap(app); err != nil {
app.Logger().Error(err.Error())
}
},
}
}

View File

@@ -16,7 +16,7 @@ func Flags(app core.App) ([]string, error) {
return nil, err
}
var flags []string
flags := make([]string, 0, len(records))
for _, r := range records {
flags = append(flags, r.GetString("name"))
@@ -36,7 +36,7 @@ func SetFlags(app core.App, args []string) error {
return err
}
var existingFlags []string
var existingFlags []string //nolint:prealloc
for _, featureRecord := range featureRecords {
// remove feature flags that are not in the args

View File

@@ -11,7 +11,9 @@ import (
)
func Test_flags(t *testing.T) {
catalystApp, cleanup := catalystTesting.App(t)
t.Parallel()
catalystApp, _, cleanup := catalystTesting.App(t)
defer cleanup()
got, err := app.Flags(catalystApp)
@@ -22,9 +24,12 @@ func Test_flags(t *testing.T) {
}
func Test_setFlags(t *testing.T) {
catalystApp, cleanup := catalystTesting.App(t)
t.Parallel()
catalystApp, _, cleanup := catalystTesting.App(t)
defer cleanup()
// stage 1
require.NoError(t, app.SetFlags(catalystApp, []string{"test"}))
got, err := app.Flags(catalystApp)
@@ -32,10 +37,19 @@ func Test_setFlags(t *testing.T) {
assert.ElementsMatch(t, []string{"test"}, got)
// stage 2
require.NoError(t, app.SetFlags(catalystApp, []string{"test2"}))
got, err = app.Flags(catalystApp)
require.NoError(t, err)
assert.ElementsMatch(t, []string{"test2"}, got)
// stage 3
require.NoError(t, app.SetFlags(catalystApp, []string{"test", "test2"}))
got, err = app.Flags(catalystApp)
require.NoError(t, err)
assert.ElementsMatch(t, []string{"test", "test2"}, got)
}

View File

@@ -33,13 +33,13 @@ func MigrateDBs(app core.App) error {
return nil
}
// this fix ignores some errors that come from upstream migrations.
var ignoreErrors = []string{
"1673167670_multi_match_migrate",
"1660821103_add_user_ip_column",
}
func isIgnored(err error) bool {
// this fix ignores some errors that come from upstream migrations.
ignoreErrors := []string{
"1673167670_multi_match_migrate",
"1660821103_add_user_ip_column",
}
for _, ignore := range ignoreErrors {
if strings.Contains(err.Error(), ignore) {
return true

View File

@@ -0,0 +1,39 @@
package app
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_isIgnored(t *testing.T) {
t.Parallel()
type args struct {
err error
}
tests := []struct {
name string
args args
want bool
}{
{
name: "error is ignored",
args: args{err: errors.New("1673167670_multi_match_migrate")},
want: true,
},
{
name: "error is not ignored",
args: args{err: errors.New("1673167670_multi_match")},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equalf(t, tt.want, isIgnored(tt.args.err), "isIgnored(%v)", tt.args.err)
})
}
}

View File

@@ -11,7 +11,9 @@ import (
)
func Test_MigrateDBsDown(t *testing.T) {
catalystApp, cleanup := catalystTesting.App(t)
t.Parallel()
catalystApp, _, cleanup := catalystTesting.App(t)
defer cleanup()
_, err := catalystApp.Dao().FindCollectionByNameOrId(migrations.ReactionCollectionName)

View File

@@ -38,11 +38,10 @@ func staticFiles() func(echo.Context) error {
return func(c echo.Context) error {
if dev() {
u, _ := url.Parse("http://localhost:3000/")
proxy := httputil.NewSingleHostReverseProxy(u)
c.Request().Host = c.Request().URL.Host
proxy.ServeHTTP(c.Response(), c.Request())
httputil.NewSingleHostReverseProxy(u).ServeHTTP(c.Response(), c.Request())
return nil
}

21
app/routes_test.go Normal file
View File

@@ -0,0 +1,21 @@
package app
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo/v5"
"github.com/stretchr/testify/require"
)
func Test_staticFiles(t *testing.T) {
t.Parallel()
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
require.NoError(t, staticFiles()(c))
}