Skip to content

Commit 0053199

Browse files
authored
Merge pull request #190 from pokt-network/dk-add-linters
[Automation] Add CI linters/checks
2 parents ba657c9 + 73f116b commit 0053199

File tree

13 files changed

+205
-60
lines changed

13 files changed

+205
-60
lines changed

.github/workflows/pr.yml .github/workflows/ggshield.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: GitGuardian scan
22

3-
on: [push, pull_request]
3+
on:
4+
workflow_dispatch:
5+
push:
46

57
jobs:
68
scanning:
@@ -16,6 +18,6 @@ jobs:
1618
env:
1719
GITHUB_PUSH_BEFORE_SHA: ${{ github.event.before }}
1820
GITHUB_PUSH_BASE_SHA: ${{ github.event.base }}
19-
GITHUB_PULL_BASE_SHA: ${{ github.event.pull_request.base.sha }}
21+
GITHUB_PULL_BASE_SHA: ${{ github.event.pull_request.base.sha }}
2022
GITHUB_DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
2123
GITGUARDIAN_API_KEY: ${{ secrets.GITGUARDIAN_API_KEY }}

.github/workflows/main.yml

+8-14
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ on:
1616

1717
env:
1818
# Even though we can test against multiple versions, this one is considered a target version.
19-
TARGET_GOLANG_VERSION: "1.18"
19+
TARGET_GOLANG_VERSION: "1.19"
2020
PROTOC_VERSION: "3.19.4"
2121
PUSH_TO_DOCKERHUB: false
2222

@@ -25,7 +25,7 @@ jobs:
2525
runs-on: ubuntu-latest
2626
strategy:
2727
matrix:
28-
go: ["1.18"] # As we are relying on generics, we can't go lower than 1.18.
28+
go: ["1.19"] # As we are relying on generics, we can't go lower than 1.18.
2929
fail-fast: false
3030
name: Go ${{ matrix.go }} test
3131
steps:
@@ -59,13 +59,15 @@ jobs:
5959
uses: guyarb/[email protected]
6060
with:
6161
test-results: test_results.json
62-
63-
# TODO(@okdas): move coverage to a separate job
62+
- name: Run golangci-lint
63+
# Only run if the test failed on target version to avoid duplicated annotations on GitHub.
64+
if: ${{ always() && env.TARGET_GOLANG_VERSION == matrix.go }}
65+
uses: golangci/golangci-lint-action@v3
6466
- name: create coverage report
65-
if: ${{ env.TARGET_GOLANG_VERSION == matrix.go }}
67+
if: ${{ always() && env.TARGET_GOLANG_VERSION == matrix.go }}
6668
run: make test_all_with_coverage
6769
- name: Upload coverage to Codecov
68-
if: ${{ env.TARGET_GOLANG_VERSION == matrix.go }}
70+
if: ${{ always() && env.TARGET_GOLANG_VERSION == matrix.go }}
6971
uses: codecov/codecov-action@v3
7072

7173
# TODO(@okdas): reuse artifacts built by the previous job instead
@@ -89,7 +91,6 @@ jobs:
8991
uses: docker/metadata-action@v4
9092
with:
9193
images: |
92-
poktnetwork/pocket-v1
9394
ghcr.io/pokt-network/pocket-v1
9495
tags: |
9596
type=schedule${{ matrix.imageType == 'dev' && ',suffix=-dev' || '' }}
@@ -100,13 +101,6 @@ jobs:
100101
type=ref,event=pr${{ matrix.imageType == 'dev' && ',suffix=-dev' || '' }}
101102
type=sha${{ matrix.imageType == 'dev' && ',suffix=-dev' || '' }}
102103
type=raw,value=latest,enable={{is_default_branch}}${{ matrix.imageType == 'dev' && ',suffix=-dev' || '' }}
103-
# Turned off until we set up docker hub
104-
- name: Login to DockerHub
105-
if: ${{ env.PUSH_TO_DOCKERHUB != 'false' }}
106-
uses: docker/login-action@v2
107-
with:
108-
username: ${{ secrets.DOCKERHUB_USERNAME }}
109-
password: ${{ secrets.DOCKERHUB_TOKEN }}
110104
- name: Login to GitHub Container Registry
111105
if: ${{ env.PUSH_TO_DOCKERHUB != 'false' }}
112106
uses: docker/login-action@v2

