Skip to content

Conversation

@G4URAV001
Copy link

@G4URAV001 G4URAV001 commented Apr 25, 2025

📋 Description

Closes PSMRI/AMRIT#87
JIRA ID: issue link

This PR implements commit message linting and automation for all contributors in the API repositories using Commitlint, Husky, and Commitizen. The aim of this initiative is to enforce a consistent and readable commit history, improving collaboration and enabling future automated changelogs and CI/CD processes. This setup aligns with the practices followed across all C4GT Community repositories.

  • Every commit in API repositories will follow the defined structure (e.g., Conventional Commits).

  • Husky will block non-compliant commit messages locally.

  • Contributors will use Commitizen (cz) to follow a guided commit format.

  • GitHub Actions will fail PR checks if commit messages do not follow the required format.

  • All changes will be compatible with the existing CI/CD pipelines, ensuring smooth integration.


✅ 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

•⁠ ⁠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.
Screenshot 2025-04-25 212250
Screenshot 2025-04-25 212304

Summary by CodeRabbit

  • New Features
    • Introduced automated commit message linting and enforcement using Commitlint and Husky Git hooks.
    • Added Commitizen integration for standardized, guided commit message creation.
  • Chores
    • Added Node.js project configuration and dependencies for commit message tooling.
    • Updated .gitignore to exclude common Node.js artifacts.
  • Documentation
    • Updated README with instructions for setting up commit hooks and following commit message conventions.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 25, 2025

Walkthrough

This change introduces a comprehensive commit message linting and enforcement system to the project. It adds configuration and tooling for Commitlint, Husky, and Commitizen, along with a GitHub Actions workflow to automatically check commit message compliance on pull requests. Documentation is updated to guide developers on setting up and using these tools, and .gitignore is updated for Node.js artifacts. The new setup ensures that all commit messages follow the Conventional Commits specification, both locally via Git hooks and in CI.

Changes

File(s) Change Summary
.github/workflows/commit-lint.yml Adds a GitHub Actions workflow to lint commit messages on pull requests using Commitlint.
.gitignore Appends Node.js-specific ignore rules for node_modules/ and npm-debug.log.
.husky/commit-msg Adds a Husky Git hook to run Commitlint on commit messages during local commits.
README.md Adds instructions for setting up commit hooks, using Commitizen, and following Conventional Commits; minor formatting update.
commitlint.config.js Introduces Commitlint configuration enforcing Conventional Commits rules and restrictions.
package.json Adds Node.js project metadata, Commitizen configuration, scripts, and devDependencies for commit linting and hooks.

Sequence Diagram(s)

sequenceDiagram
    participant Developer
    participant Husky
    participant Commitlint
    participant GitHub Actions

    Developer->>Husky: git commit
    Husky->>Commitlint: Lint commit message
    Commitlint-->>Husky: Pass/Fail
    Husky-->>Developer: Allow/Block commit

    Developer->>GitHub Actions: Open/sync PR
    GitHub Actions->>Commitlint: Lint all PR commits
    Commitlint-->>GitHub Actions: Pass/Fail
    GitHub Actions-->>Developer: PR status update
Loading

Possibly related issues

Poem

In the warren where code is spun,
Now every commit is a tidy one!
Husky and Commitlint hop in line,
With Commitizen, your messages shine.
From local hooks to CI’s gate,
Our history’s neat—no need to wait!
🐇✨ Code carrots for all who comply!

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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 generate sequence diagram to generate a sequence diagram of the changes in 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.

@sonarqubecloud
Copy link

Copy link
Contributor

@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: 2

🧹 Nitpick comments (8)
.gitignore (1)

24-26: Suggestion: Broaden npm debug log ignore pattern.
Consider using a wildcard to ignore rotated or versioned npm debug logs.

 node_modules/
-npm-debug.log
+npm-debug.log*
README.md (3)

43-46: Specify language for code block.
For syntax highlighting, add bash to the fenced code block for Node.js version checks.

-    ```
+    ```bash
     node --version
     npm --version
    ```
🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

43-43: Fenced code blocks should have a language specified
null

(MD040, fenced-code-language)


50-53: Specify language for code block.
Add bash to the fenced code block for npm commands.

-    ```
+    ```bash
     npm ci
    ```
🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

50-50: Fenced code blocks should have a language specified
null

(MD040, fenced-code-language)


83-85: Specify language for code block.
Add bash to the code fence for the Commitizen command.

- ```
+ ```bash
   npm run commit
- ```
+ ```
.github/workflows/commit-lint.yml (1)

