Go linters check and auto-fix #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Go linters check and auto-fix | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| Update: | |
| description: 'Update go version' | |
| default: false | |
| type: boolean | |
| Version: | |
| description: 'Version for Go' | |
| required: false | |
| default: '1.25' | |
| Modules: | |
| description: 'Update dependencies in modules' | |
| default: false | |
| type: boolean | |
| Golangci: | |
| description: 'Golangci linters check' | |
| default: false | |
| type: boolean | |
| Fix: | |
| description: 'Golangci linters fix' | |
| default: false | |
| type: boolean | |
| PR: | |
| description: 'Create Pull Request' | |
| default: false | |
| type: boolean | |
| Gocrit: | |
| description: 'Go critical linters check' | |
| default: false | |
| type: boolean | |
| Gosec: | |
| description: 'Go security linters check' | |
| default: false | |
| type: boolean | |
| jobs: | |
| lint: | |
| name: Linters check | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| models: read | |
| runs-on: ubuntu-latest | |
| env: | |
| APP_VERSION: "latest" | |
| steps: | |
| - name: Checkout repository (main branch and 1 last commits) | |
| uses: actions/checkout@v6 | |
| with: | |
| fetch-depth: 1 | |
| ref: main | |
| - name: Install Go | |
| uses: actions/setup-go@v6 | |
| with: | |
| go-version: 1.25 | |
| - name: Install dependencies | |
| run: | | |
| go fmt ./... | |
| go vet ./... | |
| go get ./... | |
| go mod tidy | |
| go mod verify | |
| go build -v ./... | |
| - name: Get build parameters to summary | |
| shell: bash | |
| run: | | |
| echo "### Parameters" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Update go version**: ${{ github.event.inputs.Update == 'true' && '✅' || '❌' }}" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Update dependencies in** `go.mod`: ${{ github.event.inputs.Modules == 'true' && '✅' || '❌' }}" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Golangci linters check**: ${{ github.event.inputs.Golangci == 'true' && '✅' || '❌' }}" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Golangci linters auto-fix in** `main.go`: ${{ github.event.inputs.Fix == 'true' && '✅' || '❌' }}" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Create Pull Request**: ${{ github.event.inputs.PR == 'true' && '✅' || '❌' }}" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Go critical linters check**: ${{ github.event.inputs.Gocrit == 'true' && '✅' || '❌' }}" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Go security linters check**: ${{ github.event.inputs.Gosec == 'true' && '✅' || '❌' }}" >> $GITHUB_STEP_SUMMARY | |
| - name: Get app version in env | |
| run: | | |
| APP_VERSION=$(go run main.go -v) | |
| echo "APP_VERSION=$APP_VERSION" >> $GITHUB_ENV | |
| - name: Update Go version | |
| if: ${{ github.event.inputs.Update == 'true' && github.event.inputs.Version != '' }} | |
| run: | | |
| go mod edit -go=${{ github.event.inputs.Version }} | |
| # go mod edit -toolchain=go${{ github.event.inputs.Version }} | |
| go mod tidy | |
| - name: Reinstall Go on new version | |
| if: ${{ github.event.inputs.Update == 'true' && github.event.inputs.Version != '' }} | |
| uses: actions/setup-go@v6 | |
| with: | |
| go-version: ${{ github.event.inputs.Version }} | |
| cache: true | |
| - name: Update dependencies in modules | |
| if: ${{ github.event.inputs.Modules == 'true' }} | |
| run: go get -u ./... | |
| - name: Create Pull Request for update go version amd dependencies | |
| if: ${{ github.event.inputs.Modules == 'true' && github.event.inputs.PR == 'true' }} | |
| uses: peter-evans/create-pull-request@v8 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| commit-message: "update go version and package dependencies" | |
| branch: "fix/${{ env.APP_VERSION }}-update" | |
| title: "Updated dependencies" | |
| body: "Updated go version and modules in `go.mod` and `go.sum` files." | |
| add-paths: | | |
| go.mod | |
| go.sum | |
| - name: Install golangci | |
| if: ${{ github.event.inputs.golangci == 'true' || github.event.inputs.Fix == 'true' }} | |
| run: go install github.com/golangci/golangci-lint/v2/cmd/[email protected] | |
| - name: Golangci check config | |
| id: configCheck | |
| if: ${{ github.event.inputs.golangci == 'true' || github.event.inputs.Fix == 'true' }} | |
| run: golangci-lint config verify --verbose | |
| - name: Get used linters from config and parse in Markdown | |
| if: ${{ github.event.inputs.golangci == 'true' || github.event.inputs.Fix == 'true' }} | |
| run: | | |
| golangci-lint --version | |
| version=$(golangci-lint --version | grep -Eo "[0-9.]+" | head -n 1) | |
| echo "### Configuration" >> $GITHUB_STEP_SUMMARY | |
| echo "Golangci version: $version" >> $GITHUB_STEP_SUMMARY | |
| echo "Check config status: ${{ steps.configCheck.outcome }}" >> $GITHUB_STEP_SUMMARY | |
| golangci-lint linters | |
| golangci-lint linters > linters.md | |
| sed "s/Enabled by your configuration linters:/## Enabled linters/" -i linters.md | |
| sed "s/Disabled by your configuration linters:/## Disabled linters/" -i linters.md | |
| sed "s/ \[deprecated\]//g" -i linters.md | |
| sed "s/. \[(.+)\]/ (**$1**)./g" -i linters.md | |
| sed -E "s/\. \[(.+)\]/ (**\1**)./g" -i linters.md | |
| linters=$(cat linters.md | awk -F ":" '/:/ {print "- [" $1"](https://golangci-lint.run/docs/linters/configuration/#"$1") -" $2":"$3; next} {print $0}') | |
| echo "$linters" > linters.md | |
| - name: Golangci linters check using Action | |
| id: golangci | |
| if: ${{ github.event.inputs.golangci == 'true' || github.event.inputs.Fix == 'true' }} | |
| continue-on-error: true | |
| uses: golangci/golangci-lint-action@v9 | |
| with: | |
| version: v2.8.0 | |
| args: ./main.go | |
| - name: Get issues result from linters check to summary | |
| if: ${{ github.event.inputs.golangci == 'true' || github.event.inputs.Fix == 'true' }} | |
| continue-on-error: true | |
| run: | | |
| echo "### Linters check results" >> $GITHUB_STEP_SUMMARY | |
| golangci-lint run ./main.go > results.md || true | |
| sed -n '/issues:/,$p' -i results.md | |
| cat results.md >> $GITHUB_STEP_SUMMARY | |
| - name: Golangci linters fix in main.go | |
| if: ${{ github.event.inputs.Fix == 'true' }} | |
| continue-on-error: true | |
| run: golangci-lint run --fix ./main.go | |
| - name: Golangci linters check after fix | |
| if: ${{ github.event.inputs.Fix == 'true' }} | |
| continue-on-error: true | |
| uses: golangci/golangci-lint-action@v9 | |
| with: | |
| version: v2.8.0 | |
| args: ./main.go | |
| - name: Get issues result from linters check to summary after fix | |
| if: ${{ github.event.inputs.Fix == 'true' }} | |
| continue-on-error: true | |
| run: | | |
| echo "### Linters check results after fix" >> $GITHUB_STEP_SUMMARY | |
| golangci-lint run ./main.go > results.md || true | |
| sed -n '/issues:/,$p' -i results.md | |
| cat results.md >> $GITHUB_STEP_SUMMARY | |
| - name: Public used linters from config to summary | |
| if: ${{ github.event.inputs.golangci == 'true' || github.event.inputs.Fix == 'true' }} | |
| continue-on-error: true | |
| run: cat linters.md >> $GITHUB_STEP_SUMMARY | |
| - name: Create Pull Request for fix lint | |
| if: ${{ github.event.inputs.Fix == 'true' && github.event.inputs.PR == 'true' }} | |
| uses: peter-evans/create-pull-request@v8 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| commit-message: "autofix linters using golangci" | |
| branch: "fix/${{ env.APP_VERSION }}-lint" | |
| title: "Fixed linters" | |
| body: "Automatically fixing linters with golangci from GitHub Actions." | |
| add-paths: | | |
| main.go | |
| - name: Go critical linters check | |
| id: gocrit | |
| if: ${{ github.event.inputs.gocrit == 'true' }} | |
| continue-on-error: true | |
| run: | | |
| go install github.com/go-critic/go-critic/cmd/gocritic@latest | |
| gocritic check -v -enableAll ./main.go | |
| - name: Go security linters check | |
| id: gosec | |
| if: ${{ github.event.inputs.gosec == 'true' }} | |
| continue-on-error: true | |
| run: | | |
| go install github.com/securego/gosec/v2/cmd/gosec@latest | |
| gosec -severity=high ./... | |
| - name: Send report to Telegram | |
| uses: appleboy/telegram-action@master | |
| with: | |
| token: ${{ secrets.TELEGRAM_API_TOKEN }} | |
| to: ${{ secrets.TELEGRAM_CHANNEL_ID }} | |
| debug: true | |
| format: markdown | |
| message: | | |
| 🔔 **Action**: linters check and fix | |
| 📁 **Repository**: ${{ github.repository }} | |
| 👤 **User**: ${{ github.actor }} | |
| Parameters: | |
| **Update go version**: ${{ github.event.inputs.Update == 'true' && '✅' || '❌' }} | |
| **Update dependencies in** `go.mod`: ${{ github.event.inputs.Modules == 'true' && '✅' || '❌' }} | |
| **Golangci linters check**: ${{ github.event.inputs.Golangci == 'true' && '✅' || '❌' }} | |
| **Golangci linters fix in** `main.go`: ${{ github.event.inputs.Fix == 'true' && '✅' || '❌' }} | |
| **Create Pull Request**: ${{ github.event.inputs.PR == 'true' && '✅' || '❌' }} | |
| **Go critical linters check**: ${{ github.event.inputs.Gocrit == 'true' && '✅' || '❌' }} | |
| **Go security linters check**: ${{ github.event.inputs.Gosec == 'true' && '✅' || '❌' }} | |
| Results: | |
| ${{ steps.golangci.outcome == 'failure' && '❌' || '✅' }} **Golangci linters check**: ${{ steps.golangci.outcome }} | |
| ${{ steps.gocrit.outcome == 'failure' && '❌' || '✅' }} **Go critical linters check**: ${{ steps.gocrit.outcome }} | |
| ${{ steps.gosec.outcome == 'failure' && '❌' || '✅' }} **Go security linters check**: ${{ steps.gosec.outcome }} |