From 3af370462a58385ab51e19261dd4dda7096ae476 Mon Sep 17 00:00:00 2001 From: zakir <80246097+zakir-code@users.noreply.github.com> Date: Sat, 19 Oct 2024 11:08:27 +0800 Subject: [PATCH] chore: add custom linter (#757) --- .github/workflows/golang.yml | 10 ++++++- Makefile | 10 ++----- scripts/linter.sh | 55 ++++++++++++++++++++++++++++++++++++ tests/crosschain_test.go | 2 +- tests/integration_test.go | 4 +-- tests/precompile_suite.go | 4 +-- 6 files changed, 72 insertions(+), 13 deletions(-) create mode 100755 scripts/linter.sh diff --git a/.github/workflows/golang.yml b/.github/workflows/golang.yml index ed6b0ba5..fadc1401 100644 --- a/.github/workflows/golang.yml +++ b/.github/workflows/golang.yml @@ -32,6 +32,15 @@ concurrency: cancel-in-progress: true jobs: + custom-lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: | + curl -sLO https://github.com/BurntSushi/ripgrep/releases/download/14.1.0/ripgrep_14.1.0-1_amd64.deb + sudo dpkg -i ripgrep_14.1.0-1_amd64.deb + - run: ./scripts/linter.sh + lint: runs-on: ubuntu-latest steps: @@ -40,7 +49,6 @@ jobs: with: go-version: '1.23' - run: | - make check-no-lint version=$(grep golangci_version= ./Makefile | awk -F '=' '{print $2}') echo "GOLANGCI_VERSION=$version" >> $GITHUB_ENV - uses: golangci/golangci-lint-action@v6 diff --git a/Makefile b/Makefile index fce4e6bb..f6fbdf36 100644 --- a/Makefile +++ b/Makefile @@ -141,14 +141,10 @@ lint-install: go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version); \ fi -check-no-lint: - @if [ $$(find . -name '*.go' -type f | xargs grep 'nolint\|#nosec' | wc -l) -ne 26 ]; then \ - echo "\033[91m--> increase or decrease nolint, please recheck them\033[0m"; \ - echo "\033[91m--> list nolint: \`find . -name '*.go' -type f | xargs grep 'nolint\|#nosec'\`\033[0m"; \ - exit 1;\ - fi +custom-lint: + @./scripts/linter.sh -lint: check-no-lint lint-install +lint: custom-lint lint-install @echo "--> Running linter" @golangci-lint run --build-tags=$(GO_BUILD) --out-format=tab diff --git a/scripts/linter.sh b/scripts/linter.sh new file mode 100755 index 00000000..b08c9acb --- /dev/null +++ b/scripts/linter.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +set -eo pipefail + +patternLimits=( + "nolint:21" + "#nosec:5" + "CrossChain:4" + "cross chain:0" +) + +if ! command -v rg &>/dev/null; then + echo "rg command not found, please install rg first: https://github.com/BurntSushi/ripgrep?tab=readme-ov-file#installation" && exit 1 +fi + +check_pattern_count() { + local pattern=$1 + local allowed_count=$2 + local file_type=$3 + + # Default values if not provided + file_type=${file_type:-go} + + rg_args=(--type go --glob '!*.pb.go' --glob '!*.pulsar.go' --glob '!*.sol.go' --glob '!legacy.go') + + if [[ "$allowed_count" -eq 0 ]]; then + if rg "${rg_args[@]}" "$pattern" ./ >/dev/null; then + echo "Warning: Matches found for '$pattern'." + exit 1 + fi + return + fi + + # Count the actual number of 'pattern' in specified file types + actual_count=$(rg "${rg_args[@]}" "$pattern" ./ | wc -l) + echo "Actual count of '$pattern': $actual_count" + + # Compare with the allowed count + if [ "$actual_count" -eq "$allowed_count" ]; then + echo "The count matches the allowed number of suppressions." + elif [ "$actual_count" -lt "$allowed_count" ]; then + echo "The actual count is less than $allowed_count. Consider further reducing the allowed count." + exit 1 + else + echo "Warning: The actual count is higher than $allowed_count. Please review and update suppressions or adjust the allowed count." + rg "${rg_args[@]}" "$pattern" ./ + exit 1 + fi +} + +for pattern_limit in "${patternLimits[@]}"; do + pattern=$(echo "$pattern_limit" | cut -d: -f1) + allowed_count=$(echo "$pattern_limit" | cut -d: -f2) + check_pattern_count "$pattern" "$allowed_count" +done diff --git a/tests/crosschain_test.go b/tests/crosschain_test.go index 919eede0..72dfff37 100644 --- a/tests/crosschain_test.go +++ b/tests/crosschain_test.go @@ -4,7 +4,7 @@ import ( crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types" ) -func (suite *IntegrationTest) CrossChainTest() { +func (suite *IntegrationTest) CrosschainTest() { } func (suite *IntegrationTest) UpdateParamsTest() { diff --git a/tests/integration_test.go b/tests/integration_test.go index 081d82d6..a8e959a7 100644 --- a/tests/integration_test.go +++ b/tests/integration_test.go @@ -65,7 +65,7 @@ func TestIntegrationTest(t *testing.T) { } func (suite *IntegrationTest) TestRun() { - suite.CrossChainTest() + suite.CrosschainTest() suite.StakingTest() suite.StakingContractTest() @@ -89,7 +89,7 @@ func (suite *IntegrationTest) TestRun() { suite.ByPassFeeTest() } -func (suite *IntegrationTest) GetCrossChainByName(chainName string) CrosschainTestSuite { +func (suite *IntegrationTest) GetByName(chainName string) CrosschainTestSuite { for _, c := range suite.crosschain { if c.chainName == chainName { return c diff --git a/tests/precompile_suite.go b/tests/precompile_suite.go index 769bc2dd..f438db6f 100644 --- a/tests/precompile_suite.go +++ b/tests/precompile_suite.go @@ -35,7 +35,7 @@ func (suite *PrecompileTestSuite) HexAddress() common.Address { return common.BytesToAddress(suite.privKey.PubKey().Address()) } -func (suite *PrecompileTestSuite) TransferCrossChain(token common.Address, recipient string, amount, fee *big.Int, target string) *ethtypes.Transaction { +func (suite *PrecompileTestSuite) TransferCrosschain(token common.Address, recipient string, amount, fee *big.Int, target string) *ethtypes.Transaction { privateKey := suite.privKey beforeBalanceOf := suite.BalanceOf(token, common.BytesToAddress(privateKey.PubKey().Address().Bytes())) pack, err := contract.GetFIP20().ABI.Pack("transferCrossChain", recipient, amount, fee, fxtypes.MustStrToByte32(target)) @@ -48,7 +48,7 @@ func (suite *PrecompileTestSuite) TransferCrossChain(token common.Address, recip return ethTx } -func (suite *PrecompileTestSuite) CrossChainAndResponse(token common.Address, recipient string, amount, fee *big.Int, target string) *ethtypes.Transaction { +func (suite *PrecompileTestSuite) CrosschainAndResponse(token common.Address, recipient string, amount, fee *big.Int, target string) *ethtypes.Transaction { privateKey := suite.privKey crosschainContract := crosschaintypes.GetAddress() suite.ApproveERC20(privateKey, token, crosschainContract, big.NewInt(0).Add(amount, fee))