29-30: Remove trailing whitespace and add newline at EOF.
YAML lint reports trailing spaces on line 30 and missing newline at end-of-file.

-run: |
-  npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verbose 
+run: |
+  npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verbose
+
🧰 Tools
🪛 YAMLlint (1.35.1)

[error] 30-30: no new line character at the end of file

(new-line-at-end-of-file)


[error] 30-30: trailing spaces

(trailing-spaces)

package.json (1)

1-21: Optional: Add a manual commit-lint script.
For convenience, add a script to manually run commitlint locally outside a Git hook.

 "scripts": {
   ...
+  "lint:commit": "commitlint --from=HEAD~1 --to=HEAD --verbose"
 }

This lets developers lint one or more commits on demand.

commitlint.config.js (2)

9-13: Subject-case restrictions
Currently you’re disallowing sentence-case, Start-Case, PascalCase, and UPPER-CASE in the subject, but allowing any other style (e.g. kebab-case or all-lowercase). If you’d prefer to enforce only lowercase (no hyphens), consider:

-'subject-case': [2, 'never', ['sentence-case', 'start-case', 'pascal-case', 'upper-case']],
+'subject-case': [2, 'always', 'lower-case'],

35-36: Remove trailing whitespace
There’s an extra space after the closing semicolon on line 36. Trimming it will clean up the file.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b43490d and 9e96314.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (6)
  • .github/workflows/commit-lint.yml (1 hunks)
  • .gitignore (1 hunks)
  • .husky/commit-msg (1 hunks)
  • README.md (2 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)


[error] 30-30: trailing spaces

(trailing-spaces)

🪛 LanguageTool
README.md

[uncategorized] ~41-~41: The official spelling of this programming framework is “Node.js”.
Context: ...d npm** - Download and install from nodejs.org - Verify i...

(NODE_JS)


[grammar] ~67-~67: “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] ~70-~70: Loose punctuation mark.
Context: ... changes (formatting, etc.) - refactor: Code changes that neither fix bugs nor ...

(UNLIKELY_OPENING_PUNCTUATION)

🪛 markdownlint-cli2 (0.17.2)
README.md

43-43: Fenced code blocks should have a language specified
null

(MD040, fenced-code-language)


50-50: Fenced code blocks should have a language specified
null

(MD040, fenced-code-language)


82-82: Fenced code blocks should have a language specified
null

(MD040, fenced-code-language)

🔇 Additional comments (5)
commitlint.config.js (5)

1-3: Extending the conventional config correctly
The file properly extends @commitlint/config-conventional to inherit the standard Conventional Commits ruleset.


4-8: Enforcing body & footer formatting
You’ve added rules to require leading blank lines and limit line lengths in the body (100 chars) and footer (100 chars). This maintains readability—warning for missing blank lines and errors for line-length violations.


14-15: Prevent empty/trailing-period subjects
The rules subject-empty: [2, 'never'] and subject-full-stop: [2, 'never', '.'] correctly enforce that commits have a non-empty subject without a trailing period, matching Conventional Commits.


16-21: Type-case and emptiness rules
Requiring lowercase commit types (type-case) and disallowing empty types (type-empty) ensures consistency in the <type>(<scope>): <subject> structure.


22-34: Allowed types enumeration
Your type-enum list covers the standard Conventional Commit types. If your project later introduces custom types (e.g., hotfix), remember to update this array.

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

Choose a reason for hiding this comment

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

⚠️ Potential issue

Invalid npx flag for commitlint invocation.
The Husky hook uses npx --no -- commitlint, but the recommended flag is --no-install. This may cause hook failures.

Apply this diff:

-npx --no -- commitlint --edit $1
+npx --no-install commitlint --edit "$1"
📝 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
npx --no -- commitlint --edit $1
npx --no-install commitlint --edit "$1"

Comment on lines +5 to +8
"test": "echo \"Error: no test specified\" && exit 1",
"prepare": "husky",
"commit": "cz"
},
Copy link
Contributor

@coderabbitai coderabbitai bot Apr 25, 2025

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix Husky prepare script.
Husky v7+ requires husky install in the prepare step to set up Git hooks. Using husky alone may not initialize the hooks.

-  "scripts": {
-    "test": "echo \"Error: no test specified\" && exit 1",
-    "prepare": "husky",
-    "commit": "cz"
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1",
+    "prepare": "husky install",
+    "commit": "cz"

After updating, run npm install to trigger the prepare script and create the .husky directory with your hooks.

📝 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
"test": "echo \"Error: no test specified\" && exit 1",
"prepare": "husky",
"commit": "cz"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"prepare": "husky install",
"commit": "cz"
},

