-
Notifications
You must be signed in to change notification settings - Fork 18
feat: add commit quality analysis and policy enforcement #58
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
Open
vraj826
wants to merge
1
commit into
nirvik34:main
Choose a base branch
from
vraj826:feat/commit-quality-policy-engine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| analyzeCommitQuality, | ||
| shouldBlockCommitForQuality, | ||
| } from "./commitQuality"; | ||
|
|
||
| describe("analyzeCommitQuality", () => { | ||
| it("accepts basic Conventional Commit messages", () => { | ||
| expect(analyzeCommitQuality("feat: add auth").warnings).not.toContain( | ||
| "Invalid Conventional Commit format", | ||
| ); | ||
| expect(analyzeCommitQuality("fix(core): resolve bug").score).toBe(100); | ||
| }); | ||
|
|
||
| it("warns for invalid Conventional Commit messages", () => { | ||
| expect(analyzeCommitQuality("update code").warnings).toContain( | ||
| "Invalid Conventional Commit format", | ||
| ); | ||
| expect(analyzeCommitQuality("fix stuff").warnings).toContain( | ||
| "Invalid Conventional Commit format", | ||
| ); | ||
| }); | ||
|
|
||
| it("detects generic commit messages case-insensitively", () => { | ||
| expect(analyzeCommitQuality("WIP").warnings).toContain( | ||
| "Generic commit message", | ||
| ); | ||
| }); | ||
|
|
||
| it("applies simple score penalties", () => { | ||
| expect(analyzeCommitQuality("fix stuff")).toEqual({ | ||
| score: 0, | ||
| warnings: [ | ||
| "Invalid Conventional Commit format", | ||
| "Generic commit message", | ||
| "Subject too short", | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it("blocks only in strict mode when score is below 60", () => { | ||
| const result = analyzeCommitQuality("changes"); | ||
|
|
||
| expect(shouldBlockCommitForQuality(result, true)).toBe(true); | ||
| expect(shouldBlockCommitForQuality(result, false)).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| export interface CommitQualityResult { | ||
| score: number; | ||
| warnings: string[]; | ||
| } | ||
|
|
||
| const GENERIC_MESSAGES = [ | ||
| "update code", | ||
| "fix stuff", | ||
| "misc changes", | ||
| "changes", | ||
| "update files", | ||
| "work in progress", | ||
| "wip", | ||
| ]; | ||
|
|
||
| const CONVENTIONAL_COMMIT_PATTERN = | ||
| /^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\([a-z0-9._-]+\))?: .+/i; | ||
|
|
||
| function getSubject(message: string): string { | ||
| const separatorIndex = message.indexOf(":"); | ||
| return separatorIndex >= 0 | ||
| ? message.slice(separatorIndex + 1).trim() | ||
| : message.trim(); | ||
| } | ||
|
|
||
| export function analyzeCommitQuality(message: string): CommitQualityResult { | ||
| const normalizedMessage = message.trim().toLowerCase(); | ||
| const warnings: string[] = []; | ||
| let score = 100; | ||
|
|
||
| if (!CONVENTIONAL_COMMIT_PATTERN.test(message.trim())) { | ||
| score -= 40; | ||
| warnings.push("Invalid Conventional Commit format"); | ||
| } | ||
|
|
||
| if (GENERIC_MESSAGES.includes(normalizedMessage)) { | ||
| score -= 40; | ||
| warnings.push("Generic commit message"); | ||
| } | ||
|
|
||
| if (getSubject(message).length < 10) { | ||
| score -= 20; | ||
| warnings.push("Subject too short"); | ||
| } | ||
|
|
||
| return { | ||
| score: Math.max(0, Math.min(100, score)), | ||
| warnings, | ||
| }; | ||
| } | ||
|
|
||
| export function shouldBlockCommitForQuality( | ||
| result: CommitQualityResult, | ||
| strictQuality = false, | ||
| ): boolean { | ||
| return strictQuality && result.score < 60; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.