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:** diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index b7a527fb7d8..65e009c2ce4 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -30,5 +30,5 @@ Fixes #(issue) - [ ] 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 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. diff --git a/.husky/commit-msg b/.husky/commit-msg new file mode 100755 index 00000000000..d7f876b592c --- /dev/null +++ b/.husky/commit-msg @@ -0,0 +1,11 @@ +#!/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 + echo "Skipping changelog check..." +else + npm run check-changelog +fi \ No newline at end of file diff --git a/package.json b/package.json index a484b558aca..f80bcd47328 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": "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..bf6fc49984c --- /dev/null +++ b/scripts/check_changelog.js @@ -0,0 +1,88 @@ +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`, +}; + +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 (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'); + + 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 the CHANGELOG.md in the corresponding package folder for each of the aforementioned 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); +}