From 29f30dfd51dc8acbf5f4cbb7405662de5deee294 Mon Sep 17 00:00:00 2001 From: xun Date: Sun, 29 Sep 2024 14:13:11 +0800 Subject: [PATCH] update github action trigger --- .github/workflows/lint.yml | 67 +++++++++++++++++++++++++++++++++----- .golangci.yaml | 15 +++++---- src/go.mod | 2 +- 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 039ef67..408ee7a 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -2,7 +2,9 @@ name: golangci-lint on: push: - branches: [main] + branches: + - main + - next/cicd pull_request: branches: - main @@ -12,23 +14,70 @@ on: - "go.sum" - "**.go" +env: + GO_VERSION: '1.23' + jobs: - go-static-checks: + lint-and-test: + # Only run if push to main or not previously run in a pull request + if: | + github.event_name == 'push' && + ( + github.event.pull_request.head.repo.full_name != github.repository || + github.event.pull_request.merged != true + ) || + github.event_name == 'pull_request' + runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-go@v5 + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup go + uses: actions/setup-go@v5 with: - go-version: 1.23 + go-version: ${{ env.GO_VERSION }} check-latest: true - cache: true + cache: false + + # Cache Go modules to improve build time. + - name: Cache Go Modules + uses: actions/cache@v4 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + # Verify go.mod and go.sum are tidy. - name: Verify go.mod is tidy run: | - go mod tidy -go=1.23 - git diff --exit-code + cd src + go mod tidy -go=${{ env.GO_VERSION }} + git diff --exit-code go.sum + git diff --exit-code go.mod + + # Due to poor unit testing, we comment out it. + # + # Run unit tests. + # - name: Run unit tests + # run: | + # go test ./... -v -race -coverprofile=coverage.out + # env: + # GOFLAGS: "-mod=readonly" + + # Upload code coverage report. + # - name: Upload Coverage Report + # uses: actions/upload-artifact@v3 + # with: + # name: coverage-report + # path: coverage.out + - name: golangci-lint uses: golangci/golangci-lint-action@v6 with: version: v1.61.0 - args: --verbose --timeout=3m + args: --verbose --timeout=3m --config=.golangci.yaml ./src/ skip-cache: true diff --git a/.golangci.yaml b/.golangci.yaml index c8009f7..8052dc6 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -25,16 +25,17 @@ issues: linters-settings: goimports: - # Put imports beginning with prefix after 3rd-party packages. + # Organize imports so that local packages come after third-party packages. local-prefixes: github.com/NJUPT-SAST/sast-link-backend revive: - # Default to run all linters so that new rules in the future could automatically be added to the static check. + # Enable all rules but selectively disable ones that are too restrictive. enable-all-rules: true rules: - # The following rules are too strict and make coding harder. We do not enable them for now. - - name: file-header - disabled: true + # Rules description see revive documentation: https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md - name: line-length-limit + arguments: [120] # Set a reasonable line length limit + # Disable other rules that are too strict. + - name: file-header disabled: true - name: function-length disabled: true @@ -73,14 +74,16 @@ linters-settings: - name: max-control-nesting disabled: true gocritic: + # Rules description see gocritic documentation: https://go-critic.com/overview#checkers-from-the-diagnostic-group disabled-checks: - ifElseChain govet: settings: printf: # The name of the analyzer, run `go tool vet help` to see the list of all analyzers funcs: # Run `go tool vet help printf` to see the full configuration of `printf`. - - common.Errorf + - common.Errorf # Treat common.Errorf as a printf-style function. enable-all: true + # Rules description see govet documentation: https://pkg.go.dev/github.com/golangci/govet disable: - fieldalignment - shadow diff --git a/src/go.mod b/src/go.mod index 74080a3..68a07d1 100644 --- a/src/go.mod +++ b/src/go.mod @@ -34,7 +34,7 @@ require ( github.com/spf13/afero v1.9.5 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect + github.com/spf13/pflag v1.0.5 github.com/subosito/gotenv v1.4.2 // indirect )