Code Issues Pull Requests Actions Packages Projects Releases 15 Wiki Activity Security Code Quality

ci: Integrate commitlint configuration and update PR workflow for conventional commit validation

This commit is contained in:
tienbh committed 2026-11-26 18:06:38 +07:00 Verified
1 parent 0e55734bdf
commit a1146a4d07
2 files changed
+46 -33

No files matched your search

+24 -33
View File
@@ -1,11 +1,14 @@
name: Check PR & Commit Length name: Conventional Commits Check
on: on:
pull_request: pull_request:
types: [opened, edited, synchronize, reopened] types: [opened, edited, synchronize, reopened]
env:
NODE_VERSION: '24'
jobs: jobs:
validate-length: lint-commits-and-pr:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout code - name: Checkout code
@@ -13,40 +16,28 @@ jobs:
with: with:
fetch-depth: 0 fetch-depth: 0
- name: Check PR Title, Description & Commits - name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Install commitlint
run: npm install --no-save @commitlint/cli @commitlint/config-conventional
- name: Validate PR Title & Description (<= 500 chars)
env: env:
PR_TITLE: ${{ github.event.pull_request.title }} PR_TITLE: ${{ github.event.pull_request.title }}
PR_BODY: ${{ github.event.pull_request.body }} PR_BODY: ${{ github.event.pull_request.body }}
run: |
if [ -z "$PR_BODY" ]; then
printf "%s\n" "$PR_TITLE" | npx commitlint
else
printf "%s\n\n%s\n" "$PR_TITLE" "$PR_BODY" | npx commitlint
fi
- name: Validate All Commits in PR
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }} BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }} HEAD_SHA: ${{ github.event.pull_request.head.sha }}
MAX_TITLE: 72
MAX_DESC: 500
run: | run: |
# 1. Check PR Title & Description (important when using Squash Merge) npx commitlint --from "$BASE_SHA" --to "$HEAD_SHA" --verbose
if [ ${#PR_TITLE} -gt $MAX_TITLE ]; then
echo "❌ PR Title too long (${#PR_TITLE}/$MAX_TITLE characters)."
exit 1
fi
if [ ${#PR_BODY} -gt $MAX_DESC ]; then
echo "❌ PR Description too long (${#PR_BODY}/$MAX_DESC characters)."
exit 1
fi
# 2. Check each commit inside PR (important when using Merge Commit / Rebase)
for commit in $(git rev-list "$BASE_SHA..$HEAD_SHA"); do
c_title=$(git log -n 1 --format=%s "$commit")
c_body=$(git log -n 1 --format=%b "$commit")
if [ ${#c_title} -gt $MAX_TITLE ]; then
echo "❌ Commit $commit has Title too long ${#c_title}/$MAX_TITLE characters."
exit 1
fi
if [ ${#c_body} -gt $MAX_DESC ]; then
echo "❌ Commit $commit has Description too long ${#c_body}/$MAX_DESC characters."
exit 1
fi
done
echo "✅ Passed the length requirements!"
+22
View File
@@ -0,0 +1,22 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
// Allow all types (following the Conventional Commits specification)
'type-enum': [
2,
'always',
['build', 'chore', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'revert', 'style', 'test'],
],
// Require type and description (cannot be empty)
'type-empty': [2, 'never'],
'subject-empty': [2, 'never'],
// Limit the length of the Title (header) to 72 characters
'header-max-length': [2, 'always', 72],
// Require a blank line between title and body/footer (following the Conventional Commits specification)
'body-leading-blank': [2, 'always'],
'footer-leading-blank': [2, 'always'],
// Limit the length of each line in body/description to 500 characters (optional)
'body-max-line-length': [2, 'always', 500],
},
};