Skip to content

Commit

Permalink
ci: automate protocol buffers go code generation and linting (#31)
Browse files Browse the repository at this point in the history
This was done using dagger modules to run the different tools, and a
Taskfile to orchestrate the execution.

Signed-off-by: Francesco Canovai <[email protected]>
Signed-off-by: Niccolò Fei <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Niccolò Fei <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
  • Loading branch information
3 people authored Mar 29, 2024
1 parent b0b3107 commit cf9a967
Show file tree
Hide file tree
Showing 63 changed files with 3,817 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"image": "mcr.microsoft.com/devcontainers/go:1-bullseye",
"features": {
"ghcr.io/devcontainers-contrib/features/protoc:1": {}
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
},

"postCreateCommand": "go install google.golang.org/protobuf/cmd/protoc-gen-go@latest; go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest",
"postCreateCommand": "curl -L https://dl.dagger.io/dagger/install.sh | BIN_DIR=$HOME/.local/bin sh; sh -c \"$(curl --location https://taskfile.dev/install.sh)\" -- -d -b $HOME/.local/bin",

"customizations": {
"vscode": {
Expand Down
26 changes: 16 additions & 10 deletions .github/workflows/continuous-delivery.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
name: backup-adapter CI

on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:

jobs:
lint:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- name: Checkout
uses: actions/checkout@v4
# We need the full history for the commitlint task
with:
go-version: '>=1.21.0'
- run: make build
fetch-depth: 0
ref: ${{ github.event.pull_request.head.sha }}
- name: Install Task
uses: arduino/setup-task@v2
- name: Install Dagger
run: |
curl -L https://dl.dagger.io/dagger/install.sh | BIN_DIR=$HOME/.local/bin sh
- name: Run CI task
run: |
task ci
9 changes: 4 additions & 5 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
name: release-please

on:
push:
branches:
- main
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
name: release-please

jobs:
release-please:
runs-on: ubuntu-latest
Expand All @@ -15,4 +14,4 @@ jobs:
with:
release-type: go
package-name: cnpg-i
token: ${{secrets.REPO_PAT}}
token: ${{ secrets.REPO_PAT }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.task/
.idea/
128 changes: 128 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
linters-settings:
lll:
line-length: 120
gci:
sections:
- standard
- default
- prefix(github.com/cloudnative-pg/cloudnative-pg)
- blank
- dot
gosec:
excludes:
- G101 # remove this exclude when https://github.com/securego/gosec/issues/1001 is fixed

linters:
# please, do not use `enable-all`: it's deprecated and will be removed soon.
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
disable-all: true
enable:
- asciicheck
- bodyclose
- dogsled
- dupl
- durationcheck
- errcheck
- exportloopref
- gci
- gocognit
- goconst
- gocritic
- gocyclo
- gofmt
- gofumpt
- goheader
- goimports
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- govet
- ginkgolinter
- importas
- ineffassign
- lll
- makezero
- misspell
- nakedret
- nestif
- prealloc
- predeclared
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- stylecheck
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- wastedassign
- whitespace

# to be checked:
# - errorlint
# - forbidigo
# - forcetypeassert
# - goerr113
# - ifshort
# - nilerr
# - nlreturn
# - noctx
# - nolintlint
# - paralleltest
# - promlinter
# - tagliatelle
# - wrapcheck

# don't enable:
# - cyclop
# - depguard
# - exhaustive
# - exhaustivestruct
# - funlen
# - gochecknoglobals
# - gochecknoinits
# - godot
# - godox
# - gomnd
# - testpackage
# - wsl

# deprecated:
# - deadcode
# - golint
# - interfacer
# - maligned
# - scopelint
# - structcheck
# - varcheck

run:
skip-files: "zz_generated.*"

issues:
exclude-rules:
# Allow dot imports for ginkgo and gomega
- source: ginkgo|gomega
linters:
- revive
text: "should not use dot imports"
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- goconst
# Exclude lll issues for lines with long annotations
- linters:
- lll
source: "//\\s*\\+"
# We have no control of this in zz_generated files and it looks like that excluding those files is not enough
# so we disable "ST1016: methods on the same type should have the same receiver name" in api directory
- linters:
- stylecheck
text: "ST1016:"
path: api/
exclude-use-default: false
3 changes: 0 additions & 3 deletions Makefile

This file was deleted.

75 changes: 75 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
version: '3'

tasks:
protolint:
desc: Run protolint
cmds:
- dagger -s call -m dagger/protolint lint --source . --args "--config_path=.protolint.yaml" --args proto/ stdout
sources:
- proto/**/*.proto

protoc-gen-go-grpc:
desc: Compile proto files
deps:
- protolint
cmds:
- >
dagger -s call -m dagger/protoc-gen-go-grpc run --source . --go-opt module=github.com/cloudnative-pg/cnpg-i
--go-grpcopt module=github.com/cloudnative-pg/cnpg-i --proto-path proto -o .
sources:
- proto/**/*.proto

lint:
desc: Run golangci-lint
deps:
- protoc-gen-go-grpc
cmds:
- >
dagger -s call -m github.com/sagikazarmark/daggerverse/golangci-lint@157bf2192a7a1e8672da2c4fee37d8710734c35a
run --source . --config .golangci.yml stdout
sources:
- ./**/*.go

protoc-gen-doc:
desc: Generate documentation from proto files
cmds:
- dagger -s call -m dagger/protoc-gen-doc generate --proto-dir proto -o docs
sources:
- proto/**/*.proto

build:
desc: Build the project
deps:
- lint
- protoc-gen-doc
cmds:
- >
dagger -s -m github.com/kpenfound/dagger-modules/golang@8d662e001caf8c16253226d0d456f2f0f374f009
call base --version 1.22-bookworm build --source . --args ./...
commitlint:
desc: Check for conventional commits
cmds:
- dagger -s call -m dagger/commitlint lint --source . --args "--from=origin/main" stdout

uncommitted:
desc: Check for uncommitted changes
deps:
- build
cmds:
- dagger -s call -m dagger/uncommitted/ check-uncommitted --source . stdout
sources:
- ./**

ci:
desc: Run the CI pipeline
deps:
- commitlint
- uncommitted

clean:
desc: Remove autogenerated artifacts
cmds:
- rm -f docs/docs.md
- rm -rf .task/
- rm -f pkg/*/*.pb.go
12 changes: 12 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const Configuration= {
extends: ['@commitlint/config-conventional'],
formatter: '@commitlint/format',
rules: {
'body-empty': [1, 'never'],
'body-case': [2, 'always', 'sentence-case'],
'references-empty': [1, 'never'],
'signed-off-by': [2, 'always', 'Signed-off-by:'],
},
};

module.exports = Configuration;
6 changes: 6 additions & 0 deletions dagger/commitlint/dagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "commitlint",
"sdk": "go",
"source": "dagger",
"engineVersion": "v0.10.2"
}
4 changes: 4 additions & 0 deletions dagger/commitlint/dagger/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/dagger.gen.go linguist-generated
/querybuilder/** linguist-generated
/internal/dagger/** linguist-generated
/internal/querybuilder/** linguist-generated
3 changes: 3 additions & 0 deletions dagger/commitlint/dagger/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/dagger.gen.go
/internal/dagger
/internal/querybuilder
13 changes: 13 additions & 0 deletions dagger/commitlint/dagger/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module main

go 1.21.3

require (
github.com/99designs/gqlgen v0.17.31
github.com/Khan/genqlient v0.6.0
github.com/vektah/gqlparser/v2 v2.5.6
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa
golang.org/x/sync v0.6.0
)

require github.com/stretchr/testify v1.9.0 // indirect
35 changes: 35 additions & 0 deletions dagger/commitlint/dagger/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
github.com/99designs/gqlgen v0.17.31 h1:VncSQ82VxieHkea8tz11p7h/zSbvHSxSDZfywqWt158=
github.com/99designs/gqlgen v0.17.31/go.mod h1:i4rEatMrzzu6RXaHydq1nmEPZkb3bKQsnxNRHS4DQB4=
github.com/Khan/genqlient v0.6.0 h1:Bwb1170ekuNIVIwTJEqvO8y7RxBxXu639VJOkKSrwAk=
github.com/Khan/genqlient v0.6.0/go.mod h1:rvChwWVTqXhiapdhLDV4bp9tz/Xvtewwkon4DpWWCRM=
github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/vektah/gqlparser/v2 v2.5.6 h1:Ou14T0N1s191eRMZ1gARVqohcbe1e8FrcONScsq8cRU=
github.com/vektah/gqlparser/v2 v2.5.6/go.mod h1:z8xXUff237NntSuH8mLFijZ+1tjV1swDbpDqjJmk6ME=
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ=
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit cf9a967

Please sign in to comment.