Skip to content

Commit 5be8598

Browse files
git actions: go workflow (stellar#8)
What Adds the Github Workflow running three steps: a linter, a builder and the unit tests. Why To ensure code quality.
1 parent c93a897 commit 5be8598

File tree

9 files changed

+130
-24
lines changed

9 files changed

+130
-24
lines changed

.github/workflows/go.yaml

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: ["**"]
8+
9+
jobs:
10+
check:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v5
18+
with:
19+
go-version: "1.22"
20+
21+
- name: golangci-lint
22+
uses: golangci/golangci-lint-action@v4
23+
with:
24+
version: v1.58
25+
args: --timeout=5m
26+
27+
- name: ./gomod.sh
28+
run: ./gomod.sh
29+
30+
- name: exhaustive github.com/nishanths/[email protected]
31+
run: |
32+
go install github.com/nishanths/exhaustive/cmd/[email protected]
33+
exhaustive -default-signifies-exhaustive ./...
34+
35+
- name: deadcode golang.org/x/tools/cmd/[email protected]
36+
run: |
37+
go install golang.org/x/tools/cmd/[email protected]
38+
output=$(deadcode -test ./...)
39+
if [[ -n "$output" ]]; then
40+
echo "🚨 Deadcode found:"
41+
echo "$output"
42+
exit 1
43+
else
44+
echo "✅ No deadcode found"
45+
fi
46+
47+
build:
48+
runs-on: ubuntu-latest
49+
steps:
50+
- name: Checkout
51+
uses: actions/checkout@v4
52+
53+
- name: Set up Go
54+
uses: actions/setup-go@v5
55+
with:
56+
go-version: "1.22"
57+
58+
- name: Build Project
59+
run: go build ./...
60+
61+
test:
62+
runs-on: ubuntu-latest
63+
services:
64+
postgres:
65+
image: postgres:16-alpine
66+
env:
67+
POSTGRES_USER: postgres
68+
POSTGRES_DB: postgres
69+
POSTGRES_PASSWORD: postgres
70+
PGHOST: localhost
71+
options: >-
72+
--health-cmd pg_isready
73+
--health-interval 10s
74+
--health-timeout 5s
75+
--health-retries 5
76+
ports:
77+
- 5432:5432
78+
env:
79+
PGHOST: localhost
80+
PGPORT: 5432
81+
PGUSER: postgres
82+
PGPASSWORD: postgres
83+
PGDATABASE: postgres
84+
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable
85+
86+
steps:
87+
- name: Checkout
88+
uses: actions/checkout@v4
89+
90+
- name: Set up Go
91+
uses: actions/setup-go@v5
92+
with:
93+
go-version: "1.22.1"
94+
95+
- name: Run Tests
96+
run: go test -v -race -cover ./...

cmd/root.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ var rootCmd = &cobra.Command{
1313
Use: "wallet-backend",
1414
Short: "Wallet Backend Server",
1515
Run: func(cmd *cobra.Command, args []string) {
16-
cmd.Help()
16+
err := cmd.Help()
17+
if err != nil {
18+
log.Fatalf("Error calling help command: %s", err.Error())
19+
}
1720
},
1821
}
1922

@@ -22,7 +25,7 @@ var rootCmd = &cobra.Command{
2225
func Execute() {
2326
err := rootCmd.Execute()
2427
if err != nil {
25-
log.Fatal(err)
28+
log.Fatalf("Error executing root command: %s", err.Error())
2629
}
2730
}
2831

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/rubenv/sql-migrate v1.6.1
1111
github.com/sirupsen/logrus v1.9.3
1212
github.com/spf13/cobra v1.8.0
13-
github.com/stellar/go v0.0.0-20240417192451-c63ad05d3e3b
13+
github.com/stellar/go v0.0.0-20231212225359-bc7173e667a6
1414
github.com/stretchr/testify v1.9.0
1515
)
1616

go.sum

+6-6
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
130130
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
131131
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
132132
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
133-
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
134-
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
133+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
134+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
135135
github.com/google/go-querystring v0.0.0-20160401233042-9235644dd9e5 h1:oERTZ1buOUYlpmKaqlO5fYmz8cZ1rYu5DieJzF4ZVmU=
136136
github.com/google/go-querystring v0.0.0-20160401233042-9235644dd9e5/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
137137
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=
256256
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
257257
github.com/spf13/viper v1.17.0 h1:I5txKw7MJasPL/BrfkbA0Jyo/oELqVmux4pR/UxOMfI=
258258
github.com/spf13/viper v1.17.0/go.mod h1:BmMMMLQXSbcHK6KAOiFLz0l5JHrU89OdIRHvsk0+yVI=
259-
github.com/stellar/go v0.0.0-20240417192451-c63ad05d3e3b h1:oKqqeJBLfSe5NkFjYb1vacvxbw0NQr+j14NYVWvUkiA=
260-
github.com/stellar/go v0.0.0-20240417192451-c63ad05d3e3b/go.mod h1:ckzsX0B0qfTMVZQJtPELJLs7cJ6xXMYHPVLyIsReGsU=
259+
github.com/stellar/go v0.0.0-20231212225359-bc7173e667a6 h1:LcQ01nwgxVoCmzAthjGSbxun9z/mxuqDy4uYETw4jEQ=
260+
github.com/stellar/go v0.0.0-20231212225359-bc7173e667a6/go.mod h1:PAWie4LYyDzJXqDVG4Qcj1Nt+uNk7sjzgSCXndQYsBA=
261261
github.com/stellar/go-xdr v0.0.0-20231122183749-b53fb00bcac2 h1:OzCVd0SV5qE3ZcDeSFCmOWLZfEWZ3Oe8KtmSOYKEVWE=
262262
github.com/stellar/go-xdr v0.0.0-20231122183749-b53fb00bcac2/go.mod h1:yoxyU/M8nl9LKeWIoBrbDPQ7Cy+4jxRcWcOayZ4BMps=
263263
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
380380
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
381381
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
382382
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
383-
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
384-
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
383+
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
384+
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
385385
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
386386
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
387387
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=

gomod.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#! /bin/bash
2+
set -e
3+
4+
go mod tidy
5+
git diff --exit-code -- go.mod || (echo "Go file go.mod is dirty, update the file with 'go mod tidy' locally." && exit 1)
6+
git diff --exit-code -- go.sum || (echo "Go file go.sum is dirty, update the file with 'go mod tidy' locally." && exit 1)
7+
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)

