Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dev): add changelog check into pre-commit #7377

Open
wants to merge 18 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**

Expand Down
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These spaces are added automatically by the check

- [ ] I have linked Issue(s) with this PR in "Linked Issues" menu.
11 changes: 11 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
88 changes: 88 additions & 0 deletions scripts/check_changelog.js
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Contributor

@Muhammad-Altabba Muhammad-Altabba Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@whitemoshui
What about adding a check on the commit message, such that if it contains something like --skip changelog, then the returns without checking?
This way if someone wants to explicity avoid the changelog check, he/she will add --skip changelog to the end of his commit message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me have a 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);
}
Loading