Copy link
Author

Choose a reason for hiding this comment

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

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
Contributor

Choose a reason for hiding this comment

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

🧩 Analysis chain

@G4URAV001 Thank you for the correction! You're absolutely right. For Husky v9, the correct configuration is "prepare": "husky" as you've implemented, and husky install is deprecated in v9.

I appreciate you providing the reference to the official documentation. Your implementation is correct, and I apologize for the confusion in my previous comment.


🌐 Web query:

husky v9 installation "prepare" script configuration

💡 Result:

Husky v9 Installation and "prepare" Script Configuration

Husky v9 manages Git hooks in JavaScript projects and relies on the prepare script in your package.json to set up and install its hooks. Understanding how this script is configured—and what can and cannot be customized—is essential for a smooth installation, especially in collaborative or monorepo setups.


Default Behavior of prepare Script in Husky v9

  • When you run husky init (e.g., npx husky init or pnpm exec husky init), Husky v9:
    • Creates a .husky directory.
    • Adds a default pre-commit hook script.
    • Sets the prepare script in your package.json to husky (overwriting any existing prepare script)[3][4][1].
  • The prepare script is used because it runs after npm install, ensuring Husky is set up automatically for every user who installs dependencies[2][4].

Configuring or Customizing the prepare Script

  • Custom prepare scripts are NOT supported. Husky v9's behavior is to always overwrite the prepare script with prepare: husky, so you cannot have custom logic in this script alongside Husky[1].
  • If your repository already has a prepare script, it will be replaced. This is a known issue and there is no official workaround to merge custom prepare steps[1].
  • If you need custom setup steps, you must run them separately from the standard Husky installation process, or manually set up Husky hooks as detailed below.

Workarounds and Manual Setup

If you require additional logic or custom commands during preparation, you must not rely on Husky’s automatic script and instead:

  1. Manually set up Husky:

    • Follow Husky’s manual setup guide to perform the three essential steps by hand[1].
    • Then, you can use your own custom prepare script that includes both your custom logic and Husky installation steps, for example:
      "scripts": {
        "prepare": "your-custom-setup && husky install"
      }
    • This way, you control what happens in prepare, but you assume responsibility for Husky setup[1][5].
  2. In monorepos or custom directory setups:

    • Adjust the prepare script to target the correct location of your .git or .husky directories, as needed[5].
    • Example:
      "scripts": {
        "prepare": "cd ../ && husky install frontend/.husky"
      }

Handling CI Environments

  • If you want to prevent prepare from failing in CI (where devDependencies might not be installed), you can use an OR fallback:
    "scripts": {
      "prepare": "husky || true"
    }
    This ensures the script does not cause the build to fail if Husky is not installed[2][6].

Summary Table: Husky v9 Prepare Script Behavior

Scenario Husky Installs / Overwrites Customization Workaround
prepare script present Overwritten by Husky No Manual setup, combine steps
Need multiple prepare actions Not supported by default No Manual setup, combine steps
CI environment Fails if missing Yes, with `

Key Takeaways

  • Husky v9 enforces prepare: husky and does not support custom or multiple prepare scripts out of the box[1][3][4].
  • If you need custom logic, adopt manual setup and move all required commands into your own prepare script[1].
  • Always run npm run prepare after updates to ensure hooks are correctly installed[5].

For ongoing issues and updates, monitor Husky’s official GitHub repository[1][8].

Citations:


@G4URAV001 You're absolutely right, and I apologize for the incorrect suggestion. The web search confirms that for Husky v9, the correct configuration is indeed "prepare": "husky" as you've implemented, and the husky install command is deprecated in v9.

Thank you for providing the reference to the official documentation. Your implementation is correct, and I appreciate you taking the time to correct this misunderstanding.


✏️ Learnings added
Learnt from: G4URAV001
PR: PSMRI/Helpline104-API#45
File: package.json:5-8
Timestamp: 2025-04-25T16:54:00.028Z
Learning: Husky v9+ specifically requires "prepare": "husky" in package.json scripts, while "prepare": "husky install" was for Husky v8 and earlier versions. The install command is deprecated in v9 as documented at https://typicode.github.io/husky/how-to.html

Learnt from: G4URAV001
PR: PSMRI/Helpline104-API#45
File: package.json:5-8
Timestamp: 2025-04-25T16:54:00.028Z
Learning: Husky v9+ specifically requires "prepare": "husky" in package.json scripts, while "prepare": "husky install" was for Husky v8 and earlier versions. The install command is deprecated in v9 as documented at 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.

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 Helpline104-API

1 participant