From 5be85986277c0cf7c641036cc1ae0ddac9151750 Mon Sep 17 00:00:00 2001 From: daniel <40469266+daniel-burghardt@users.noreply.github.com> Date: Thu, 9 May 2024 16:01:53 -0300 Subject: [PATCH] git actions: go workflow (#8) What Adds the Github Workflow running three steps: a linter, a builder and the unit tests. Why To ensure code quality. --- .github/workflows/go.yaml | 96 +++++++++++++++++++ cmd/root.go | 7 +- go.mod | 2 +- go.sum | 12 +-- gomod.sh | 7 ++ internal/data/payments_test.go | 6 +- internal/db/db.go | 6 +- internal/db/db_test.go | 6 +- .../httphandler/payments_handler_test.go | 12 +-- 9 files changed, 130 insertions(+), 24 deletions(-) create mode 100644 .github/workflows/go.yaml create mode 100755 gomod.sh diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml new file mode 100644 index 0000000..eaa294c --- /dev/null +++ b/.github/workflows/go.yaml @@ -0,0 +1,96 @@ +name: Go + +on: + push: + branches: [main] + pull_request: + branches: ["**"] + +jobs: + check: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.22" + + - name: golangci-lint + uses: golangci/golangci-lint-action@v4 + with: + version: v1.58 + args: --timeout=5m + + - name: ./gomod.sh + run: ./gomod.sh + + - name: exhaustive github.com/nishanths/exhaustive@v0.12.0 + run: | + go install github.com/nishanths/exhaustive/cmd/exhaustive@v0.12.0 + exhaustive -default-signifies-exhaustive ./... + + - name: deadcode golang.org/x/tools/cmd/deadcode@v0.21.0 + run: | + go install golang.org/x/tools/cmd/deadcode@v0.21.0 + output=$(deadcode -test ./...) + if [[ -n "$output" ]]; then + echo "🚨 Deadcode found:" + echo "$output" + exit 1 + else + echo "✅ No deadcode found" + fi + + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.22" + + - name: Build Project + run: go build ./... + + test: + runs-on: ubuntu-latest + services: + postgres: + image: postgres:16-alpine + env: + POSTGRES_USER: postgres + POSTGRES_DB: postgres + POSTGRES_PASSWORD: postgres + PGHOST: localhost + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 + env: + PGHOST: localhost + PGPORT: 5432 + PGUSER: postgres + PGPASSWORD: postgres + PGDATABASE: postgres + DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.22.1" + + - name: Run Tests + run: go test -v -race -cover ./... diff --git a/cmd/root.go b/cmd/root.go index 4644db1..7a10ffc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -13,7 +13,10 @@ var rootCmd = &cobra.Command{ Use: "wallet-backend", Short: "Wallet Backend Server", Run: func(cmd *cobra.Command, args []string) { - cmd.Help() + err := cmd.Help() + if err != nil { + log.Fatalf("Error calling help command: %s", err.Error()) + } }, } @@ -22,7 +25,7 @@ var rootCmd = &cobra.Command{ func Execute() { err := rootCmd.Execute() if err != nil { - log.Fatal(err) + log.Fatalf("Error executing root command: %s", err.Error()) } } diff --git a/go.mod b/go.mod index f7381f8..57649fb 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/rubenv/sql-migrate v1.6.1 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.8.0 - github.com/stellar/go v0.0.0-20240417192451-c63ad05d3e3b + github.com/stellar/go v0.0.0-20231212225359-bc7173e667a6 github.com/stretchr/testify v1.9.0 ) diff --git a/go.sum b/go.sum index bc3d13b..ca35e18 100644 --- a/go.sum +++ b/go.sum @@ -130,8 +130,8 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-querystring v0.0.0-20160401233042-9235644dd9e5 h1:oERTZ1buOUYlpmKaqlO5fYmz8cZ1rYu5DieJzF4ZVmU= github.com/google/go-querystring v0.0.0-20160401233042-9235644dd9e5/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= @@ -256,8 +256,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.17.0 h1:I5txKw7MJasPL/BrfkbA0Jyo/oELqVmux4pR/UxOMfI= github.com/spf13/viper v1.17.0/go.mod h1:BmMMMLQXSbcHK6KAOiFLz0l5JHrU89OdIRHvsk0+yVI= -github.com/stellar/go v0.0.0-20240417192451-c63ad05d3e3b h1:oKqqeJBLfSe5NkFjYb1vacvxbw0NQr+j14NYVWvUkiA= -github.com/stellar/go v0.0.0-20240417192451-c63ad05d3e3b/go.mod h1:ckzsX0B0qfTMVZQJtPELJLs7cJ6xXMYHPVLyIsReGsU= +github.com/stellar/go v0.0.0-20231212225359-bc7173e667a6 h1:LcQ01nwgxVoCmzAthjGSbxun9z/mxuqDy4uYETw4jEQ= +github.com/stellar/go v0.0.0-20231212225359-bc7173e667a6/go.mod h1:PAWie4LYyDzJXqDVG4Qcj1Nt+uNk7sjzgSCXndQYsBA= github.com/stellar/go-xdr v0.0.0-20231122183749-b53fb00bcac2 h1:OzCVd0SV5qE3ZcDeSFCmOWLZfEWZ3Oe8KtmSOYKEVWE= github.com/stellar/go-xdr v0.0.0-20231122183749-b53fb00bcac2/go.mod h1:yoxyU/M8nl9LKeWIoBrbDPQ7Cy+4jxRcWcOayZ4BMps= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -380,8 +380,8 @@ golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= -golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= +golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= diff --git a/gomod.sh b/gomod.sh new file mode 100755 index 0000000..f90f958 --- /dev/null +++ b/gomod.sh @@ -0,0 +1,7 @@ +#! /bin/bash +set -e + +go mod tidy +git diff --exit-code -- go.mod || (echo "Go file go.mod is dirty, update the file with 'go mod tidy' locally." && exit 1) +git diff --exit-code -- go.sum || (echo "Go file go.sum is dirty, update the file with 'go mod tidy' locally." && exit 1) +go mod verify || (echo "One or more Go dependencies failed verification. Either a version is no longer available, or the author or someone else has modified the version so it no longer points to the same code." && exit 1) \ No newline at end of file diff --git a/internal/data/payments_test.go b/internal/data/payments_test.go index 119fe1d..cf6c683 100644 --- a/internal/data/payments_test.go +++ b/internal/data/payments_test.go @@ -13,10 +13,10 @@ import ( ) func TestSubscribeAddress(t *testing.T) { - dbtest := dbtest.Open(t) - defer dbtest.Close() + dbt := dbtest.Open(t) + defer dbt.Close() - dbConnectionPool, err := db.OpenDBConnectionPool(dbtest.DSN) + dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN) require.NoError(t, err) defer dbConnectionPool.Close() diff --git a/internal/db/db.go b/internal/db/db.go index a38ba38..5806bac 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -19,13 +19,13 @@ type ConnectionPool interface { SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error } +// Make sure *DBConnectionPoolImplementation implements DBConnectionPool: +var _ ConnectionPool = (*DBConnectionPoolImplementation)(nil) + type DBConnectionPoolImplementation struct { *sqlx.DB } -// Make sure *DBConnectionPoolImplementation implements DBConnectionPool: -var _ ConnectionPool = (*DBConnectionPoolImplementation)(nil) - const ( MaxDBConnIdleTime = 10 * time.Second MaxOpenDBConns = 30 diff --git a/internal/db/db_test.go b/internal/db/db_test.go index f981b8f..ee64545 100644 --- a/internal/db/db_test.go +++ b/internal/db/db_test.go @@ -9,10 +9,10 @@ import ( ) func TestOpenDBConnectionPool(t *testing.T) { - db := dbtest.Postgres(t) - defer db.Close() + dbt := dbtest.Postgres(t) + defer dbt.Close() - dbConnectionPool, err := OpenDBConnectionPool(db.DSN) + dbConnectionPool, err := OpenDBConnectionPool(dbt.DSN) require.NoError(t, err) defer dbConnectionPool.Close() diff --git a/internal/serve/httphandler/payments_handler_test.go b/internal/serve/httphandler/payments_handler_test.go index 9498dc2..d02358e 100644 --- a/internal/serve/httphandler/payments_handler_test.go +++ b/internal/serve/httphandler/payments_handler_test.go @@ -20,10 +20,10 @@ import ( ) func TestSubscribeAddress(t *testing.T) { - dbtest := dbtest.Open(t) - defer dbtest.Close() + dbt := dbtest.Open(t) + defer dbt.Close() - dbConnectionPool, err := db.OpenDBConnectionPool(dbtest.DSN) + dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN) require.NoError(t, err) defer dbConnectionPool.Close() @@ -101,10 +101,10 @@ func TestSubscribeAddress(t *testing.T) { } func TestUnsubscribeAddress(t *testing.T) { - dbtest := dbtest.Open(t) - defer dbtest.Close() + dbt := dbtest.Open(t) + defer dbt.Close() - dbConnectionPool, err := db.OpenDBConnectionPool(dbtest.DSN) + dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN) require.NoError(t, err) defer dbConnectionPool.Close()