Skip to content

Conversation

G4URAV001
Copy link

@G4URAV001 G4URAV001 commented Apr 25, 2025

📋 Description

Closes PSMRI/AMRIT#90
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 215113
Screenshot 2025-04-25 215125

Summary by CodeRabbit

  • New Features
    • Introduced automated commit message linting to enforce conventional commit standards.
    • Added interactive commit creation with prompts for standardized commit messages.
  • Chores
    • Updated project documentation with guidelines for commit message formatting.
    • Added configuration and scripts for commit message validation and Git hooks.
    • Updated .gitignore to exclude Node.js dependencies and logs.

Copy link

coderabbitai bot commented Apr 25, 2025

Walkthrough

This update introduces automated commit message linting and enforcement into the project. It adds configuration files for Commitlint, Husky, and Commitizen, sets up a GitHub Actions workflow to check commit messages on pull requests, and documents the process in the README. The .gitignore is updated to exclude typical Node.js artifacts. These changes collectively ensure that all commit messages adhere to a standardized format, both locally via Git hooks and remotely via continuous integration.

Changes

File(s) Change Summary
.github/workflows/commit-lint.yml Added a GitHub Actions workflow for commit message linting on pull requests using Commitlint.
.husky/commit-msg Introduced a Husky Git hook script to enforce commit message linting during local commits.
commitlint.config.js Added Commitlint configuration file specifying conventional commit rules and custom restrictions for commit messages.
package.json Introduced a new Node.js project configuration with scripts and devDependencies for Commitlint, Commitizen, Husky, and related tools.
.gitignore Appended Node.js-specific ignores for node_modules/ and npm-debug.log.
README.md Documented the commit message linting process, including instructions for developers, and fixed a minor typo.

Sequence Diagram(s)

sequenceDiagram
    participant Developer
    participant Git
    participant Husky
    participant Commitlint
    participant GitHub
    participant GitHub Actions

    Developer->>Git: git commit
    Git->>Husky: Trigger commit-msg hook
    Husky->>Commitlint: Lint commit message
    Commitlint-->>Husky: Pass/Fail
    Husky-->>Git: Allow/Block commit

    Developer->>GitHub: Push branch / Open PR
    GitHub->>GitHub Actions: Trigger commit-lint workflow
    GitHub Actions->>Commitlint: Lint all PR commits
    Commitlint-->>GitHub Actions: Pass/Fail
    GitHub Actions-->>GitHub: Report status on PR
Loading

Possibly related issues

Poem

A rabbit with code in its paws,
Hopped in to enforce commit laws.
With Husky and lint,
Your messages won’t stint,
On clarity, structure, or cause.
Now every push, every merge,
Is tidy and neat—let’s surge!
🐇✨

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

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

🧹 Nitpick comments (3)
package.json (1)

4-8: Add a manual commitlint script for on-demand linting
It can be useful to run commitlint outside of CI or Git hooks. Consider adding:

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "prepare": "husky install",
    "commit": "cz",
+   "lint:commit": "commitlint --from HEAD~10 --to HEAD --verbose"
  },

This lets developers manually validate recent commits.

.github/workflows/commit-lint.yml (1)

29-30: Remove trailing spaces and add newline at EOF
Your workflow file is missing a newline at the end and has trailing whitespace on the --verbose line. Apply:

-          npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verbose 
+          npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verbose
+

This satisfies YAML linters and best practices.

🧰 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)

commitlint.config.js (1)

18-34: Allowed Commit Types Reviewed
The listed types (build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test) are standard. If your workflow ever uses dedicated release commits, consider adding a release type to this enum.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1244c01 and fc28a11.

⛔ 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)

🔇 Additional comments (8)
package.json (1)

14-20: Verify Commitizen configuration path
Double-check that the config.commitizen.path correctly points to cz-conventional-changelog under node_modules. If you ever move or upgrade the adapter, update this accordingly.

.gitignore (1)

37-40: Excluding Node.js artifacts
The new section correctly ignores node_modules/ and npm-debug.log, preventing large directories and debug logs from being committed.

README.md (1)

37-52: Document commit hook setup clearly
The new "Commit Message Hooks" section is concise, accurate, and guides contributors through installing dependencies and using the interactive commit flow.

commitlint.config.js (5)

1-3: Configuration Setup Looks Good
Extending @commitlint/config-conventional is the correct approach to inherit the conventional commit rules. No issues found in the module export or extends configuration.


4-8: Line Length and Blank-line Rules Are Appropriate
The rules for enforcing blank lines before body/footer and capping their line lengths (100 chars), as well as the 100-character header max length, align with the PR objectives to maintain readability.


9-15: Subject Rules Enforce Consistency
The configuration correctly prohibits unwanted casing styles and trailing periods in the subject line, and disallows empty subjects. This ensures concise, well-formatted commit messages.


16-17: Type Case and Non-emptiness Enforced
Requiring lowercase commit types and disallowing empty types is consistent with Conventional Commits standards.


35-36: Closing Syntax – No Action Needed
These lines only close the object and file. No changes required here.

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

@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 to properly install hooks
The current prepare script ("husky") will invoke the CLI help rather than initializing Git hooks. According to Husky v7+ docs, it should be:

-scripts": {
-  "prepare": "husky",
+scripts": {
+  "prepare": "husky install",

This ensures hooks in .husky/ are hooked into the repository on npm 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
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

Choose a reason for hiding this comment

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

🧩 Analysis chain

You're absolutely right, @G4URAV001! I apologize for the incorrect suggestion. After checking the Husky v9 documentation, I can confirm that for Husky v9.1.7, the correct prepare script is indeed "prepare": "husky" without the "install" command.

The documentation clearly states that for v9, the recommended setup is:

npm pkg set scripts.prepare="husky"

And as you correctly pointed out, the "install" command is deprecated in v9.

Thank you for the correction and for providing the reference documentation. The current implementation in your package.json is correct for Husky v9.1.7.


🌐 Web query:

Husky v9 documentation prepare script

💡 Result:

Husky v9 "prepare" Script: Documentation and Usage

Summary:
With Husky v9, setting up Git hooks is streamlined via the "prepare" script in package.json, the husky NPM package, and npx husky init. This approach leverages Git's core.hooksPath and ensures a consistent, team-friendly setup.


Purpose of the "prepare" Script

  • The "prepare" script in package.json ensures that after any npm install, Husky sets up the necessary Git hooks for all users of the repository[1][3][5].
  • Its presence automates hook installation, providing a frictionless experience for all developers interacting with the project.

What Does Husky v9 Do When Initialized?

  1. Installation:
    Install Husky as a development dependency:

    npm install --save-dev husky
    

    or with your preferred package manager:

    yarn add --dev husky
    pnpm add --save-dev husky
    bun add --dev husky
    
  2. Initialization:
    Run the init command:

    npx husky init
    

    This will:

    • Add or update "prepare": "husky" in your package.json.
    • Create a .husky/ directory and add a sample pre-commit hook.
    • Set Git's core.hooksPath to .husky/_ automatically[1][3][5].

    Example package.json snippet after initialization:

    "scripts": {
      "prepare": "husky"
    }
  3. How It Works:

    • After every npm install, the "prepare": "husky" script ensures Husky sets up and maintains the hooks directory and links the shell scripts.
    • Actual Git hook scripts live in .husky/ (e.g., .husky/pre-commit), but Husky internally uses .husky/_ for its operational files[3][5].

Key Points & Best Practices

  • Do not manually edit or remove .husky/_ — Husky manages this directory.
  • Place your custom hook scripts (e.g., pre-commit, pre-push) directly in the .husky/ folder[1][3][5].
  • The "prepare": "husky" script is the recommended form for Husky v9 (previous versions used "prepare": "husky install")[5].
  • For CI/CD environments where devDependencies might not be installed, you can make the script non-failing:
    "prepare": "husky || true"
    