.golangci.yml

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
linters:
2+
enable:
3+
# TODO: investigate what linters we want to enable (many disabled by default!)
4+
# https://golangci-lint.run/usage/linters/#disabled-by-default
5+
6+
# Default linters
7+
- errcheck
8+
- gosimple
9+
- govet
10+
- ineffassign
11+
- staticcheck
12+
- typecheck
13+
- unused
14+
15+
# Additional linters
16+
- gosec
17+
- misspell
18+
- promlinter
19+
- unparam
20+
21+
# Gocritic allows to use ruleguard - custom linting rules
22+
- gocritic
23+
linters-settings:
24+
# Gocritic settings
25+
# https://go-critic.com/overview
26+
gocritic:
27+
enabled-checks:
28+
- ruleguard
29+
enabled-tags:
30+
- diagnostic
31+
- experimental
32+
- opinionated # This might be too much, but we can try it out
33+
- performance
34+
- style
35+
disabled-checks:
36+
- dupImport # https://github.com/go-critic/go-critic/issues/845
37+
- ifElseChain
38+
# - octalLiteral
39+
# - whyNoLint
40+
settings:
41+
ruleguard:
42+
# Enable debug to identify which 'Where' condition was rejected.
43+
# The value of the parameter is the name of a function in a ruleguard file.
44+
#
45+
# When a rule is evaluated:
46+
# If:
47+
# The Match() clause is accepted; and
48+
# One of the conditions in the Where() clause is rejected,
49+
# Then:
50+
# ruleguard prints the specific Where() condition that was rejected.
51+
#
52+
# The flag is passed to the ruleguard 'debug-group' argument.
53+
# Default: ""
54+
# debug: "testEq"
55+
# Determines the behavior when an error occurs while parsing ruleguard files.
56+
# If flag is not set, log error and skip rule files that contain an error.
57+
# If flag is set, the value must be a comma-separated list of error conditions.
58+
# - 'all': fail on all errors.
59+
# - 'import': ruleguard rule imports a package that cannot be found.
60+
# - 'dsl': gorule file does not comply with the ruleguard DSL.
61+
# Default: ""
62+
failOn: dsl
63+
rules: "build/linters/*.go"
64+
run:
65+
go: "1.19"
66+
skip-dirs:
67+
- build/linters
68+
build-tags:
69+
- codeanalysis

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ go_protoc-go-inject-tag:
8484
go_clean_deps:
8585
go mod tidy && go mod vendor
8686

87+
.PHONY: go_lint
88+
## Run all linters that are triggered by the CI pipeline
89+
go_lint:
90+
golangci-lint run ./...
91+
8792
.PHONY: gofmt
8893
## Format all the .go files in the project in place.
8994
gofmt:

build/Dockerfile.debian.dev

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Purpose of this container image is to ship pocket binary with additional
22
# tools such as dlv, curl, etc.
33

4-
ARG TARGET_GOLANG_VERSION=1.18
4+
ARG TARGET_GOLANG_VERSION=1.19
55

66
FROM golang:${TARGET_GOLANG_VERSION}-bullseye AS builder
77

@@ -33,7 +33,7 @@ RUN set -eux; \
3333
unzip -q protoc.zip -d /usr/local/; \
3434
protoc --version
3535

36-
# dlv
36+
# dlv
3737
RUN go install github.com/go-delve/delve/cmd/dlv@latest; \
3838
dlv version
3939

@@ -52,4 +52,4 @@ RUN go get -d -v ./app/pocket
5252
RUN go build -o /usr/local/bin/pocket ./app/pocket
5353
RUN go build -o /usr/local/bin/client ./app/client
5454

