Skip to content
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

chore(lint): improve commit-linting #12200

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions COMMITS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,17 @@ feat: allow provided config object to extend other configs
feat(lang): added polish language
```

### Commit linting
Use `yarn lint:commits` to validate your commit messages compared to `$BASE_BRANCH_NAME` (default: `develop`)

### Git hook

Use this git hook to auto-check your commit messages. Save the following snippet into `.git/hooks/commit-msg`. This way, the check will run locally and can avoid some unnecessary CI runs.

```bash
#!/bin/sh

commit_msg=$(cat "$1")
if echo "$commit_msg" | grep -qE "^(Revert|fixup! )"; then
# Skip validation in case of fixup and revert commits
exit 0
fi

if ! grep -qE "^(build|ci|docs|feat|fix|perf|refactor|style|test|chore|revert)(\([a-z, -]+\))?: " "$1" ; then
echo "Conventional Commits validation failed"
exit 1
fi

LINT_COMMIT_MSG="$1" ./scripts/check-commit-messages.sh
```

If you want to bypass commit-msg hook check, you may always use
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"type-check": "yarn nx run-many --target=type-check",
"type-check:force": "rimraf -rf -- **/libDev && yarn type-check",
"test:unit": "yarn nx run-many --target=test:unit",
"lint:commits": "./scripts/check-commit-messages.sh",
"lint:js": "eslint . --cache --cache-strategy content --ignore-path .gitignore",
"lint:styles": "yarn nx run-many --target=lint:styles",
"lint": "yarn lint:styles && yarn lint:js",
Expand Down
62 changes: 41 additions & 21 deletions scripts/check-commit-messages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,57 @@

# This script is run by commit-messages-check github action

if ! git rev-list origin/"$BASE_BRANCH_NAME"..HEAD > /dev/null 2>&1; then
tput -T linux setaf 1
echo "git rev-list command failed"
tput -T linux sgr0
exit 1
fi

for commit in $(git rev-list origin/"$BASE_BRANCH_NAME"..HEAD); do
BASE_BRANCH_NAME="${BASE_BRANCH_NAME:-develop}"

commit_msg=$(git log --format=%B -n 1 "$commit")
lint_commit_msg() {
# Skip validation in case of revert commits
if echo "$commit_msg" | grep -qE "^Revert"; then
return
fi

# Skip validation in case of revert commits
if echo "$commit_msg" | grep -qE "^Revert"; then
continue
fi

# Check for fixup commits
if echo "$commit_msg" | grep -qE "^fixup! "; then
# Check for fixup commits
if echo "$commit_msg" | grep -qE "^fixup! "; then
tput -T linux setaf 1
echo "Fixup commit validation failed for commit $commit: $commit_msg"
tput -T linux sgr0
echo -e "To squash fixup commits, run:\ngit rebase -i --autosquash origin/HEAD"
exit 1
fi
fi

# Check commit message syntax
if ! echo "$commit_msg" | grep -qE "^(build|ci|docs|feat|fix|perf|refactor|style|test|chore|revert|npm-release|release)(\([a-z, -]+\))?: "; then
# Check commit message syntax
if ! echo "$commit_msg" | grep -qE "^(build|ci|docs|feat|fix|perf|refactor|style|test|chore|revert|npm-release|release)(\([a-z, -]+\))?: "; then
tput -T linux setaf 1
echo "Conventional Commits validation failed for commit $commit: $commit_msg"
tput -T linux sgr0
echo "Learn more about Conventional Commits at https://www.conventionalcommits.org"
exit 1
fi
done
fi
}

lint_commit() {
commit=$1
commit_msg=$(git log --format=%B -n 1 "$commit")
lint_commit_msg $commit_msg
}

if [ ! -z "$LINT_COMMIT_MSG" ]; then
# passing an explicit messages skips git interactions; for use in commit hook
lint_commit_msg "$LINT_COMMIT_MSG"
exit 0
fi

if [ -z "$LINT_COMMITS" ]; then
# if commit hashes are not passed explicitly, lint history compared to base branch
if ! git rev-list origin/"$BASE_BRANCH_NAME"..HEAD > /dev/null 2>&1; then
tput -T linux setaf 1
echo "git rev-list command failed"
tput -T linux sgr0
exit 1
fi
LINT_COMMITS=$(git rev-list origin/"$BASE_BRANCH_NAME"..HEAD)
fi


for commit in $LINT_COMMITS; do
lint_commit $commit
done