-
Notifications
You must be signed in to change notification settings - Fork 28
feat(git): setup Commitlint, Husky, and Commitizen for standardized commit messages #74
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
Conversation
WalkthroughThis change introduces commit message linting and automation to the project. It adds configuration and tooling for Commitlint, Husky, Commitizen, and GitHub Actions. The setup enforces conventional commit message standards, blocks non-compliant commits locally using Git hooks, provides guided commit prompts, and validates commit messages in pull requests via a CI workflow. The configuration is managed through new files for Commitlint rules, Husky hooks, npm scripts, and a GitHub Actions workflow. Changes
Sequence Diagram(s)sequenceDiagram
participant Developer
participant Husky (Local)
participant Commitlint (Local)
participant Commitizen (Local)
participant GitHub Actions (CI)
participant Commitlint (CI)
Developer->>Commitizen (Local): Run "npm run commit"
Commitizen (Local)->>Developer: Prompt for commit message
Developer->>Husky (Local): git commit
Husky (Local)->>Commitlint (Local): Validate commit message
Commitlint (Local)-->>Husky (Local): Pass/Fail
Husky (Local)-->>Developer: Allow/Block commit
Developer->>GitHub Actions (CI): Open/Update PR
GitHub Actions (CI)->>Commitlint (CI): Lint PR commit messages
Commitlint (CI)-->>GitHub Actions (CI): Pass/Fail
GitHub Actions (CI)-->>Developer: PR check result
Assessment against linked issues
Possibly related issues
Poem
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
🧹 Nitpick comments (6)
package.json (3)
5-8
: Add an explicit commitlint script
It can be helpful to expose a standalonecommitlint
script for manual linting outside of Git hooks or CI. Consider adding something like:"scripts": { "test": "echo \"Error: no test specified\" && exit 1", + "commitlint": "commitlint --from origin/main --to HEAD --verbose", "prepare": "husky install", "commit": "cz" }
14-18
: Confirm lint-staged file targets
You currently lint.ts
,.css
, and.html
files. If your repo also includes other types (e.g..js
,.md
), update this section so all relevant filetypes are auto-fixed.
19-27
: Consider exact version pinning for reproducibility
Using^
ranges can lead to unintended upgrades. If you need deterministic installs (e.g., in CI), you might pin to exact versions or rely fully on your lockfile..github/workflows/commit-lint.yml (2)
21-27
: Enable caching for dependencies
To speed up CI runs, consider addingcache: 'npm'
under theactions/setup-node
step or usingactions/cache
on your npm cache directory.
28-30
: Add newline at end of file
YAML parsers and linters often expect a trailing newline. Please append one at the end of the file.🧰 Tools
🪛 YAMLlint (1.35.1)
[error] 30-30: no new line character at the end of file
(new-line-at-end-of-file)
commitlint.config.js (1)
35-36
: Add newline at end of file
A trailing newline improves readability and satisfies many linters.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
package-lock.json
is excluded by!**/package-lock.json
📒 Files selected for processing (5)
.github/workflows/commit-lint.yml
(1 hunks).husky/commit-msg
(1 hunks).husky/pre-commit
(1 hunks)commitlint.config.js
(1 hunks)package.json
(1 hunks)
🧰 Additional context used
🪛 YAMLlint (1.35.1)
.github/workflows/commit-lint.yml
[error] 30-30: no new line character at the end of file
(new-line-at-end-of-file)
🔇 Additional comments (2)
.husky/pre-commit (1)
1-1
: Verify Husky hook initialization
Ensure this hook file begins with the standard shebang (#!/usr/bin/env sh
) and sourceshusky.sh
(e.g.,. "$(dirname "$0")/_/husky.sh"
) before runningnpx lint-staged
. Otherwise, the hook may not execute..husky/commit-msg (1)
1-1
: Verify commit-msg hook setup
Make sure the file includes the shebang and sourceshusky.sh
prior to invokingcommitlint
. Without these lines, the hook won’t be initialized correctly.
"prepare": "husky", | ||
"commit": "cz" |
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.
🛠️ Refactor suggestion
Ensure correct Husky installation command
The prepare
script should invoke husky install
to set up your Git hooks, rather than calling the Husky CLI without arguments.
- "prepare": "husky",
+ "prepare": "husky install",
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
"prepare": "husky", | |
"commit": "cz" | |
"prepare": "husky install", | |
"commit": "cz" |
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.
This change suggestion by coderabbitai is incorrect for Husky v9.
The project is using Husky v9.1.7, which specifically requires:
"prepare": "husky"
The suggested "prepare": "husky install"
is for Husky v8 and earlier.
From the official Husky v9 documentation:
- For v9:
npm pkg set scripts.prepare="husky"
- The
install
command is deprecated in v9
Reference: https://typicode.github.io/husky/how-to.html
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.
🧩 Analysis chain
@abhijeetw035 You're absolutely right, and thank you for the correction. I apologize for my incorrect suggestion.
Looking at the Husky v9 documentation, I see that the configuration indeed changed from previous versions:
- Husky v9:
"prepare": "husky"
(correct as you have it) - Husky v8 and earlier:
"prepare": "husky install"
(my outdated suggestion)
Your current configuration is already following the proper recommendation for Husky v9. I appreciate you sharing the reference documentation and pointing out this important distinction.
/^.^/\
Thinking...
🌐 Web query:
husky v9 prepare script documentation
💡 Result:
Husky v9 Prepare Script Documentation
Husky v9 is a modern tool for managing Git hooks in JavaScript and Node.js projects. Its "prepare" script plays a central role in automating the setup and maintenance of these hooks for every contributor.
What is the prepare
Script in Husky v9?
The prepare
script in your package.json
ensures Husky is installed and hooks are set up every time someone runs npm install
(or compatible commands with other package managers). This makes sharing Git hooks with your team seamless, as everyone gets the hooks initialized without manual steps[4][6][7].
How Husky v9 Uses the prepare
Script
-
Initialization: When you run
npx husky init
, Husky creates a.husky/
directory and adds aprepare
script to yourpackage.json
, typically:"scripts": { "prepare": "husky" }
This script tells npm (or yarn/pnpm/bun) to run Husky's setup logic after installing dependencies[4][6][7].
-
Git Hooks Path: Running the
prepare
script configures Git'score.hooksPath
to.husky/_
, letting Husky manage all Git hooks from this directory[6]. -
Automatic Setup: Every time someone installs dependencies, the
prepare
script sets up or refreshes hooks, ensuring the repo's hooks are always correct across all environments[6].
How to Set Up Husky v9 with the Prepare Script
- Install Husky
npm install --save-dev husky
- Initialize Husky
npx husky init
- This creates the
.husky/
folder, a sample pre-commit hook, and updates/adds the prepare script inpackage.json
[4][6][7].
- This creates the
- Check Your
package.json
You should see:"scripts": { "prepare": "husky" }
- Try a Commit
After runningnpm install
, make a Git commit to see hooks in action[6].
Customizing the Prepare Script
-
Monorepos or Custom Git Directories:
If your.git
directory is not in the same folder as yourpackage.json
(such as in monorepo setups), modify the prepare script with the appropriate path. Example:"scripts": { "prepare": "cd ../ && husky install frontend/.husky" }
Adjust this according to your specific directory layout[5].
-
CI/CD Environments:
If you want to prevent failures when devDependencies aren't installed (common in CI builds), use:"scripts": { "prepare": "husky || true" }
This way, the build won't fail if Husky isn't present during production deploys[6].
Summary Table
Purpose | Example Script | Notes |
---|---|---|
Default Husky setup | "prepare": "husky" | For standard Node.js projects |
Monorepo or custom git directory path | "prepare": "cd ../ && husky install frontend/.husky" | Adjust path for monorepos |
CI/CD safe installation | "prepare": "husky |
Key Points
- The
prepare
script is essential for Husky v9’s automation and team consistency. - It makes sure hooks are installed after every
npm install
without manual intervention[4][6][7]. - Customize the script as needed for your project structure, especially in monorepos[5].
Refer to Husky’s official documentation for more usage scenarios and advanced configuration[4][1].
Citations:
- 1: https://typicode.github.io/husky/
- 2: docs: confusion on v9 usage with husky init typicode/husky#1535
- 3: https://github.com/typicode/husky
- 4: https://typicode.github.io/husky/get-started.html
- 5: https://dev.to/zhangzewei/pre-commit-with-husky-lint-staged-2kcm
- 6: https://itenium.be/blog/dev-setup/git-hooks-with-husky-v9/
- 7: https://javascript.plainenglish.io/migrating-husky-to-v9-33c1f02eb3a9
@abhijeetw035 You're absolutely right, and I apologize for my incorrect suggestion. Thank you for the correction and for providing the reference documentation.
The web search confirms exactly what you stated - Husky v9 uses "prepare": "husky"
as the proper configuration in package.json, while "prepare": "husky install"
was for Husky v8 and earlier versions.
Your current configuration is correct and follows the recommended approach for Husky v9. I appreciate you taking the time to point this out and provide the proper documentation link.
/^.^/
Thinking...
✏️ Learnings added
Learnt from: abhijeetw035
PR: PSMRI/MMU-API#74
File: package.json:6-7
Timestamp: 2025-04-17T20:00:47.413Z
Learning: For Husky v9.x, the correct prepare script in package.json is `"prepare": "husky"` (without "install"). The previous `"prepare": "husky install"` syntax is for Husky v8 and earlier versions and is deprecated in v9.
Learnt from: abhijeetw035
PR: PSMRI/MMU-API#74
File: package.json:6-7
Timestamp: 2025-04-17T20:00:47.413Z
Learning: For Husky v9.x, the correct prepare script in package.json is `"prepare": "husky"` (without "install"). The previous `"prepare": "husky install"` syntax is for Husky v8 and earlier versions and is deprecated in v9. Reference: https://typicode.github.io/husky/how-to.html
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
|
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.
Actionable comments posted: 0
🧹 Nitpick comments (3)
README.md (3)
60-68
: Consider explicit Husky installation instructionRight now, running
npm ci
will trigger theprepare
script (if configured) to install Husky hooks. Some users may not have aprepare
script inpackage.json
.
It could be helpful to add a note or fallback step:+4. **If hooks do not install automatically** + - Run `npm run prepare` or `npx husky install`🧰 Tools
🪛 LanguageTool
[uncategorized] ~63-~63: The official spelling of this programming framework is “Node.js”.
Context: ...d npm** - Download and install from nodejs.org - Verify i...(NODE_JS)
77-80
: Enhance hook verification commandsFor clarity, you may want to show an exact command for verifying hooks. For example:
- Run `ls -la .husky` to confirm the presence of hook scripts.This gives users a concrete way to check.
83-97
: Add therevert
commit type for completenessConventional Commits also include a
revert
type for undone changes. Consider appending:Types include: + - `revert`: Reverting a previous commit
This will make the list comprehensive.
🧰 Tools
🪛 LanguageTool
[grammar] ~89-~89: “Bug” is a singular noun. It appears that the verb form is incorrect.
Context: ... -feat
: A new feature -fix
: A bug fix -docs
: Documentation changes - `styl...(PCT_SINGULAR_NOUN_PLURAL_VERB_AGREEMENT)
[uncategorized] ~92-~92: Loose punctuation mark.
Context: ... changes (formatting, etc.) -refactor
: Code changes that neither fix bugs nor ...(UNLIKELY_OPENING_PUNCTUATION)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
README.md
(2 hunks)
🧰 Additional context used
🪛 LanguageTool
README.md
[uncategorized] ~63-~63: The official spelling of this programming framework is “Node.js”.
Context: ...d npm** - Download and install from nodejs.org - Verify i...
(NODE_JS)
[grammar] ~89-~89: “Bug” is a singular noun. It appears that the verb form is incorrect.
Context: ... - feat
: A new feature - fix
: A bug fix - docs
: Documentation changes - `styl...
(PCT_SINGULAR_NOUN_PLURAL_VERB_AGREEMENT)
[uncategorized] ~92-~92: Loose punctuation mark.
Context: ... changes (formatting, etc.) - refactor
: Code changes that neither fix bugs nor ...
(UNLIKELY_OPENING_PUNCTUATION)
🔇 Additional comments (5)
README.md (5)
52-54
: Well-structured introduction to commit hooksThe new "## Setting Up Commit Hooks" section clearly explains the purpose and integrates nicely with existing documentation. The explanation is concise and easy to follow.
56-58
: Prerequisites section is clearListing Node.js (v14+) and npm as prerequisites aligns with the tooling requirements. No spelling or formatting issues here.
70-76
: Dependency installation instructions look goodThe
npm ci
step succinctly installs all necessary dev dependencies, including Husky and Commitlint.
99-99
: Commit validation notice is clearStating that commit messages will be automatically validated upon commit reinforces the tooling benefits.
114-114
: Approve community link formattingRemoving the extra hyphen improves the readability of the "Join Our Community" section.
@abhijeetw035 Thanks for adding the documentation. |
Yes @drtechie , will update it!! |
@drtechie , I updated AMRIT-Docs , please review |
@@ -0,0 +1 @@ | |||
npx --no -- commitlint --edit $1 No newline at end of file |
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.
@abhijeetw035 is this --no-install.
Have you missed something?
"path": "./node_modules/cz-conventional-changelog" | ||
} | ||
}, | ||
"lint-staged": { |
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.
We don't need lint-staged
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.
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.
i think lint-staged is necessary for this project as it provides pre-commit validation for Java files with Checkstyle, JavaScript/TypeScript with ESLint, and XML syntax checking.
📋 Description
JIRA ID:
GitHub Issue: Fixes PSMRI/AMRIT#83
This PR introduces commit message standardization for the
HWC-UI
project by setting up Commitlint, Husky, and Commitizen. This PR enforce a consistent commit message format across the repository to improve collaboration, readability, and enable automation (like changelogs and versioning) in the future.These tools are now fully integrated:
✅ Type of Change
ℹ️ Additional Information
Changes Overview
commitlint.config.js
with conventional commit rules..husky/commit-msg
.Commitizen
with configuration inpackage.json
for interactive commit prompts..github/workflows/commit-lint.yml
to check commit messages on PRs.package.json
andpackage-lock.json
with required dev dependencies:@commitlint/cli
@commitlint/config-conventional
commitizen
cz-conventional-changelog
Testing & Validation
npm run commit
ornpx cz
.Screenshots
Summary by CodeRabbit
New Features
Chores