Skip to content

Commit

Permalink
feat: improve python actions (#1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
cugu authored Jul 21, 2024
1 parent 81bfbb2 commit 91429ef
Show file tree
Hide file tree
Showing 55 changed files with 1,138 additions and 580 deletions.
28 changes: 22 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ jobs:
with: { go-version: '1.22' }
- uses: oven-sh/setup-bun@v1

- run: make build-ui
- run: |
bun install
mkdir -p dist
touch dist/index.html
working-directory: ui
- run: make install
- run: make fmt
Expand All @@ -28,15 +32,16 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with: { go-version: '1.22' }
- uses: oven-sh/setup-bun@v1

- run: make build-ui
- run: |
mkdir -p ui/dist
touch ui/dist/index.html
- uses: golangci/golangci-lint-action@v6
with: { version: 'v1.59' }

test:
name: Test
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -46,7 +51,18 @@ jobs:

- run: make build-ui

- run: make test
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with: { go-version: '1.22' }
- uses: oven-sh/setup-bun@v1

- run: |
mkdir -p ui/dist
touch ui/dist/index.html
- run: make test-coverage

Expand Down
21 changes: 1 addition & 20 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,20 @@ linters:
enable-all: true
disable:
# complexity
- cyclop
- gocognit
- gocyclo
- maintidx
- nestif
- funlen

# disable
- bodyclose
- depguard
- dupl
- err113
- execinquery
- exhaustruct
- funlen
- gochecknoglobals
- gochecknoinits
- goconst
- godox
- gomnd
- gomoddirectives
- ireturn
- lll
- makezero
- mnd
- paralleltest
- perfsprint
- prealloc
- tagalign
- tagliatelle
- testpackage
- varnamelen
- wrapcheck
- wsl
linters-settings:
gci:
sections:
Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ build-ui:
dev:
@echo "Running..."
rm -rf catalyst_data
go run . bootstrap
go run . admin create admin@catalyst-soar.com 1234567890
go run . set-feature-flags dev
go run . fake-data
Expand Down
26 changes: 14 additions & 12 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 0 additions & 25 deletions app/bootstrap.go

This file was deleted.

4 changes: 2 additions & 2 deletions app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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
Expand Down
18 changes: 16 additions & 2 deletions app/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -22,20 +24,32 @@ 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)
require.NoError(t, err)

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)
}
12 changes: 6 additions & 6 deletions app/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions app/migrate_internal_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
4 changes: 3 additions & 1 deletion app/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions app/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
21 changes: 21 additions & 0 deletions app/routes_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
Loading

0 comments on commit 91429ef

Please sign in to comment.