diff --git a/.gitea/scoped_workflows/length-validation.yaml b/.gitea/scoped_workflows/length-validation.yaml index 370f64e..8d809ca 100644 --- a/.gitea/scoped_workflows/length-validation.yaml +++ b/.gitea/scoped_workflows/length-validation.yaml @@ -1,11 +1,14 @@ -name: Check PR & Commit Length +name: Conventional Commits Check on: pull_request: types: [opened, edited, synchronize, reopened] +env: + NODE_VERSION: '24' + jobs: - validate-length: + lint-commits-and-pr: runs-on: ubuntu-latest steps: - name: Checkout code @@ -13,40 +16,28 @@ jobs: with: 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: PR_TITLE: ${{ github.event.pull_request.title }} 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 }} HEAD_SHA: ${{ github.event.pull_request.head.sha }} - MAX_TITLE: 72 - MAX_DESC: 500 run: | - # 1. Check PR Title & Description (important when using Squash Merge) - 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!" + npx commitlint --from "$BASE_SHA" --to "$HEAD_SHA" --verbose diff --git a/commitlint.config.js b/commitlint.config.js new file mode 100644 index 0000000..df8d04a --- /dev/null +++ b/commitlint.config.js @@ -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], + }, + }; + \ No newline at end of file