55-
CMD ["/usr/local/bin/pocket"]
55+
CMD ["/usr/local/bin/pocket"]

build/Dockerfile.debian.prod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Purpose of this container image is to ship pocket binary with minimal dependencies.
22

3-
ARG TARGET_GOLANG_VERSION=1.18
3+
ARG TARGET_GOLANG_VERSION=1.19
44

55
FROM golang:${TARGET_GOLANG_VERSION}-bullseye AS builder
66

@@ -53,4 +53,4 @@ RUN apt-get update -qq && \
5353

5454
COPY --from=builder /go/bin/pocket /usr/local/bin/pocket
5555

56-
CMD ["/usr/local/bin/pocket"]
56+
CMD ["/usr/local/bin/pocket"]

build/linters/tests.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//nolint // it's not a Go code file
2+
//go:build !codeanalysis
3+
// +build !codeanalysis
4+
5+
// This file includes our custom linters.
6+
// If you want to add/modify an existing linter, please check out ruleguard's documentation: https://github.com/quasilyte/go-ruleguard#documentation
7+
8+
package gorules
9+
10+
import (
11+
"github.com/quasilyte/go-ruleguard/dsl"
12+
)
13+
14+
// This is a custom linter that checks ensures a use of require.Equal
15+
func EqualInsteadOfTrue(m dsl.Matcher) {
16+
m.Match(`require.True($t, $x == $y, $*args)`).
17+
Suggest(`require.Equal($t, $x, $y, $args)`).
18+
Report(`use require.Equal instead of require.True`)
19+
}

docs/build/README.md

-25
This file was deleted.

docs/build/automatic-builds.md

-14
This file was deleted.

