Skip to content

Commit 46037cf

Browse files
authored
Add Staticcheck to Build Pipeline (hashicorp#2421)
Run staticcheck linters on pull requests
1 parent 38b1239 commit 46037cf

11 files changed

+495
-19
lines changed

.github/workflows/linting.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: "golangci-lint"
2+
on: ["pull_request"]
3+
4+
jobs:
5+
lint:
6+
name: "Run Linter"
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
with:
11+
fetch-depth: '0'
12+
- name: Determine Go version
13+
id: get-go-version
14+
# We use .go-version as our source of truth for current Go
15+
# version, because "goenv" can react to it automatically.
16+
run: |
17+
echo "Building with Go $(cat .go-version)"
18+
echo "::set-output name=go-version::$(cat .go-version)"
19+
- name: Set up Go
20+
uses: actions/setup-go@v3
21+
with:
22+
go-version: "${{ steps.get-go-version.outputs.go-version }}"
23+
- name: Install Dependencies
24+
# if we really need to we can update this to run `make tools`
25+
# later but its just not necessary to only run linters
26+
run: |
27+
make golangci-lint
28+
- name: Running Linters
29+
run: |
30+
LINT_DIFF_BRANCH="origin/${GITHUB_BASE_REF}" make lint-diff

.golangci.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
linters:
2+
disable-all: true
3+
enable:
4+
- staticcheck
5+
- stylecheck
6+
7+
issues:
8+
exclude-rules:
9+
- linters:
10+
- stylecheck
11+
text: "ST1005:"
12+
- linters:
13+
- stylecheck
14+
text: "ST1003:"

Makefile

+23-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ TMP_DIR := $(shell mktemp -d)
77
REPO_PATH := github.com/hashicorp/boundary
88

99
CGO_ENABLED?=0
10+
GO_PATH = $(shell go env GOPATH)
1011

1112
export GEN_BASEPATH := $(shell pwd)
1213

@@ -19,11 +20,21 @@ cli:
1920
$(MAKE) --environment-overrides -C internal/cmd/gencli cli
2021

2122
.PHONY: tools
22-
tools:
23+
tools: golangci-lint
2324
go generate -tags tools tools/tools.go
2425
go install github.com/bufbuild/buf/cmd/[email protected]
2526
go install github.com/mfridman/[email protected]
2627

28+
# golangci-lint recommends installing the binary directly, instead of using go get
29+
# See the note: https://golangci-lint.run/usage/install/#install-from-source
30+
.PHONY: golangci-lint
31+
golangci-lint:
32+
$(eval GOLINT_INSTALLED := $(shell which golangci-lint))
33+
34+
if [ "$(GOLINT_INSTALLED)" = "" ]; then \
35+
sh scripts/install-golangci-lint.sh -b $(GO_PATH)/bin v1.50.1; \
36+
fi;
37+
2738
.PHONY: cleangen
2839
cleangen:
2940
@rm -f $(shell find ${THIS_DIR} -name '*.gen.go' && find ${THIS_DIR} -name '*.pb.go' && find ${THIS_DIR} -name '*.pb.gw.go')
@@ -63,6 +74,17 @@ fmt:
6374
grep -L -R "^\/\/ Code generated .* DO NOT EDIT\.$$" --exclude-dir=.git --include="*.go" . | xargs gofumpt -w
6475
buf format -w
6576

77+
lint:
78+
golangci-lint run --timeout 10m
79+
80+
ifndef LINT_DIFF_BRANCH
81+
override LINT_DIFF_BRANCH = main
82+
endif
83+
84+
lint-diff:
85+
@echo "Checking for lint compared to $(LINT_DIFF_BRANCH)"
86+
golangci-lint run --timeout 10m --new-from-rev=$(LINT_DIFF_BRANCH)
87+
6688
# Set env for all UI targets.
6789
UI_TARGETS := update-ui-version build-ui build-ui-ifne clean-ui
6890
# Note the extra .tmp path segment in UI_CLONE_DIR is significant and required.

internal/credential/vault/repository_credential_library.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ func (pl *listLookupLibrary) toCredentialLibrary() *CredentialLibrary {
405405
}
406406

407407
// TableName returns the table name for gorm.
408-
func (_ *listLookupLibrary) TableName() string { return "credential_vault_library_list_lookup" }
408+
func (*listLookupLibrary) TableName() string { return "credential_vault_library_list_lookup" }
409409

410410
// GetPublicId returns the public id.
411411
func (pl *listLookupLibrary) GetPublicId() string { return pl.PublicId }

internal/credential/vault/repository_credential_store.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ func (ps *listLookupStore) toCredentialStore() *CredentialStore {
320320
}
321321

322322
// TableName returns the table name for gorm.
323-
func (_ *listLookupStore) TableName() string { return "credential_vault_store_list_lookup" }
323+
func (*listLookupStore) TableName() string { return "credential_vault_store_list_lookup" }
324324

325325
// GetPublicId returns the public id.
326326
func (ps *listLookupStore) GetPublicId() string { return ps.PublicId }

internal/db/read_writer.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,9 @@ func (rw *Db) DeleteItems(ctx context.Context, deleteItems []any, opt ...Option)
397397
// you should ensure that any objects written to the db in your TxHandler are retryable, which
398398
// means that the object may be sent to the db several times (retried), so things like the primary key must
399399
// be reset before retry
400-
func (w *Db) DoTx(ctx context.Context, retries uint, backOff Backoff, handler TxHandler) (RetryInfo, error) {
400+
func (rw *Db) DoTx(ctx context.Context, retries uint, backOff Backoff, handler TxHandler) (RetryInfo, error) {
401401
const op = "db.DoTx"
402-
if w.underlying == nil {
402+
if rw.underlying == nil {
403403
return RetryInfo{}, errors.New(ctx, errors.InvalidParameter, op, "missing underlying db")
404404
}
405405
if backOff == nil {
@@ -415,7 +415,7 @@ func (w *Db) DoTx(ctx context.Context, retries uint, backOff Backoff, handler Tx
415415
}
416416

417417
// step one of this, start a transaction...
418-
beginTx, err := dbw.New(w.underlying.wrapped.Load()).Begin(ctx)
418+
beginTx, err := dbw.New(rw.underlying.wrapped.Load()).Begin(ctx)
419419
if err != nil {
420420
return info, wrapError(ctx, err, op)
421421
}

internal/iam/principal_role.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ func (r *UserRole) Clone() any {
108108
}
109109

110110
// VetForWrite implements db.VetForWrite() interface for user roles.
111-
func (role *UserRole) VetForWrite(ctx context.Context, _ db.Reader, _ db.OpType, _ ...db.Option) error {
111+
func (r *UserRole) VetForWrite(ctx context.Context, _ db.Reader, _ db.OpType, _ ...db.Option) error {
112112
const op = "iam.(UserRole).VetForWrite"
113-
if role.RoleId == "" {
113+
if r.RoleId == "" {
114114
return errors.New(ctx, errors.InvalidParameter, op, "missing role id")
115115
}
116-
if role.PrincipalId == "" {
116+
if r.PrincipalId == "" {
117117
return errors.New(ctx, errors.InvalidParameter, op, "missing user id")
118118
}
119119
return nil
@@ -185,12 +185,12 @@ func (r *GroupRole) Clone() any {
185185
}
186186

187187
// VetForWrite implements db.VetForWrite() interface for group roles.
188-
func (role *GroupRole) VetForWrite(ctx context.Context, r db.Reader, opType db.OpType, opt ...db.Option) error {
188+
func (r *GroupRole) VetForWrite(ctx context.Context, _ db.Reader, opType db.OpType, opt ...db.Option) error {
189189
const op = "iam.(GroupRole).VetForWrite"
190-
if role.RoleId == "" {
190+
if r.RoleId == "" {
191191
return errors.New(ctx, errors.InvalidParameter, op, "missing role id")
192192
}
193-
if role.PrincipalId == "" {
193+
if r.PrincipalId == "" {
194194
return errors.New(ctx, errors.InvalidParameter, op, "missing group id")
195195
}
196196
return nil
@@ -264,12 +264,12 @@ func (r *ManagedGroupRole) Clone() any {
264264
}
265265

266266
// VetForWrite implements db.VetForWrite() interface for managed group roles.
267-
func (role ManagedGroupRole) VetForWrite(ctx context.Context, r db.Reader, opType db.OpType, opt ...db.Option) error {
267+
func (r ManagedGroupRole) VetForWrite(ctx context.Context, _ db.Reader, opType db.OpType, opt ...db.Option) error {
268268
const op = "iam.(ManagedGroupRole).VetForWrite"
269-
if role.RoleId == "" {
269+
if r.RoleId == "" {
270270
return errors.New(ctx, errors.InvalidParameter, op, "missing role id")
271271
}
272-
if role.PrincipalId == "" {
272+
if r.PrincipalId == "" {
273273
return errors.New(ctx, errors.InvalidParameter, op, "missing managed group id")
274274
}
275275
return nil

internal/observability/event/event_observation.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ func newObservation(fromOperation Op, opt ...Option) (*observation, error) {
5555
}
5656

5757
// EventType is required for all event types by the eventlogger broker
58-
func (i *observation) EventType() string { return string(ObservationType) }
58+
func (o *observation) EventType() string { return string(ObservationType) }
5959

60-
func (i *observation) validate() error {
60+
func (o *observation) validate() error {
6161
const op = "event.(Observation).validate"
62-
if i.ID == "" {
62+
if o.ID == "" {
6363
return fmt.Errorf("%s: missing id: %w", op, ErrInvalidParameter)
6464
}
65-
if i.Op == "" {
65+
if o.Op == "" {
6666
return fmt.Errorf("%s: missing operation: %w", op, ErrInvalidParameter)
6767
}
6868
return nil

internal/session/job_session_cleanup_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func TestSessionConnectionCleanupJob(t *testing.T) {
4040
serversRepo, err := server.NewRepository(rw, rw, kms)
4141
require.NoError(err)
4242
sessionRepo, err := NewRepository(ctx, rw, rw, kms)
43+
require.NoError(err)
4344
connectionRepo, err := NewConnectionRepository(ctx, rw, rw, kms)
4445
require.NoError(err)
4546

internal/session/repository_session_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,7 @@ func TestRepository_deleteTargetFKey(t *testing.T) {
15641564
kms := kms.TestKms(t, conn, wrapper)
15651565
ctx := context.Background()
15661566
repo, err := NewRepository(ctx, rw, rw, kms)
1567+
require.NoError(t, err)
15671568
targetRepo, err := target.NewRepository(ctx, rw, rw, kms)
15681569
require.NoError(t, err)
15691570

0 commit comments

Comments
 (0)