Example Migration to Husky v9

  1. Update Husky:

    npm install --save-dev husky@latest
    
  2. Ensure the Correct Prepare Script:

    "scripts": {
      "prepare": "husky"
    }
  3. Initialize Husky:

    npx husky init
    

    This creates the necessary directories and a sample hook file[1][3][5].

  4. Customize Hooks:
    Edit .husky/pre-commit and other hook files as needed.


References to Official Documentation


In summary:
With Husky v9, the "prepare": "husky" script in package.json is central to setup. After each dependency install, it ensures Husky hooks are properly configured, hooks live in .husky/, and all operational details are handled automatically for both local development and shared team environments[1][3][5].

Citations:


@G4URAV001 You're absolutely right! Thank you for the correction and reference.

I can confirm from the documentation that Husky v9 indeed uses "prepare": "husky" and the install command is deprecated in v9. This differs from v8 and earlier versions which used "prepare": "husky install".

Your current implementation is correct for Husky v9.1.7. I apologize for the incorrect suggestion.


✏️ Learnings added
Learnt from: G4URAV001
PR: PSMRI/Identity-API#95
File: package.json:6-7
Timestamp: 2025-04-25T16:49:37.488Z
Learning: For Husky v9+, the correct prepare script is "prepare": "husky" (without "install"). The "install" command is deprecated in v9. For v8 and earlier versions, the command was "prepare": "husky install".

Learnt from: G4URAV001
PR: PSMRI/Identity-API#95
File: package.json:6-7
Timestamp: 2025-04-25T16:49:37.488Z
Learning: For Husky v9+, the correct prepare script is "prepare": "husky" (without "install"). The "install" command is deprecated in v9. For v8 and earlier versions, the command was "prepare": "husky install".

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.

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

Choose a reason for hiding this comment

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

⚠️ Potential issue

Use the correct npx flag and quote the commit file
The hook currently uses --no, which is not a valid npx flag and may not prevent installs. Please update to:

- npx --no -- commitlint --edit $1 
+ npx --no-install commitlint --edit "$1"

This ensures the locally installed commitlint is used without side-effect installs and handles filenames with spaces safely.

📝 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 +1 to +30
name: Commit Compliance

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
commitlint:
runs-on: ubuntu-latest

steps:
- name: Checkout code with submodule
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
submodules: true
fetch-depth: 0

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18

- name: Install dependencies
run: npm ci --legacy-peer-deps

- name: Run commitlint on PR
run: |
npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verbose No newline at end of file
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Verify presence of commitlint.config.js
Ensure that commitlint.config.js exists in the repository root and correctly extends @commitlint/config-conventional. You can run:

This confirms your CI and local hooks have the intended rules.


🏁 Script executed:

#!/bin/bash
# Check for commitlint config
if [[ ! -f commitlint.config.js ]]; then
  echo "Error: commitlint.config.js is missing"
  exit 1
else
  echo "commitlint.config.js found"
fi

Length of output: 96


#!/bin/bash
# Verify that commitlint.config.js extends @commitlint/config-conventional
grep -n "extends" commitlint.config.js || echo "No 'extends' key found"

🏁 Script executed:

#!/bin/bash
# Verify commitlint.config.js extends @commitlint/config-conventional
rg -n "extends\s*:\s*['\"]@commitlint/config-conventional['\"]" commitlint.config.js || echo "Error: commitlint.config.js does not extend @commitlint/config-conventional"

Length of output: 251


Add conventional commit rules to commitlint.config.js
The file exists but doesn’t extend the official conventional preset, so CI won’t enforce your team’s commit guidelines. Please update commitlint.config.js to include:

module.exports = {
  extends: ['@commitlint/config-conventional'],
  // …any custom rules here
};

• File: commitlint.config.js
• Line(s): at the top of the exported config

🧰 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)

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 Identity-API
1 participant