internal/data/payments_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
)
1414

1515
func TestSubscribeAddress(t *testing.T) {
16-
dbtest := dbtest.Open(t)
17-
defer dbtest.Close()
16+
dbt := dbtest.Open(t)
17+
defer dbt.Close()
1818

19-
dbConnectionPool, err := db.OpenDBConnectionPool(dbtest.DSN)
19+
dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN)
2020
require.NoError(t, err)
2121
defer dbConnectionPool.Close()
2222

internal/db/db.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ type ConnectionPool interface {
1919
SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
2020
}
2121

22+
// Make sure *DBConnectionPoolImplementation implements DBConnectionPool:
23+
var _ ConnectionPool = (*DBConnectionPoolImplementation)(nil)
24+
2225
type DBConnectionPoolImplementation struct {
2326
*sqlx.DB
2427
}
2528

26-
// Make sure *DBConnectionPoolImplementation implements DBConnectionPool:
27-
var _ ConnectionPool = (*DBConnectionPoolImplementation)(nil)
28-
2929
const (
3030
MaxDBConnIdleTime = 10 * time.Second
3131
MaxOpenDBConns = 30

internal/db/db_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
)
1010

1111
func TestOpenDBConnectionPool(t *testing.T) {
12-
db := dbtest.Postgres(t)
13-
defer db.Close()
12+
dbt := dbtest.Postgres(t)
13+
defer dbt.Close()
1414

15-
dbConnectionPool, err := OpenDBConnectionPool(db.DSN)
15+
dbConnectionPool, err := OpenDBConnectionPool(dbt.DSN)
1616
require.NoError(t, err)
1717
defer dbConnectionPool.Close()
1818

internal/serve/httphandler/payments_handler_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020
)
2121

2222
func TestSubscribeAddress(t *testing.T) {
23-
dbtest := dbtest.Open(t)
24-
defer dbtest.Close()
23+
dbt := dbtest.Open(t)
24+
defer dbt.Close()
2525

26-
dbConnectionPool, err := db.OpenDBConnectionPool(dbtest.DSN)
26+
dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN)
2727
require.NoError(t, err)
2828
defer dbConnectionPool.Close()
2929

@@ -101,10 +101,10 @@ func TestSubscribeAddress(t *testing.T) {
101101
}
102102

103103
func TestUnsubscribeAddress(t *testing.T) {
104-
dbtest := dbtest.Open(t)
105-
defer dbtest.Close()
104+
dbt := dbtest.Open(t)
105+
defer dbt.Close()
106106

107-
dbConnectionPool, err := db.OpenDBConnectionPool(dbtest.DSN)
107+
dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN)
108108
require.NoError(t, err)
109109
defer dbConnectionPool.Close()
110110

0 commit comments

Comments
 (0)