From 994378c3dab82479d248d822b22eba84ab870d2f Mon Sep 17 00:00:00 2001 From: Matthew Sevey <15232757+MSevey@users.noreply.github.com> Date: Thu, 30 May 2024 12:00:37 -0400 Subject: [PATCH] feat: add linters (#402) * fix: add markdownlint and resolve errors * fix: add and resolve errors for go vet and yamllint * fix: add and resolve errors for golangci * feat: add additional Makefile commands * feat(ci): add lint ci * chore: fix mispelled name * chore: clean up punctuation * feat(ci): add govulncheck to make lint and lint ci * chore: define constant for file perms --- .env | 7 ++- .github/workflows/knuu_testing.yml | 5 +- .github/workflows/lint.yml | 55 ++++++++++++++++++ .gitignore | 4 ++ .golangci.yml | 46 +++++++++++++++ .markdownlint.yaml | 6 ++ .yamllint.yml | 9 +++ CONTRIBUTING.md | 53 ++++++++--------- LICENCE => LICENSE | 0 Makefile | 58 ++++++++++++++++++- README.md | 4 +- .../adr-001-redesign-knuu-async-approach.md | 22 +++---- e2e/basic/file_test_image_cached_test.go | 2 +- e2e/basic/files_to_volumes_cm_test.go | 4 +- e2e/basic/folder_test.go | 3 +- e2e/basic/folder_test_image_cached_test.go | 2 +- e2e/basic/main_test.go | 3 +- e2e/basic/probe_test.go | 3 +- e2e/basic/rbac_test.go | 3 +- pkg/builder/docker/docker.go | 5 +- pkg/builder/kaniko/kaniko.go | 7 ++- pkg/builder/kaniko/kaniko_test.go | 5 +- pkg/builder/kaniko/utils_test.go | 6 +- pkg/container/docker.go | 4 +- pkg/instance/bittwister.go | 3 +- pkg/instance/helper.go | 3 +- pkg/instance/instance.go | 4 +- pkg/k8s/k8s_custom_resource.go | 2 +- pkg/k8s/k8s_pod.go | 3 +- pkg/k8s/types.go | 11 ++-- pkg/knuu/instance_old.go | 5 +- pkg/knuu/knuu_test.go | 7 ++- pkg/minio/minio.go | 2 +- pkg/preloader/preloader.go | 3 +- pkg/system/dependencies.go | 3 +- pkg/traefik/traefik.go | 5 +- 36 files changed, 280 insertions(+), 87 deletions(-) create mode 100644 .github/workflows/lint.yml create mode 100644 .gitignore create mode 100644 .golangci.yml create mode 100644 .markdownlint.yaml create mode 100644 .yamllint.yml rename LICENCE => LICENSE (100%) diff --git a/.env b/.env index a0ff0aab..5087a0d1 100644 --- a/.env +++ b/.env @@ -1,3 +1,6 @@ -KNUU_TIMEOUT=60m # The timeout for the tests +# NOTE: this file should only contain generic environment variables as it is +# currently part of the git history. + +KNUU_TIMEOUT=60m # The timeout for the tests KNUU_BUILDER=docker # The builder to use for building images. docker or kubernetes -LOG_LEVEL=info # The debug level. debug, info, warn, error +LOG_LEVEL=info # The debug level. debug, info, warn, error diff --git a/.github/workflows/knuu_testing.yml b/.github/workflows/knuu_testing.yml index 28089b09..7a2e9de5 100644 --- a/.github/workflows/knuu_testing.yml +++ b/.github/workflows/knuu_testing.yml @@ -2,14 +2,13 @@ name: Knuu Testing on: push: - branches: [ main ] + branches: [main] pull_request: - branches: [ main ] + branches: [main] workflow_dispatch: jobs: test: - runs-on: ubuntu-latest steps: diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000..48683a85 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,55 @@ +name: Lint +on: + push: + branches: [main] + pull_request: + workflow_dispatch: + merge_group: + +jobs: + golangci-lint: + name: golangci-lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: 1.22.1 + # This steps sets the GIT_DIFF environment variable to true + # if files defined in PATTERS changed + - uses: technote-space/get-diff-action@v6.1.2 + with: + # This job will pass without running if go.mod, go.sum, and *.go + # wasn't modified. + PATTERNS: | + **/**.go + go.mod + go.sum + - uses: golangci/golangci-lint-action@v6.0.1 + with: + version: latest + args: --timeout 10m + github-token: ${{ secrets.github_token }} + if: env.GIT_DIFF + + yamllint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: celestiaorg/.github/.github/actions/yamllint@v0.4.1 + + markdown-lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: celestiaorg/.github/.github/actions/markdown-lint@v0.4.1 + + govulncheck: + runs-on: ubuntu-latest + name: Run govulncheck + steps: + - id: govulncheck + uses: golang/govulncheck-action@v1 + with: + go-version-input: 1.22.1 + go-package: ./... diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..26490d39 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +coverage.txt +.vscode/launch.json +*/**.html +*.idea diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..f6d91b67 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,46 @@ +run: + timeout: 5m + modules-download-mode: readonly + +linters: + disable: + # TODO: there are tons of errors for these, we need to handle in a separate PR + - errorlint + - errcheck + - gosec + enable: + - gofmt + - goimports + - gosimple + - govet + - ineffassign + - misspell + - revive + - staticcheck + - typecheck + - unconvert + - unused + +issues: + exclude-use-default: false + include: + - EXC0012 # EXC0012 revive: Annoying issue about not having a comment. The rare codebase has such comments + - EXC0014 # EXC0014 revive: Annoying issue about not having a comment. The rare codebase has such comments + exclude: + - SA1019 # TODO enable: SA 1019 staticcheck: this is being triggered by the refactor and all the deprecated code. We need to enable this once we make the breaking change official + +linters-settings: + revive: + rules: + - name: package-comments + disabled: true + - name: duplicated-imports + severity: warning + - name: exported + disabled: true + # Leaving for reference if we can enable in the future. + # arguments: + # - disableStutteringCheck + + goimports: + local-prefixes: github.com/celestiaorg diff --git a/.markdownlint.yaml b/.markdownlint.yaml new file mode 100644 index 00000000..ee333bec --- /dev/null +++ b/.markdownlint.yaml @@ -0,0 +1,6 @@ +default: true +MD010: false +MD013: false +MD041: false +MD024: + allow_different_nesting: true diff --git a/.yamllint.yml b/.yamllint.yml new file mode 100644 index 00000000..cd2a9e82 --- /dev/null +++ b/.yamllint.yml @@ -0,0 +1,9 @@ +--- +# Built from docs https://yamllint.readthedocs.io/en/stable/configuration.html +extends: default + +rules: + # 120 chars should be enough, but don't fail if a line is longer + line-length: + max: 120 + level: warning diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c8eb2561..35842760 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,48 +10,49 @@ If you are new to Knuu, please go through the [README](./README.md) to familiari - Have a suggestion you want to implement? Open an issue, then create a Pull Request with your implementation. -- Want to resolve an issue in the repo, make a request to be assigned, and be sure to tag the issue in your PR!. +- Want to resolve an issue in the repo, make a request to be assigned, and be sure to tag the issue in your PR! ## Contribution Workflow To contribute to Knuu, follow these steps: 1. Fork the repository: Click on the 'Fork' button at the top right of this page. This will create a copy of the repository in your GitHub account. - -2. Clone the forked repository to your local machine. Open your terminal and run: -```bash -git clone -``` +1. Clone the forked repository to your local machine. Open your terminal and run: -3. Create your working branch: In the project directory, create a new branch by running the following command in your terminal: + ```bash + git clone + ``` -```bash -git checkout -b working-branch -``` -Be sure to name your branch according to the changes you are making. -For example: `add-missing-tests`. +1. Create your working branch: In the project directory, create a new branch by running the following command in your terminal: -4. Make your changes: Do not address multiple issues per PR. -For example, if you are adding a feature, it should not have bug fixes too. This is to enable the maintainers review your PR efficiently. + ```bash + git checkout -b working-branch + ``` -5. Commit the changes to your branch: After making the desired changes to the repo, run the following commands to commit them: + Be sure to name your branch according to the changes you are making. + For example: `add-missing-tests`. -```bash -git add . -``` +1. Make your changes: Do not address multiple issues per PR. + For example, if you are adding a feature, it should not have bug fixes too. This is to enable the maintainers review your PR efficiently. -```bash -git commit -m "add an appropriate commit message" -``` +1. Commit the changes to your branch: After making the desired changes to the repo, run the following commands to commit them: -```bash -git push origin -``` + ```bash + git add . + ``` -To ensure that your contribution is working as expected, please run `knuu-example` with your fork and working branch. + ```bash + git commit -m "add an appropriate commit message" + ``` -5. Create a Pull Request: Go to your forked repository on GitHub, and be sure that you are in the branch you pushed the changes to. Click on the 'Compare & pull request' button. This will open a new page where you can create your PR. Fill in the description field and click on 'Create pull request'. + ```bash + git push origin + ``` + + To ensure that your contribution is working as expected, please run `knuu-example` with your fork and working branch. + +1. Create a Pull Request: Go to your forked repository on GitHub, and be sure that you are in the branch you pushed the changes to. Click on the 'Compare & pull request' button. This will open a new page where you can create your PR. Fill in the description field and click on 'Create pull request'. Be sure to name your PR with a semantic prefix. For example, if it is a fix, it should be specified with the `fix:` prefix. Congratulations! You have successfully made a Pull Request, and your changes will be reviewed by the maintainers. diff --git a/LICENCE b/LICENSE similarity index 100% rename from LICENCE rename to LICENSE diff --git a/Makefile b/Makefile index 724ba306..3bdd6e7c 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,62 @@ pkgs := $(shell go list ./...) run := . count := 1 -test: +## help: Show this help message +help: Makefile + @echo " Choose a command run in "$(PROJECTNAME)":" + @sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /' +.PHONY: help + +## clean: clean testcache +clean: + @echo "--> Clearing testcache" + @go clean --testcache + @rm coverage.txt +.PHONY: clean + +## cover: generate to code coverage report. +cover: + @echo "--> Generating Code Coverage" + @go install github.com/ory/go-acc@latest + @go-acc -o coverage.txt $(pkgs) +.PHONY: cover + +## deps: Install dependencies +deps: + @echo "--> Installing dependencies" + @go mod download + @go mod tidy +.PHONY: deps + +## lint: Run linters golangci-lint and markdownlint. +lint: vet + @echo "--> Running markdownlint" + @markdownlint --config .markdownlint.yaml '**/*.md' + @echo "--> Running yamllint" + @yamllint --no-warnings . -c .yamllint.yml + @echo "--> Running actionlint" + @actionlint + @echo "--> Running govulncheck" + @govulncheck ./... + @echo "--> Running golangci-lint" + @golangci-lint run +.PHONY: lint + +## fmt: Run fixes for linters. +fmt: + @echo "--> Formatting markdownlint" + @markdownlint --config .markdownlint.yaml '**/*.md' -f + @echo "--> Formatting go" + @golangci-lint run --fix +.PHONY: fmt + +## vet: Run go vet +vet: + @echo "--> Running go vet" + @go vet $(pkgs) +.PHONY: vet + +## test: Run the testsuite +test: vet KNUU_TIMEOUT=120m go test -v $(pkgs) -run $(run) -count=$(count) -timeout 120m .PHONY: test diff --git a/README.md b/README.md index db76891b..daac61b8 100644 --- a/README.md +++ b/README.md @@ -204,7 +204,7 @@ In the folder `e2e`, you will find some examples of how to use the [knuu](https: ## Write Tests -You can find the relevant documentation in the `pkg/knuu` package at: https://pkg.go.dev/github.com/celestiaorg/knuu +You can find the relevant documentation in the `pkg/knuu` package at: ## Run @@ -256,7 +256,7 @@ See [SECURITY.md](SECURITY.md) for security and disclosure information. ## Licensing -Knuu is licensed under the [Apache License 2.0](LICENSE). +Knuu is licensed under the [Apache License 2.0](./LICENSE).