From 7ba06d7a781f942963d7874ff8e0a2deac9565eb Mon Sep 17 00:00:00 2001 From: whitemoshui Date: Fri, 8 Nov 2024 15:54:49 +0800 Subject: [PATCH 01/11] add changelog tests in pre-commit --- .husky/pre-commit | 2 ++ package.json | 3 +- scripts/check_changelog.sh | 69 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100755 scripts/check_changelog.sh diff --git a/.husky/pre-commit b/.husky/pre-commit index d24fdfc601b..839ae259b14 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -2,3 +2,5 @@ . "$(dirname -- "$0")/_/husky.sh" npx lint-staged + +npm run check-changelog \ No newline at end of file diff --git a/package.json b/package.json index 8e741afb9f6..647b2da4bbf 100644 --- a/package.json +++ b/package.json @@ -99,7 +99,8 @@ "postinstall": "yarn build", "compile:contracts": "node ./scripts/compile_contracts.js && yarn format && yarn lint:fix", "publish:canary": "lerna publish --canary --dist-tag dev --preid dev.$(git rev-parse --short HEAD) --exact --graph-type all --force-publish \"*\" --no-verify-access --yes", - "prepare": "husky install" + "prepare": "husky install", + "check-changelog": "bash ./scripts/check_changelog.sh" }, "devDependencies": { "@cypress/webpack-preprocessor": "^5.12.0", diff --git a/scripts/check_changelog.sh b/scripts/check_changelog.sh new file mode 100755 index 00000000000..3c357f0f182 --- /dev/null +++ b/scripts/check_changelog.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bash + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' # No Color + +# Get all staged files +STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR) + +# Initialize arrays to track packages with code changes and changelog updates +declare -A packages_with_code_changes +declare -A packages_with_changelog_updates + +# Function to get package name from file path +get_package_name() { + local file_path=$1 + # Assuming packages are in packages/package-name structure + echo $file_path | grep -o "packages/[^/]*" | cut -d'/' -f2 +} + +# Function to check if file is a code file +is_code_file() { + local file=$1 + # Add or modify extensions based on your project needs + [[ $file =~ \.(js|jsx|ts|tsx|css)$ ]] +} + +# Scan all staged files +for file in $STAGED_FILES; do + # Skip if file doesn't exist + [ ! -f "$file" ] && continue + + # Get package name + package_name=$(get_package_name "$file") + + # Skip if not in a package directory + [ -z "$package_name" ] && continue + + # Check if it's a code file + if is_code_file "$file"; then + packages_with_code_changes[$package_name]=1 + fi + + # Check if it's a changelog file + if [[ $file =~ CHANGELOG\.md$ ]]; then + packages_with_changelog_updates[$package_name]=1 + fi +done + +# Check if packages with code changes have changelog updates +error_found=0 +for package in "${!packages_with_code_changes[@]}"; do + if [ -z "${packages_with_changelog_updates[$package]}" ]; then + echo -e "${RED}Error: Package '$package' has code changes but no CHANGELOG.md update${NC}" + error_found=1 + else + echo -e "${GREEN}✓ Package '$package' has both code changes and CHANGELOG.md update${NC}" + fi +done + +# Exit with error if any package is missing changelog updates +if [ $error_found -eq 1 ]; then + echo -e "\n${RED}Commit rejected: Please update packages/web3-xxx/CHANGELOG.md for all modified packages${NC}" + exit 1 +else + echo -e "\n${GREEN}All package changes have corresponding changelog updates${NC}" + exit 0 +fi \ No newline at end of file From 0840d4ece5e38bc6dc088f1ae31fcabd08fcda04 Mon Sep 17 00:00:00 2001 From: whitemoshui Date: Fri, 8 Nov 2024 16:01:35 +0800 Subject: [PATCH 02/11] switch to js scripts --- package.json | 2 +- scripts/check_changelog.js | 89 ++++++++++++++++++++++++++++++++++++++ scripts/check_changelog.sh | 69 ----------------------------- 3 files changed, 90 insertions(+), 70 deletions(-) create mode 100644 scripts/check_changelog.js delete mode 100755 scripts/check_changelog.sh diff --git a/package.json b/package.json index 647b2da4bbf..f0f27ebc9cc 100644 --- a/package.json +++ b/package.json @@ -100,7 +100,7 @@ "compile:contracts": "node ./scripts/compile_contracts.js && yarn format && yarn lint:fix", "publish:canary": "lerna publish --canary --dist-tag dev --preid dev.$(git rev-parse --short HEAD) --exact --graph-type all --force-publish \"*\" --no-verify-access --yes", "prepare": "husky install", - "check-changelog": "bash ./scripts/check_changelog.sh" + "check-changelog": "node ./scripts/check_changelog.js" }, "devDependencies": { "@cypress/webpack-preprocessor": "^5.12.0", diff --git a/scripts/check_changelog.js b/scripts/check_changelog.js new file mode 100644 index 00000000000..e58aeecee80 --- /dev/null +++ b/scripts/check_changelog.js @@ -0,0 +1,89 @@ +const { execSync } = require('child_process'); +const path = require('path'); +const fs = require('fs'); + +// Colors for output +const colors = { + red: text => `\x1b[31m${text}\x1b[0m`, + green: text => `\x1b[32m${text}\x1b[0m`, + yellow: text => `\x1b[33m${text}\x1b[0m`, +}; + +try { + // Get all staged files + const stagedFiles = execSync('git diff --cached --name-only', { encoding: 'utf-8' }) + .trim() + .split('\n') + .filter(Boolean); + + // Initialize map to track packages with code changes and changelog updates + const packagesWithChanges = new Map(); + + // Scan all staged files + stagedFiles.forEach(file => { + // Get package name + const packageMatch = file.match(/packages\/([^/]+)/); + if (!packageMatch) return; + + const packageName = packageMatch[1]; + + // Check if it's a code file + const isCodeFile = /\.(js|jsx|ts|tsx|css|json)$/.test(file); + // Check if it's a changelog file + const isChangelog = file.endsWith('CHANGELOG.md'); + + if (!packagesWithChanges.has(packageName)) { + packagesWithChanges.set(packageName, { + hasCodeChanges: false, + hasChangelogUpdate: false, + }); + } + + const packageInfo = packagesWithChanges.get(packageName); + + if (isCodeFile && !isChangelog) { + packageInfo.hasCodeChanges = true; + } + + if (isChangelog) { + packageInfo.hasChangelogUpdate = true; + } + }); + + // Check if packages with code changes have changelog updates + let hasError = false; + + for (const [packageName, info] of packagesWithChanges) { + if (info.hasCodeChanges) { + if (!info.hasChangelogUpdate) { + console.log( + colors.red( + `Error: Package '${packageName}' has code changes but no CHANGELOG.md update`, + ), + ); + hasError = true; + } else { + console.log( + colors.green( + `✓ Package '${packageName}' has both code changes and CHANGELOG.md update`, + ), + ); + } + } + } + + if (hasError) { + console.log( + colors.red( + '\nCommit rejected: Please update packages/web3-xxx/CHANGELOG.md for all modified packages', + ), + ); + process.exit(1); + } else if (packagesWithChanges.size > 0) { + console.log(colors.green('\nAll package changes have corresponding changelog updates')); + } +} catch (error) { + console.error(colors.red('Error executing script:'), error); + // Skip this check and return 0 if something goes wrong + process.exit(0); +} diff --git a/scripts/check_changelog.sh b/scripts/check_changelog.sh deleted file mode 100755 index 3c357f0f182..00000000000 --- a/scripts/check_changelog.sh +++ /dev/null @@ -1,69 +0,0 @@ -#!/usr/bin/env bash - -# Colors for output -RED='\033[0;31m' -GREEN='\033[0;32m' -NC='\033[0m' # No Color - -# Get all staged files -STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR) - -# Initialize arrays to track packages with code changes and changelog updates -declare -A packages_with_code_changes -declare -A packages_with_changelog_updates - -# Function to get package name from file path -get_package_name() { - local file_path=$1 - # Assuming packages are in packages/package-name structure - echo $file_path | grep -o "packages/[^/]*" | cut -d'/' -f2 -} - -# Function to check if file is a code file -is_code_file() { - local file=$1 - # Add or modify extensions based on your project needs - [[ $file =~ \.(js|jsx|ts|tsx|css)$ ]] -} - -# Scan all staged files -for file in $STAGED_FILES; do - # Skip if file doesn't exist - [ ! -f "$file" ] && continue - - # Get package name - package_name=$(get_package_name "$file") - - # Skip if not in a package directory - [ -z "$package_name" ] && continue - - # Check if it's a code file - if is_code_file "$file"; then - packages_with_code_changes[$package_name]=1 - fi - - # Check if it's a changelog file - if [[ $file =~ CHANGELOG\.md$ ]]; then - packages_with_changelog_updates[$package_name]=1 - fi -done - -# Check if packages with code changes have changelog updates -error_found=0 -for package in "${!packages_with_code_changes[@]}"; do - if [ -z "${packages_with_changelog_updates[$package]}" ]; then - echo -e "${RED}Error: Package '$package' has code changes but no CHANGELOG.md update${NC}" - error_found=1 - else - echo -e "${GREEN}✓ Package '$package' has both code changes and CHANGELOG.md update${NC}" - fi -done - -# Exit with error if any package is missing changelog updates -if [ $error_found -eq 1 ]; then - echo -e "\n${RED}Commit rejected: Please update packages/web3-xxx/CHANGELOG.md for all modified packages${NC}" - exit 1 -else - echo -e "\n${GREEN}All package changes have corresponding changelog updates${NC}" - exit 0 -fi \ No newline at end of file From b921d4f8700665cb69d38af6bbc91dec06e031fe Mon Sep 17 00:00:00 2001 From: whitemoshui Date: Fri, 8 Nov 2024 16:53:46 +0800 Subject: [PATCH 03/11] remove yellow color --- scripts/check_changelog.js | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/check_changelog.js b/scripts/check_changelog.js index e58aeecee80..e4110e192c4 100644 --- a/scripts/check_changelog.js +++ b/scripts/check_changelog.js @@ -6,7 +6,6 @@ const fs = require('fs'); const colors = { red: text => `\x1b[31m${text}\x1b[0m`, green: text => `\x1b[32m${text}\x1b[0m`, - yellow: text => `\x1b[33m${text}\x1b[0m`, }; try { From 61f83d88f58e9949cee85ddbc6e770fc853a16a0 Mon Sep 17 00:00:00 2001 From: whitemoshui Date: Fri, 8 Nov 2024 17:05:12 +0800 Subject: [PATCH 04/11] update the note of the PR template --- .github/PULL_REQUEST_TEMPLATE.md | 34 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 7ad07339c5c..8c90cc202f6 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -11,23 +11,23 @@ Fixes #(issue) -- [ ] Bug fix (non-breaking change which fixes an issue) -- [ ] New feature (non-breaking change which adds functionality) -- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) +- [ ] Bug fix (non-breaking change which fixes an issue) +- [ ] New feature (non-breaking change which adds functionality) +- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) ## Checklist: -- [ ] I have selected the correct base branch. -- [ ] I have performed a self-review of my own code. -- [ ] I have commented my code, particularly in hard-to-understand areas. -- [ ] I have made corresponding changes to the documentation. -- [ ] My changes generate no new warnings. -- [ ] Any dependent changes have been merged and published in downstream modules. -- [ ] I ran `npm run lint` with success and extended the tests and types if necessary. -- [ ] I ran `npm run test:unit` with success. -- [ ] I ran `npm run test:coverage` and my test cases cover all the lines and branches of the added code. -- [ ] I ran `npm run build` and tested `dist/web3.min.js` in a browser. -- [ ] I have tested my code on the live network. -- [ ] I have checked the Deploy Preview and it looks correct. -- [ ] I have updated the `CHANGELOG.md` file in the root folder. -- [ ] I have linked Issue(s) with this PR in "Linked Issues" menu. +- [ ] I have selected the correct base branch. +- [ ] I have performed a self-review of my own code. +- [ ] I have commented my code, particularly in hard-to-understand areas. +- [ ] I have made corresponding changes to the documentation. +- [ ] My changes generate no new warnings. +- [ ] Any dependent changes have been merged and published in downstream modules. +- [ ] I ran `npm run lint` with success and extended the tests and types if necessary. +- [ ] I ran `npm run test:unit` with success. +- [ ] I ran `npm run test:coverage` and my test cases cover all the lines and branches of the added code. +- [ ] I ran `npm run build` and tested `dist/web3.min.js` in a browser. +- [ ] I have tested my code on the live network. +- [ ] I have checked the Deploy Preview and it looks correct. +- [ ] I have updated the `CHANGELOG.md` file in both the root folder and corresponding packages/web3-xxx folders. +- [ ] I have linked Issue(s) with this PR in "Linked Issues" menu. From 88d4f40ff3f419bc46edd550a46a195568ef781a Mon Sep 17 00:00:00 2001 From: whitemoshui Date: Mon, 18 Nov 2024 20:52:14 +0800 Subject: [PATCH 05/11] refine log --- scripts/check_changelog.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/check_changelog.js b/scripts/check_changelog.js index e4110e192c4..6bcf3985fff 100644 --- a/scripts/check_changelog.js +++ b/scripts/check_changelog.js @@ -74,7 +74,7 @@ try { if (hasError) { console.log( colors.red( - '\nCommit rejected: Please update packages/web3-xxx/CHANGELOG.md for all modified packages', + '\nCommit rejected: Please update the CHANGELOG.md in the corresponding package folder for each of the aforementioned packages.', ), ); process.exit(1); From e9f8f0827915176856b8c0fb4d218d875495f696 Mon Sep 17 00:00:00 2001 From: whitemoshui Date: Tue, 26 Nov 2024 09:36:29 +0800 Subject: [PATCH 06/11] just check ts/json and exclude test files --- scripts/check_changelog.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/check_changelog.js b/scripts/check_changelog.js index 6bcf3985fff..bf6fc49984c 100644 --- a/scripts/check_changelog.js +++ b/scripts/check_changelog.js @@ -26,8 +26,8 @@ try { const packageName = packageMatch[1]; - // Check if it's a code file - const isCodeFile = /\.(js|jsx|ts|tsx|css|json)$/.test(file); + // Check if it's a code file (excluding test files) + const isCodeFile = /\.(ts|json)$/.test(file) && !file.includes('/test/'); // Check if it's a changelog file const isChangelog = file.endsWith('CHANGELOG.md'); From d36991ef3ab061bc8a91dcc133635531be940c0a Mon Sep 17 00:00:00 2001 From: whitemoshui Date: Thu, 5 Dec 2024 14:47:41 +0800 Subject: [PATCH 07/11] support --skip-changelog --- .husky/pre-commit | 6 +++++- .husky/prepare-commit-msg | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100755 .husky/prepare-commit-msg diff --git a/.husky/pre-commit b/.husky/pre-commit index 839ae259b14..0250ac3dd89 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -3,4 +3,8 @@ npx lint-staged -npm run check-changelog \ No newline at end of file +if [ "$SKIP_CHANGELOG" = "true" ]; then + echo "Skipping changelog check..." +else + npm run check-changelog +fi \ No newline at end of file diff --git a/.husky/prepare-commit-msg b/.husky/prepare-commit-msg new file mode 100755 index 00000000000..9366210a0d4 --- /dev/null +++ b/.husky/prepare-commit-msg @@ -0,0 +1,9 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +commit_msg_file=$1 +commit_msg=$(cat "$commit_msg_file") + +if echo "$commit_msg" | grep -qE "(--skip-changelog|--sc)$"; then + export SKIP_CHANGELOG=true +fi \ No newline at end of file From 64bd3ad2bf63a6294cd6122d7111f71f62fcada4 Mon Sep 17 00:00:00 2001 From: whitemoshui Date: Thu, 5 Dec 2024 14:50:48 +0800 Subject: [PATCH 08/11] support --skip-changelog --- .husky/pre-commit | 2 +- .husky/prepare-commit-msg | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index 0250ac3dd89..3a908b8ce63 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -3,7 +3,7 @@ npx lint-staged -if [ "$SKIP_CHANGELOG" = "true" ]; then +if [ "$(cat .husky/.skip-changelog 2>/dev/null)" = "true" ]; then echo "Skipping changelog check..." else npm run check-changelog diff --git a/.husky/prepare-commit-msg b/.husky/prepare-commit-msg index 9366210a0d4..5e2132de048 100755 --- a/.husky/prepare-commit-msg +++ b/.husky/prepare-commit-msg @@ -5,5 +5,7 @@ commit_msg_file=$1 commit_msg=$(cat "$commit_msg_file") if echo "$commit_msg" | grep -qE "(--skip-changelog|--sc)$"; then - export SKIP_CHANGELOG=true + echo "true" > .husky/.skip-changelog +else + echo "false" > .husky/.skip-changelog fi \ No newline at end of file From 5e63587ee4fddd727c6561cf36a51e80710fd875 Mon Sep 17 00:00:00 2001 From: whitemoshui Date: Thu, 5 Dec 2024 14:52:50 +0800 Subject: [PATCH 09/11] support --skip-changelog --- .husky/{prepare-commit-msg => commit-msg} | 4 ++-- .husky/pre-commit | 8 +------- 2 files changed, 3 insertions(+), 9 deletions(-) rename .husky/{prepare-commit-msg => commit-msg} (68%) diff --git a/.husky/prepare-commit-msg b/.husky/commit-msg similarity index 68% rename from .husky/prepare-commit-msg rename to .husky/commit-msg index 5e2132de048..d7f876b592c 100755 --- a/.husky/prepare-commit-msg +++ b/.husky/commit-msg @@ -5,7 +5,7 @@ commit_msg_file=$1 commit_msg=$(cat "$commit_msg_file") if echo "$commit_msg" | grep -qE "(--skip-changelog|--sc)$"; then - echo "true" > .husky/.skip-changelog + echo "Skipping changelog check..." else - echo "false" > .husky/.skip-changelog + npm run check-changelog fi \ No newline at end of file diff --git a/.husky/pre-commit b/.husky/pre-commit index 3a908b8ce63..0312b76025e 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,10 +1,4 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" -npx lint-staged - -if [ "$(cat .husky/.skip-changelog 2>/dev/null)" = "true" ]; then - echo "Skipping changelog check..." -else - npm run check-changelog -fi \ No newline at end of file +npx lint-staged \ No newline at end of file From 7895796b0d93d5211ca887ee1cc5ed6f404771f1 Mon Sep 17 00:00:00 2001 From: whitemoshui Date: Thu, 5 Dec 2024 15:03:50 +0800 Subject: [PATCH 10/11] update Contributing.md --sc --- .github/CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index ad7ab9572d2..af12fe734e1 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -43,7 +43,7 @@ yarn 7. Check changes in the local environment: Run the command `yarn start` and you'll see a local environment in `localhost:3000` with the documents. -8. **Commit your changes:** `git add .` and `git commit -m 'descriptive msg'` +8. **Commit your changes:** `git add .` and `git commit -m 'descriptive msg'`. You can append `--sc` or `--skip-changelog` to your commit message to skip the changelog check, e.g. `git commit -m 'descriptive msg --sc'` 9. **Push your changes:** From abb48c1ff5d357e4f4e8e91c3042761091547c5b Mon Sep 17 00:00:00 2001 From: whitemoshui Date: Thu, 5 Dec 2024 15:04:54 +0800 Subject: [PATCH 11/11] recover pre-commit --- .husky/pre-commit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index 0312b76025e..d24fdfc601b 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,4 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" -npx lint-staged \ No newline at end of file +npx lint-staged