Last active
July 30, 2024 15:32
-
-
Save smyaseen/17083d60d02a07b2a3122410e2d39b6f to your computer and use it in GitHub Desktop.
Husky commit-msg hook to check branch & commit message convention, run lint, prettier and unit tests.
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
#!/usr/bin/env sh | |
. "$(dirname -- "$0")/_/husky.sh" | |
# Checks for branch name | |
currentBranch=$(git rev-parse --abbrev-ref HEAD) | |
requiredPattern="^(build|chore|feat|docs|refactor|perf|test)/ABC-\d+-.+$" | |
if ! echo "$currentBranch" | grep -qE $requiredPattern; then | |
echo "\nInvalid branch name: $currentBranch" | |
echo "-" | |
echo "Should follow this pattern: build|chore|feat|docs|refactor|perf|test/ABC-ticketnumber-any-text" | |
echo "-" | |
echo "example: docs/ABC-123-update-readme.md" | |
echo "-" | |
echo "Refer to this for convention:" | |
echo "-" | |
echo "build : Changes related to building the code (e.g. adding npm dependencies or external libraries)." | |
echo "-" | |
echo "chore: Changes that do not affect the external user (e.g. updating the .gitignore file or .prettierrc file)." | |
echo "-" | |
echo "feat: A new feature." | |
echo "-" | |
echo "fix: A bug fix." | |
echo "-" | |
echo "docs: Documentation a related changes." | |
echo "-" | |
echo "refactor: A code that neither fix bug nor adds a feature." | |
echo "-" | |
echo "perf: A code that improves performance style: A code that is related to styling." | |
echo "-" | |
echo "test: Adding new test or making changes to existing test" | |
echo "-\n" | |
exit 1 # Branch name doesn't match the pattern, exit with error code | |
fi | |
# Checks for commit message | |
commit_message="$(cat "$1")" | |
pattern='^\[ABC-[0-9]+\] (build|chore|feat|docs|refactor|perf|test): .+$' | |
if [[ ! $commit_message =~ $pattern ]]; then | |
echo "\nInvalid commit message: $commit_message" | |
echo "-" | |
echo "Should follow this pattern: [ABC-ticketnumber] build|chore|feat|docs|refactor|perf|test: objective" | |
echo "-" | |
echo "example: [ABC-15] chore: updated .gitignore" | |
echo "-" | |
echo "Refer to this for convention:" | |
echo "-" | |
echo "build : Changes related to building the code (e.g. adding npm dependencies or external libraries)." | |
echo "-" | |
echo "chore: Changes that do not affect the external user (e.g. updating the .gitignore file or .prettierrc file)." | |
echo "-" | |
echo "feat: A new feature." | |
echo "-" | |
echo "fix: A bug fix." | |
echo "-" | |
echo "docs: Documentation a related changes." | |
echo "-" | |
echo "refactor: A code that neither fix bug nor adds a feature." | |
echo "-" | |
echo "perf: A code that improves performance style: A code that is related to styling." | |
echo "-" | |
echo "test: Adding new test or making changes to existing test" | |
echo "-\n" | |
exit 1 | |
fi | |
# npx lint-staged -- uncomment when have lint setted up | |
# npm run test -- uncomment when have test setted up | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment