-
Notifications
You must be signed in to change notification settings - Fork 5k
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
base: 4.x
Are you sure you want to change the base?
Changes from all commits
7ba06d7
0840d4e
b921d4f
61f83d8
9182369
d60117f
88d4f40
fdc67e9
e9f8f08
c48f5ce
b2dc1a7
6b448d3
d36991e
64bd3ad
5e63587
7895796
abb48c1
ea7013b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @whitemoshui There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} |
There was a problem hiding this comment.
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