docs/development/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ Please note that this repository is under very active development and breaking c
1010
- [Running Unit Tests](#running-unit-tests)
1111
- [Running LocalNet](#running-localnet)
1212
- [Code Organization](#code-organization)
13+
- [Linters](#linters)
14+
- [Installation of golangci-lint](#installation-of-golangci-lint)
15+
- [Running linters locally](#running-linters-locally)
16+
- [VSCode Integration](#vscode-integration)
17+
- [Configuration](#configuration)
18+
- [Custom linters](#custom-linters)
1319

1420
## LFG - Development
1521

@@ -156,3 +162,44 @@ Pocket
156162
├── utility # Implementation of the Utility module
157163
├── Makefile # [to-be-deleted] The source of targets used to develop, build and test
158164
```
165+
166+
### Linters
167+
168+
We utilize `golangci-lint` to run the linters. It is a wrapper around a number of linters and is configured to run many at once. The linters are configured to run on every commit and pull request via CI, and all code issues are populated as GitHub annotations to let developers and reviewers easily locate an issue.
169+
170+
#### Installation of golangci-lint
171+
172+
Please follow the instructions on the [golangci-lint](https://golangci-lint.run/usage/install/#local-installation) website.
173+
174+
#### Running linters locally
175+
176+
You can run `golangci-lint` locally against all packages with:
177+
178+
```bash
179+
make go_lint
180+
```
181+
182+
If you need to specify any additional flags, you can run `golangci-lint` directly as it picks up the configuration from the `.golangci.yml` file.
183+
184+
#### VSCode Integration
185+
186+
`golangci-lint` has an integration with VSCode. Per [documentation](https://golangci-lint.run/usage/integrations/), recommended settings are:
187+
188+
```json
189+
"go.lintTool":"golangci-lint",
190+
"go.lintFlags": [
191+
"--fast"
192+
]
193+
```
194+
195+
#### Configuration
196+
197+
`golangci-lint` is a sophisticated tool including both default and custom linters. The configuration file, which can grow quite large, is located at [`.golangci.yml`](../../.golangci.yml).
198+
199+
The official documentation includes a list of different linters and their configuration options. Please refer to [this page](https://golangci-lint.run/usage/linters/) for more information.
200+
201+
#### Custom linters
202+
203+
We can write custom linters using [`go-ruleguard`](https://go-ruleguard.github.io/). The rules are located in the [`build/linters`](../../build/linters) directory. The rules are written in the [Ruleguard DSL](https://github.com/quasilyte/go-ruleguard/blob/master/_docs/dsl.md), if you've never worked with ruleguard in the past, it makes sense to go through [introduction article](https://quasilyte.dev/blog/post/ruleguard/) and [Ruleguard by example tour](https://go-ruleguard.github.io/by-example/).
204+
205+
Ruleguard is run via `gocritic` linter which is a part of `golangci-lint`, so if you wish to change configuration or debug a particular rule, you can modify the `.golangci.yml` file.

docs/releases/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Building & Versioning
2+
3+
- [Building & Versioning](#building--versioning)
4+
- [Release Tag Versioning](#release-tag-versioning)
5+
- [[WIP] Build Versioning](#wip-build-versioning)
6+
- [Container Images](#container-images)
7+
- [Tags](#tags)
8+
- [Extended images with additional tooling](#extended-images-with-additional-tooling)
9+
- [[TODO] Magefile build system](#todo-magefile-build-system)
10+
11+
## Release Tag Versioning
12+
13+
We follow Go's [Module Version Numbering](https://go.dev/doc/modules/version-numbers) for software releases along with typical [Software release life cycles](https://en.wikipedia.org/wiki/Software_release_life_cycle).
14+
15+
For example, `v0.0.1-alpha.pre.1` is the tag used for the first milestone merge and `v0.0.1-alpha.1` can be used for the first official alpha release.
16+
17+
## [WIP] Build Versioning
18+
19+
Automatic development / test / production builds are still a work in progress, but we plan to incorporate the following when we reach that point:
20+
21+
- `+dirty` for uncommited changes
22+
- `-version` flag that can be injected or defaults to `UNKNOWN`
23+
- `branch_name` and a shortened `commit_hash` should be included
24+
25+
For example, `X.Y.Z[-<pre_release_tag][+branch_name][+short_hash][+dirty]` is the name of a potential build we will release in the future.
26+
27+
## Container Images
28+
29+
Our images are hosted on Github's Container Registry (GHCR) and are available at `ghcr.io/poktnetwork/pocket-v1`. You can find the list of latest images [here](https://github.com/pokt-network/pocket/pkgs/container/pocket-v1).
30+
31+
### Tags
32+
33+
Code built from the default branch (i.e. `main`) is tagged as `latest`.
34+
35+
Code built from commits in Pull Requests, is tagged as `pr-<number>`, as well as `sha-<7 digit sha>`.
36+
37+
Once releases are managed, they will be tagged with the version number (e.g. `v0.0.1-alpha.pre.1`).
38+
39+
### Extended images with additional tooling
40+
41+
Extended images with additional tooling are built to aid in troubleshoot and debugging. The extended image is formatted as `<tag>-dev`. For example, `latest-dev`, or `pr-123-dev`.
42+
43+
## [TODO] Magefile build system
44+
45+
Once the V1 implementation reaches the stage of testable binaries, we are looking to use [Mage](https://magefile.org/) which is being tracked in [pocket/issues/43](https://github.com/pokt-network/pocket/issues/43) that'll inject a version with the `-version` flag.

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
github.com/dgraph-io/badger/v3 v3.2103.2
2525
github.com/jackc/pgconn v1.11.0
2626
github.com/jordanorelli/lexnum v0.0.0-20141216151731-460eeb125754
27+
github.com/quasilyte/go-ruleguard/dsl v0.3.21
2728
)
2829

2930
require (

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
354354
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
355355
github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU=
356356
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
357+
github.com/quasilyte/go-ruleguard/dsl v0.3.21 h1:vNkC6fC6qMLzCOGbnIHOd5ixUGgTbp3Z4fGnUgULlDA=
358+
github.com/quasilyte/go-ruleguard/dsl v0.3.21/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU=
357359
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
358360
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
359361
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=

0 commit comments

Comments
 (0)