Skip to content

Conversation

abhijeetw035
Copy link
Contributor

@abhijeetw035 abhijeetw035 commented Apr 17, 2025

📋 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:

  • Commitlint enforces conventional commit message structure.
  • Husky prevents non-compliant commits by using Git hooks.
  • Commitizen provides an easy, interactive CLI for making properly formatted commits.
  • GitHub Actions validate commit messages for all pull requests.

✅ Type of Change

  • 🐞 Bug fix (non-breaking change which resolves 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)
  • 🛠 Refactor (change that is neither a fix nor a new feature)
  • ⚙️ Config change (configuration file or build script updates)
  • 📚 Documentation (updates to docs or readme)
  • 🧪 Tests (adding new or updating existing tests)
  • 🎨 UI/UX (changes that affect the user interface)
  • 🚀 Performance (improves performance)
  • 🧹 Chore (miscellaneous changes that don't modify src or test files)

ℹ️ Additional Information

Changes Overview

  • Added commitlint.config.js with conventional commit rules.
  • Configured Husky to run Commitlint on every commit via .husky/commit-msg.
  • Set up Commitizen with configuration in package.json for interactive commit prompts.
  • Added GitHub Actions workflow .github/workflows/commit-lint.yml to check commit messages on PRs.
  • Updated package.json and package-lock.json with required dev dependencies:
    • @commitlint/cli
    • @commitlint/config-conventional
    • commitizen
    • cz-conventional-changelog

Testing & Validation

  • Invalid commit messages are blocked locally with clear lint errors.
  • Interactive commit flow works via npm run commit or npx cz.
  • GitHub Actions workflow correctly fails PRs with non-compliant commit messages.
  • No disruption to existing build or CI pipelines.

Screenshots

  1. Commitlint

image

  1. Commitizen

image

Summary by CodeRabbit

  • New Features

    • Introduced automated commit message linting to enforce consistent commit message standards.
    • Added pre-commit checks to lint staged files before commits are finalized.
    • Integrated tools for managing Git hooks and commit message formatting.
  • Chores

    • Added configuration files and scripts to support commit message and code quality checks.
    • Updated documentation with setup instructions for commit hooks and commit message conventions.

Copy link

coderabbitai bot commented Apr 17, 2025

Walkthrough

This 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

File(s) Change Summary
.github/workflows/commit-lint.yml Adds a GitHub Actions workflow to lint commit messages in pull requests using Commitlint.
.husky/commit-msg, .husky/pre-commit Adds Husky hooks: one to lint commit messages with Commitlint, another to lint staged files with lint-staged before committing.
commitlint.config.js Adds Commitlint configuration enforcing conventional commit rules and message formatting.
package.json Adds project metadata, npm scripts, Commitizen config, lint-staged config, and devDependencies for Commitlint, Commitizen, Husky, etc.
README.md Adds documentation on setting up commit hooks, commit message conventions, and usage instructions for contributors.

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
Loading

Assessment against linked issues

Objective Addressed Explanation
Integrate Commitlint to enforce commit message standards (#83)
Set up Husky to trigger commit-msg hooks (#83)
Add Commitizen for structured commit prompts (#83)
Configure GitHub Actions to verify commit format in Pull Requests (#83)

Possibly related issues

Poem

🐇
In the garden of code, where carrots abound,
Commitlint and Husky now hop all around.
With Commitizen prompts and CI checks so bright,
Our messages are tidy, our history is right.
No more wild commits in the patchy green—
Only the neatest notes, fit for a queen!
🥕✨


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a 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 standalone commitlint 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 adding cache: 'npm' under the actions/setup-node step or using actions/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

📥 Commits

Reviewing files that changed from the base of the PR and between 3e0b192 and 30aabc2.

⛔ 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 sources husky.sh (e.g., . "$(dirname "$0")/_/husky.sh") before running npx 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 sources husky.sh prior to invoking commitlint. Without these lines, the hook won’t be initialized correctly.

Comment on lines +6 to +7
"prepare": "husky",
"commit": "cz"
Copy link

@coderabbitai coderabbitai bot Apr 17, 2025

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.

Suggested change
"prepare": "husky",
"commit": "cz"
"prepare": "husky install",
"commit": "cz"

Copy link
Contributor Author

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

Copy link

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 a prepare script to your package.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's core.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

  1. Install Husky
    npm install --save-dev husky
  2. Initialize Husky
    npx husky init
    • This creates the .husky/ folder, a sample pre-commit hook, and updates/adds the prepare script in package.json[4][6][7].
  3. Check Your package.json
    You should see:
    "scripts": {
      "prepare": "husky"
    }
  4. Try a Commit
    After running npm 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 your package.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:


@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.

Copy link

Copy link

@coderabbitai coderabbitai bot left a 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 instruction

Right now, running npm ci will trigger the prepare script (if configured) to install Husky hooks. Some users may not have a prepare script in package.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 commands

For 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 the revert commit type for completeness

Conventional 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

📥 Commits

Reviewing files that changed from the base of the PR and between 30aabc2 and d0a750d.

📒 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 hooks

The 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 clear

Listing Node.js (v14+) and npm as prerequisites aligns with the tooling requirements. No spelling or formatting issues here.


70-76: Dependency installation instructions look good

The npm ci step succinctly installs all necessary dev dependencies, including Husky and Commitlint.


99-99: Commit validation notice is clear

Stating that commit messages will be automatically validated upon commit reinforces the tooling benefits.


114-114: Approve community link formatting

Removing the extra hyphen improves the readability of the "Join Our Community" section.

@drtechie
Copy link
Member

@abhijeetw035 Thanks for adding the documentation.
Will you copy the same to AMRIT-Docs too?
Particularly mention the checks here:
https://piramal-swasthya.gitbook.io/amrit/community-and-support/contribution-guidelines
And mention about commitlint check done on PRs in GitHub here:
https://piramal-swasthya.gitbook.io/amrit/deployment/ci-cd-pipelines

@abhijeetw035
Copy link
Contributor Author

Yes @drtechie , will update it!!

@abhijeetw035 abhijeetw035 deleted the feature/issue-83 branch April 18, 2025 13:33
@abhijeetw035
Copy link
Contributor Author

@drtechie , I updated AMRIT-Docs , please review
PSMRI/AMRIT-Docs#3

@@ -0,0 +1 @@
npx --no -- commitlint --edit $1 No newline at end of file
Copy link
Member

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": {
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@drtechie made the suggested changes
please refer : #76

Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[C4GT Community]: Implement Commit Message Linting and Automation for MMU-API
2 participants