-
Notifications
You must be signed in to change notification settings - Fork 33
feat: use regex pattern to valid message #11
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,12 @@ | ||
const DEFAULT_COMMIT_TYPES = [ | ||
"feat", | ||
"fix", | ||
"docs", | ||
"style", | ||
"refactor", | ||
"test", | ||
"build", | ||
"perf", | ||
"ci", | ||
"chore", | ||
"revert", | ||
"merge", | ||
"wip", | ||
]; | ||
|
||
const isValidCommitMessage = (message, availableTypes = DEFAULT_COMMIT_TYPES): boolean => { | ||
const isValidCommitMessage = (message): boolean => { | ||
// Exceptions. | ||
// This is a message that's auto-generated by git. Can't do much about it unfortunately. Let's allow it. | ||
if (message.startsWith("Merge ") || message.startsWith("Revert ")) { | ||
return true; | ||
} | ||
|
||
// Commit message doesn't fall into the exceptions group. Let's do the validation. | ||
let [possiblyValidCommitType] = message.split(":"); | ||
possiblyValidCommitType = possiblyValidCommitType.toLowerCase(); | ||
|
||
// Let's remove scope if present. | ||
if (possiblyValidCommitType.match(/\(\S*?\)/)) { | ||
possiblyValidCommitType = possiblyValidCommitType.replace(/\(\S*?\)/, ""); | ||
} | ||
|
||
possiblyValidCommitType = possiblyValidCommitType | ||
.replace(/\s/g, "") // Remove all whitespace | ||
.replace(/\!/g, "") // Remove bang for notify breaking change | ||
.replace(/()/g, "") // Remove all whitespace | ||
.replace(/[^a-z]/g, ""); // Only leave [a-z] characters. | ||
|
||
return availableTypes.includes(possiblyValidCommitType); | ||
let pattern = /^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test){1}(\([\w\-\.]+\))?(!)?: \S+([\s\S]*)/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather the
to build the pattern. This makes it easier to extend the list of valid commit prefixes and improves readability. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's worth noting this change is also not backwards compatible as the allowed types are no longer supported. I don't think this is a desirable change and if it were to proceed the PR should updated to I believe it's possible to update the regex without removing the existing capabilities. |
||
return pattern.test(message) | ||
}; | ||
|
||
export default isValidCommitMessage; |
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 feel that adding emojis to commit messages should be fine as long as the rest of the commit messages adheres to the conventional commits semantics.
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 this plugin should match the spec as close as possible and I'm not sure emojis are supported: https://www.conventionalcommits.org/en/v1.0.0/#specification