Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: reactions #1080

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ on:
release: { types: [ published ] }

jobs:
fmt:
name: Fmt
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: make build-ui

- run: make install
- run: make fmt

- run: git diff --exit-code

lint:
name: Lint
runs-on: ubuntu-latest
Expand All @@ -31,3 +47,10 @@ jobs:
- run: make build-ui

- run: make test

- run: make test-coverage

- uses: codecov/codecov-action@v4
with:
files: ./coverage.out
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/semantic-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ jobs:
with:
scopes: |
deps
subjectPattern: "^(?!deps$).+"
subjectPattern: ^(?![A-Z]).+$
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ dist
pb_data
catalyst
catalyst_data

coverage.out
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ linters:
- nestif

# disable
- bodyclose
- depguard
- dupl
- err113
Expand All @@ -28,6 +29,8 @@ linters:
- lll
- makezero
- mnd
- paralleltest
- perfsprint
- prealloc
- tagalign
- tagliatelle
Expand Down
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.PHONY: install
install:
@echo "Installing..."
go install github.com/bombsimon/wsl/v4/cmd...@master
go install mvdan.cc/gofumpt@latest
go install github.com/daixiang0/gci@latest
go install github.com/bombsimon/wsl/v4/cmd...@v4.4.1
go install mvdan.cc/gofumpt@v0.6.0
go install github.com/daixiang0/gci@v0.13.4

.PHONY: fmt
fmt:
Expand All @@ -26,6 +26,13 @@ test:
go test -v ./...
cd ui && bun test

.PHONY: test-coverage
test-coverage:
@echo "Testing with coverage..."
go test -coverpkg=./... -coverprofile=coverage.out ./...
go tool cover -func=coverage.out
go tool cover -html=coverage.out

.PHONY: build-ui
build-ui:
@echo "Building..."
Expand Down
44 changes: 44 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package app

import (
"os"
"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() {
migrations.Register()
}

func App(dir string) *pocketbase.PocketBase {
app := pocketbase.NewWithConfig(pocketbase.Config{
DefaultDev: dev(),
DefaultDataDir: dir,
})

BindHooks(app)

// Register additional commands
app.RootCmd.AddCommand(bootstrapCmd(app))
app.RootCmd.AddCommand(fakeDataCmd(app))
app.RootCmd.AddCommand(setFeatureFlagsCmd(app))

return app
}

func BindHooks(app core.App) {
webhook.BindHooks(app)
reaction.BindHooks(app)

app.OnBeforeServe().Add(addRoutes())
}

func dev() bool {
return strings.HasPrefix(os.Args[0], os.TempDir())
}
25 changes: 25 additions & 0 deletions app/bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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())
}
},
}
}
27 changes: 27 additions & 0 deletions app/fakedata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package app

import (
"github.com/pocketbase/pocketbase/core"
"github.com/spf13/cobra"

"github.com/SecurityBrewery/catalyst/fakedata"
)

func fakeDataCmd(app core.App) *cobra.Command {
var userCount, ticketCount int

cmd := &cobra.Command{
Use: "fake-data",
Run: func(_ *cobra.Command, _ []string) {
if err := fakedata.Generate(app, userCount, ticketCount); err != nil {
app.Logger().Error(err.Error())
}
},
}

cmd.PersistentFlags().IntVar(&userCount, "users", 10, "Number of users to generate")

cmd.PersistentFlags().IntVar(&ticketCount, "tickets", 100, "Number of tickets to generate")

return cmd
}
80 changes: 80 additions & 0 deletions app/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package app

import (
"slices"

"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/models"
"github.com/spf13/cobra"

"github.com/SecurityBrewery/catalyst/migrations"
)

func Flags(app core.App) ([]string, error) {
records, err := app.Dao().FindRecordsByExpr(migrations.FeatureCollectionName)
if err != nil {
return nil, err
}

var flags []string

for _, r := range records {
flags = append(flags, r.GetString("name"))
}

return flags, nil
}

func SetFlags(app core.App, args []string) error {
featureCollection, err := app.Dao().FindCollectionByNameOrId(migrations.FeatureCollectionName)
if err != nil {
return err
}

featureRecords, err := app.Dao().FindRecordsByExpr(migrations.FeatureCollectionName)
if err != nil {
return err
}

var existingFlags []string

for _, featureRecord := range featureRecords {
// remove feature flags that are not in the args
if !slices.Contains(args, featureRecord.GetString("name")) {
if err := app.Dao().DeleteRecord(featureRecord); err != nil {
return err
}

continue
}

existingFlags = append(existingFlags, featureRecord.GetString("name"))
}

for _, arg := range args {
if slices.Contains(existingFlags, arg) {
continue
}

// add feature flags that are not in the args
record := models.NewRecord(featureCollection)
record.Set("name", arg)

if err := app.Dao().SaveRecord(record); err != nil {
return err
}
}

return nil
}

func setFeatureFlagsCmd(app core.App) *cobra.Command {
return &cobra.Command{
Use: "set-feature-flags",
Run: func(_ *cobra.Command, args []string) {
if err := SetFlags(app, args); err != nil {
app.Logger().Error(err.Error())
}
},
}
}
41 changes: 41 additions & 0 deletions app/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package app_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/SecurityBrewery/catalyst/app"
catalystTesting "github.com/SecurityBrewery/catalyst/testing"
)

func Test_flags(t *testing.T) {
catalystApp, cleanup := catalystTesting.App(t)
defer cleanup()

got, err := app.Flags(catalystApp)
require.NoError(t, err)

want := []string{}
assert.ElementsMatch(t, want, got)
}

func Test_setFlags(t *testing.T) {
catalystApp, cleanup := catalystTesting.App(t)
defer cleanup()

require.NoError(t, app.SetFlags(catalystApp, []string{"test"}))

got, err := app.Flags(catalystApp)
require.NoError(t, err)

assert.ElementsMatch(t, []string{"test"}, got)

require.NoError(t, app.SetFlags(catalystApp, []string{"test2"}))

got, err = app.Flags(catalystApp)
require.NoError(t, err)

assert.ElementsMatch(t, []string{"test2"}, got)
}
72 changes: 72 additions & 0 deletions app/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package app

import (
"strings"

"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/migrations/logs"
"github.com/pocketbase/pocketbase/tools/migrate"
)

type migration struct {
db *dbx.DB
migrations migrate.MigrationsList
}

func MigrateDBs(app core.App) error {
for _, m := range []migration{
{db: app.DB(), migrations: migrations.AppMigrations},
{db: app.LogsDB(), migrations: logs.LogsMigrations},
} {
runner, err := migrate.NewRunner(m.db, m.migrations)
if err != nil {
return err
}

if _, err := runner.Up(); err != nil {
return err
}
}

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 {
for _, ignore := range ignoreErrors {
if strings.Contains(err.Error(), ignore) {
return true
}
}

return false
}

func MigrateDBsDown(app core.App) error {
for _, m := range []migration{
{db: app.DB(), migrations: migrations.AppMigrations},
{db: app.LogsDB(), migrations: logs.LogsMigrations},
} {
runner, err := migrate.NewRunner(m.db, m.migrations)
if err != nil {
return err
}

if _, err := runner.Down(len(m.migrations.Items())); err != nil {
if isIgnored(err) {
continue
}

return err
}
}

return nil
}
Loading
Loading