From 0f7cd32de5061819cd8fa2ae3d56c854da5a51d0 Mon Sep 17 00:00:00 2001 From: Anh Nguyen Date: Fri, 8 Mar 2024 10:20:55 +0700 Subject: [PATCH 1/9] [ci] add test step --- .github/scripts/contract-test-mapping.json | 5 ++ .github/scripts/detect_and_test_changes.sh | 46 +++++++++++++++++ .github/workflows/test.yaml | 57 ++++++++++++++++++++++ package.json | 1 + truffle-config.js | 4 ++ 5 files changed, 113 insertions(+) create mode 100644 .github/scripts/contract-test-mapping.json create mode 100755 .github/scripts/detect_and_test_changes.sh create mode 100644 .github/workflows/test.yaml diff --git a/.github/scripts/contract-test-mapping.json b/.github/scripts/contract-test-mapping.json new file mode 100644 index 0000000..0bd1e18 --- /dev/null +++ b/.github/scripts/contract-test-mapping.json @@ -0,0 +1,5 @@ +{ + "contracts/FeralFileAirdropV1.sol": ["test/feralfile_airdrop_v1.js"], + "contracts/FeralfileArtworkV3_3.sol": ["test/feralfile_exhibition_v3_3.js"], + "contracts/OwnerData.sol": ["test/owner_data.js"] +} \ No newline at end of file diff --git a/.github/scripts/detect_and_test_changes.sh b/.github/scripts/detect_and_test_changes.sh new file mode 100755 index 0000000..f083662 --- /dev/null +++ b/.github/scripts/detect_and_test_changes.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +# Fetch target branch +TARGET_BRANCH=$1 + +# Fetch changes +git fetch origin $TARGET_BRANCH +CHANGED_FILES=$(git diff --name-only FETCH_HEAD $(git merge-base FETCH_HEAD $TARGET_BRANCH) | grep '.sol$') +echo "Changed files: $CHANGED_FILES" + +# Read the mapping file +declare -A MAPPING + +# Use jq to iterate over each key-value pair in the JSON object. +while IFS= read -r line; do + contract=$(jq -r '.contract' <<< "$line") + IFS=$'\n' read -r -d '' -a tests < <(jq -r '.tests[]' <<< "$line" && printf '\0') + + # Add each test to the mapping for the contract. + for test in "${tests[@]}"; do + MAPPING["$contract"]+="$test " + done +done < <(jq -c 'to_entries[] | {contract: .key, tests: .value}' .github/scripts/contract-test-mapping.json) + + +# Initialize a variable to hold test files to run +TEST_FILES="" + +# Loop through changed files and append relevant test files +for FILE in $CHANGED_FILES; do + if [[ -n "${MAPPING[$FILE]}" ]]; then + TEST_FILES+="${MAPPING[$FILE]}" + fi +done + +# Remove duplicate test files +TEST_FILES=$(echo "$TEST_FILES" | tr ' ' '\n' | sort -u | tr '\n' ' ') + +# Run Truffle tests if there are any test files to run +if [ -n "$TEST_FILES" ]; then + echo "Running tests for changed contracts:" + echo $TEST_FILES + truffle test $TEST_FILES | tee test-output.txt +else + echo "No contract changes detected." +fi \ No newline at end of file diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..b7b3ae0 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,57 @@ +name: Solidity Test + +on: + pull_request: + branches: [ main ] + paths: + - '**/*.sol' + +jobs: + solidity-lint: + name: Lint Solidity + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Node.js + uses: actions/setup-node@v1 + with: + node-version: '18' + + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v42 + + - name: Write secret.json file + env: + MNEMONIC: ${{ secrets.MNEMONIC }} + MAINNET_ENDPOINT: ${{ secrets.MAINNET_ENDPOINT }} + GOERLI_ENDPOINT: ${{ secrets.GOERLI_ENDPOINT }} + BSCTEST_ENDPOINT: ${{ secrets.BSCTEST_ENDPOINT }} + BSC_ENDPOINT: ${{ secrets.BSC_ENDPOINT }} + ETHERSCAN_API: ${{ secrets.ETHERSCAN_API }} + BSC_API: ${{ secrets.BSC_API }} + run: | + cat < .secret.json + { + "mnemonic": "${MNEMONIC}", + "mainnet_endpoint": "${MAINNET_ENDPOINT}", + "goerli_endpoint": "${GOERLI_ENDPOINT}", + "bsctest_endpoint": "${BSCTEST_ENDPOINT}", + "bsc_endpoint": "${BSC_ENDPOINT}", + "etherscan_api": "${ETHERSCAN_API}", + "bsc_api": "${BSC_API}" + } + EOF + + - name: Test + env: + CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} + TARGET_BRANCH: ${{ github.base_ref }} + run: | + npm install + .github/scripts/detect_and_test_changes.sh + \ No newline at end of file diff --git a/package.json b/package.json index b94e6ee..6dbfadd 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "devDependencies": { "@openzeppelin/test-helpers": "^0.5.16", "coveralls": "^3.1.1", + "eth-gas-reporter": "^0.2.27", "ganache-cli": "^6.12.2", "solidity-coverage": "^0.7.22", "truffle": "^5.9.2", diff --git a/truffle-config.js b/truffle-config.js index d5cbe72..656e9f2 100644 --- a/truffle-config.js +++ b/truffle-config.js @@ -106,6 +106,10 @@ module.exports = { // Set default mocha options here, use special reporters etc. mocha: { // timeout: 100000 + reporter: 'eth-gas-reporter', + reporterOptions : { + currency: 'USD', + } }, // Configure your compilers From 8133397add645589bb170796aad471943ca20b9c Mon Sep 17 00:00:00 2001 From: Anh Nguyen Date: Fri, 8 Mar 2024 10:36:53 +0700 Subject: [PATCH 2/9] [ci] install dependecies --- .github/workflows/test.yaml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index b7b3ae0..5899059 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -25,6 +25,15 @@ jobs: id: changed-files uses: tj-actions/changed-files@v42 + - name: Install dependencies + env: + CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} + TARGET_BRANCH: ${{ github.base_ref }} + run: | + npm install + npm install -g truffle ganache + ganache -D -p 7545 + - name: Write secret.json file env: MNEMONIC: ${{ secrets.MNEMONIC }} @@ -52,6 +61,5 @@ jobs: CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} TARGET_BRANCH: ${{ github.base_ref }} run: | - npm install .github/scripts/detect_and_test_changes.sh \ No newline at end of file From 0fc3a6c09bb53d5fccb7c3e4ccdd513e72f9991a Mon Sep 17 00:00:00 2001 From: Anh Nguyen Date: Fri, 8 Mar 2024 11:03:48 +0700 Subject: [PATCH 3/9] [ci] update the coinmarketcap API --- .github/workflows/test.yaml | 1 + truffle-config.js | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 5899059..a000311 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -60,6 +60,7 @@ jobs: env: CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} TARGET_BRANCH: ${{ github.base_ref }} + COINMARKETCAP_API_KEY: ${{ secrets.COIN_MARKETCAP_API_KEY }} run: | .github/scripts/detect_and_test_changes.sh \ No newline at end of file diff --git a/truffle-config.js b/truffle-config.js index 656e9f2..f38bf35 100644 --- a/truffle-config.js +++ b/truffle-config.js @@ -109,6 +109,7 @@ module.exports = { reporter: 'eth-gas-reporter', reporterOptions : { currency: 'USD', + coinmarketcap: process.env.COINMARKETCAP_API_KEY } }, From 81e04c40ff2af4f56dfaf466f3e5f7bc5f770291 Mon Sep 17 00:00:00 2001 From: Anh Nguyen Date: Fri, 8 Mar 2024 11:08:38 +0700 Subject: [PATCH 4/9] [ci] add gas price api --- truffle-config.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/truffle-config.js b/truffle-config.js index f38bf35..9035a6e 100644 --- a/truffle-config.js +++ b/truffle-config.js @@ -109,6 +109,8 @@ module.exports = { reporter: 'eth-gas-reporter', reporterOptions : { currency: 'USD', + gasPriceApi: 'https://api.etherscan.io/api?module=proxy&action=eth_gasPrice', + token: 'ETH', coinmarketcap: process.env.COINMARKETCAP_API_KEY } }, From 7f9002200e618e037b39aa4e58ca2239c3a9d866 Mon Sep 17 00:00:00 2001 From: Anh Nguyen Date: Fri, 8 Mar 2024 11:23:10 +0700 Subject: [PATCH 5/9] [ci] remove output --- .github/scripts/detect_and_test_changes.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/scripts/detect_and_test_changes.sh b/.github/scripts/detect_and_test_changes.sh index f083662..cb0eb77 100755 --- a/.github/scripts/detect_and_test_changes.sh +++ b/.github/scripts/detect_and_test_changes.sh @@ -40,7 +40,7 @@ TEST_FILES=$(echo "$TEST_FILES" | tr ' ' '\n' | sort -u | tr '\n' ' ') if [ -n "$TEST_FILES" ]; then echo "Running tests for changed contracts:" echo $TEST_FILES - truffle test $TEST_FILES | tee test-output.txt + truffle test $TEST_FILES else echo "No contract changes detected." fi \ No newline at end of file From e8d11b5e1d2eaaa64b66dfc06fa4ab08955af184 Mon Sep 17 00:00:00 2001 From: Anh Nguyen Date: Fri, 8 Mar 2024 11:40:29 +0700 Subject: [PATCH 6/9] [ci] tweak the sh file --- .github/scripts/detect_and_test_changes.sh | 14 +++++++------- .github/workflows/test.yaml | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/scripts/detect_and_test_changes.sh b/.github/scripts/detect_and_test_changes.sh index cb0eb77..8742835 100755 --- a/.github/scripts/detect_and_test_changes.sh +++ b/.github/scripts/detect_and_test_changes.sh @@ -1,11 +1,5 @@ #!/bin/bash -# Fetch target branch -TARGET_BRANCH=$1 - -# Fetch changes -git fetch origin $TARGET_BRANCH -CHANGED_FILES=$(git diff --name-only FETCH_HEAD $(git merge-base FETCH_HEAD $TARGET_BRANCH) | grep '.sol$') echo "Changed files: $CHANGED_FILES" # Read the mapping file @@ -15,6 +9,12 @@ declare -A MAPPING while IFS= read -r line; do contract=$(jq -r '.contract' <<< "$line") IFS=$'\n' read -r -d '' -a tests < <(jq -r '.tests[]' <<< "$line" && printf '\0') + + # Check if tests array is empty + if [ ${#tests[@]} -eq 0 ]; then + echo "Error: No tests found for contract $contract." + exit 1 + fi # Add each test to the mapping for the contract. for test in "${tests[@]}"; do @@ -40,7 +40,7 @@ TEST_FILES=$(echo "$TEST_FILES" | tr ' ' '\n' | sort -u | tr '\n' ' ') if [ -n "$TEST_FILES" ]; then echo "Running tests for changed contracts:" echo $TEST_FILES - truffle test $TEST_FILES + COINMARKETCAP_API_KEY=$COINMARKETCAP_API_KEY truffle test $TEST_FILES else echo "No contract changes detected." fi \ No newline at end of file diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index a000311..e9ca8e1 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -8,7 +8,7 @@ on: jobs: solidity-lint: - name: Lint Solidity + name: Test Solidity runs-on: ubuntu-latest steps: - name: Check out code From a825236ad7155b8cf617cd285a94f99fe388a0c6 Mon Sep 17 00:00:00 2001 From: Anh Nguyen Date: Fri, 8 Mar 2024 11:45:48 +0700 Subject: [PATCH 7/9] [ci] update files --- .github/scripts/detect_and_test_changes.sh | 3 ++- .github/workflows/test.yaml | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/scripts/detect_and_test_changes.sh b/.github/scripts/detect_and_test_changes.sh index 8742835..ea265dc 100755 --- a/.github/scripts/detect_and_test_changes.sh +++ b/.github/scripts/detect_and_test_changes.sh @@ -40,7 +40,8 @@ TEST_FILES=$(echo "$TEST_FILES" | tr ' ' '\n' | sort -u | tr '\n' ' ') if [ -n "$TEST_FILES" ]; then echo "Running tests for changed contracts:" echo $TEST_FILES - COINMARKETCAP_API_KEY=$COINMARKETCAP_API_KEY truffle test $TEST_FILES + echo $COINMARKETCAP_API_KEY + truffle test $TEST_FILES else echo "No contract changes detected." fi \ No newline at end of file diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e9ca8e1..bba71df 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -61,6 +61,5 @@ jobs: CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} TARGET_BRANCH: ${{ github.base_ref }} COINMARKETCAP_API_KEY: ${{ secrets.COIN_MARKETCAP_API_KEY }} - run: | - .github/scripts/detect_and_test_changes.sh + run: .github/scripts/detect_and_test_changes.sh \ No newline at end of file From 17e7af80da8b9ae5ac763227d52f939b9ec7231f Mon Sep 17 00:00:00 2001 From: Anh Nguyen Date: Fri, 8 Mar 2024 15:09:12 +0700 Subject: [PATCH 8/9] [ci] try to reveal the api key --- .github/scripts/detect_and_test_changes.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/scripts/detect_and_test_changes.sh b/.github/scripts/detect_and_test_changes.sh index ea265dc..9aff776 100755 --- a/.github/scripts/detect_and_test_changes.sh +++ b/.github/scripts/detect_and_test_changes.sh @@ -1,6 +1,7 @@ #!/bin/bash echo "Changed files: $CHANGED_FILES" +echo "API KEY: $COINMARKETCAP_API_KEY" # Read the mapping file declare -A MAPPING @@ -40,7 +41,6 @@ TEST_FILES=$(echo "$TEST_FILES" | tr ' ' '\n' | sort -u | tr '\n' ' ') if [ -n "$TEST_FILES" ]; then echo "Running tests for changed contracts:" echo $TEST_FILES - echo $COINMARKETCAP_API_KEY truffle test $TEST_FILES else echo "No contract changes detected." From ce3095b704a06f673c7926850a99068281b5e319 Mon Sep 17 00:00:00 2001 From: Anh Nguyen Date: Fri, 8 Mar 2024 15:14:37 +0700 Subject: [PATCH 9/9] ci: update file --- .github/scripts/detect_and_test_changes.sh | 1 - .github/workflows/test.yaml | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/scripts/detect_and_test_changes.sh b/.github/scripts/detect_and_test_changes.sh index 9aff776..fe398d3 100755 --- a/.github/scripts/detect_and_test_changes.sh +++ b/.github/scripts/detect_and_test_changes.sh @@ -1,7 +1,6 @@ #!/bin/bash echo "Changed files: $CHANGED_FILES" -echo "API KEY: $COINMARKETCAP_API_KEY" # Read the mapping file declare -A MAPPING diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index bba71df..566fb37 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -58,8 +58,7 @@ jobs: - name: Test env: + COINMARKETCAP_API_KEY: ${{ secrets.COINMARKETCAP_API_KEY }} CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} TARGET_BRANCH: ${{ github.base_ref }} - COINMARKETCAP_API_KEY: ${{ secrets.COIN_MARKETCAP_API_KEY }} - run: .github/scripts/detect_and_test_changes.sh - \ No newline at end of file + run: .github/scripts/detect_and_test_changes.sh \ No newline at end of file