|
| 1 | +# -------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +# contributor license agreements. See the NOTICE file distributed |
| 5 | +# with this work for additional information regarding copyright |
| 6 | +# ownership. The ASF licenses this file to You under the Apache |
| 7 | +# License, Version 2.0 (the "License"); you may not use this file |
| 8 | +# except in compliance with the License. You may obtain a copy of the |
| 9 | +# License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 16 | +# implied. See the License for the specific language governing |
| 17 | +# permissions and limitations under the License. |
| 18 | +# |
| 19 | +# -------------------------------------------------------------------- |
| 20 | +# Commit Message Check |
| 21 | +# -------------------------------------------------------------------- |
| 22 | +# Checks the commit messages for a pull request to ensure they follow |
| 23 | +# the project's guidelines. This is not a required check and will not |
| 24 | +# block the PR from being merged. It provides suggestions for improving |
| 25 | +# commit messages. |
| 26 | +# |
| 27 | +# The main checks include: |
| 28 | +# * Title length (recommended: 50 characters or less) |
| 29 | +# * Title capitalization (should start with an uppercase letter) |
| 30 | +# * Body presence (should not be empty) |
| 31 | +# * Body line length (recommended: 72 characters or less) |
| 32 | +# -------------------------------------------------------------------- |
| 33 | + |
| 34 | +name: Commit Message Check |
| 35 | + |
| 36 | +on: |
| 37 | + pull_request: |
| 38 | + types: [opened, synchronize, reopened, edited] |
| 39 | + |
| 40 | +permissions: |
| 41 | + contents: read |
| 42 | + |
| 43 | +concurrency: |
| 44 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 45 | + cancel-in-progress: true |
| 46 | + |
| 47 | +env: |
| 48 | + MAX_TITLE_LENGTH: 50 |
| 49 | + MAX_BODY_LINE_LENGTH: 72 |
| 50 | + MAX_COMMITS_TO_CHECK: 20 |
| 51 | + |
| 52 | +jobs: |
| 53 | + check-commit-message: |
| 54 | + runs-on: ubuntu-latest |
| 55 | + steps: |
| 56 | + - uses: actions/checkout@v4 |
| 57 | + with: |
| 58 | + fetch-depth: 0 |
| 59 | + |
| 60 | + - name: Check commit messages |
| 61 | + shell: bash |
| 62 | + run: | |
| 63 | + # Don't exit on errors - we want to check all commits |
| 64 | + set +e |
| 65 | + |
| 66 | + # Get PR commits with limit |
| 67 | + PR_COMMITS=$(git log --format="%H" ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }} | head -${MAX_COMMITS_TO_CHECK}) |
| 68 | + |
| 69 | + # Check if there are any commits |
| 70 | + if [[ -z "$PR_COMMITS" ]]; then |
| 71 | + echo "## Commit Message Style Check" >> $GITHUB_STEP_SUMMARY |
| 72 | + echo "No commits found in this PR!" >> $GITHUB_STEP_SUMMARY |
| 73 | + exit 0 |
| 74 | + fi |
| 75 | + |
| 76 | + COMMIT_COUNT=$(echo "$PR_COMMITS" | wc -l) |
| 77 | + |
| 78 | + echo "## Commit Message Style Check" >> $GITHUB_STEP_SUMMARY |
| 79 | + echo "⏰ Non-mandatory suggestions for reference only." >> $GITHUB_STEP_SUMMARY |
| 80 | + echo "Checking $COMMIT_COUNT commit(s)" >> $GITHUB_STEP_SUMMARY |
| 81 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 82 | + |
| 83 | + HAS_ISSUES=false |
| 84 | + |
| 85 | + for COMMIT in $PR_COMMITS; do |
| 86 | + # Get commit info with error handling |
| 87 | + TITLE=$(git log --format="%s" -n 1 "$COMMIT" 2>/dev/null || echo "Warning: unable to read commit title") |
| 88 | + BODY=$(git log --format="%b" -n 1 "$COMMIT" 2>/dev/null || echo "") |
| 89 | + RAW=$(git log --format="%B" -n 1 "$COMMIT" 2>/dev/null || echo "Warning: unable to read commit message") |
| 90 | + |
| 91 | + echo "### Checking commit: ${COMMIT:0:8}" >> $GITHUB_STEP_SUMMARY |
| 92 | + echo '```' >> $GITHUB_STEP_SUMMARY |
| 93 | + echo "$RAW" >> $GITHUB_STEP_SUMMARY |
| 94 | + echo '```' >> $GITHUB_STEP_SUMMARY |
| 95 | + |
| 96 | + ISSUES=() |
| 97 | + |
| 98 | + # Check title length |
| 99 | + if [[ ${#TITLE} -gt ${MAX_TITLE_LENGTH} ]]; then |
| 100 | + ISSUES+=("- Title length is ${#TITLE} characters (recommended: ${MAX_TITLE_LENGTH} or less)") |
| 101 | + fi |
| 102 | + |
| 103 | + # Check title capitalization |
| 104 | + if [[ ! $TITLE =~ ^[A-Z] ]]; then |
| 105 | + ISSUES+=("- Title should start with an uppercase letter") |
| 106 | + fi |
| 107 | + |
| 108 | + # Check body is not empty (optional for simple fixes) |
| 109 | + if [[ -z "$BODY" || "$BODY" =~ ^[[:space:]]*$ ]]; then |
| 110 | + # Only suggest body for non-trivial commits |
| 111 | + if [[ ! $TITLE =~ (typo|whitespace|comment|indentation) ]]; then |
| 112 | + ISSUES+=("- Consider adding a commit body for better context") |
| 113 | + fi |
| 114 | + fi |
| 115 | + |
| 116 | + # Check body line length |
| 117 | + if [[ -n "$BODY" ]]; then |
| 118 | + LONG_LINES=0 |
| 119 | + while IFS= read -r line; do |
| 120 | + if [[ ${#line} -gt ${MAX_BODY_LINE_LENGTH} && -n "$line" ]]; then |
| 121 | + ((LONG_LINES++)) |
| 122 | + fi |
| 123 | + done <<< "$BODY" |
| 124 | + |
| 125 | + if [[ $LONG_LINES -gt 0 ]]; then |
| 126 | + ISSUES+=("- Found $LONG_LINES line(s) in body exceeding ${MAX_BODY_LINE_LENGTH} characters") |
| 127 | + fi |
| 128 | + fi |
| 129 | + |
| 130 | + # Skip to next commit if there's a warning reading this one |
| 131 | + if [[ "$TITLE" == "Warning: unable to read commit title" ]]; then |
| 132 | + echo "⚠️ Warning: unable to read commit $COMMIT, skipping..." >> $GITHUB_STEP_SUMMARY |
| 133 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 134 | + continue |
| 135 | + fi |
| 136 | + |
| 137 | + # Output issues |
| 138 | + if [[ ${#ISSUES[@]} -gt 0 ]]; then |
| 139 | + HAS_ISSUES=true |
| 140 | + echo "⚠️ **Suggestions:**" >> $GITHUB_STEP_SUMMARY |
| 141 | + for issue in "${ISSUES[@]}"; do |
| 142 | + echo "$issue" >> $GITHUB_STEP_SUMMARY |
| 143 | + done |
| 144 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 145 | + else |
| 146 | + echo "✅ Commit message follows recommended style" >> $GITHUB_STEP_SUMMARY |
| 147 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 148 | + fi |
| 149 | + done |
| 150 | + |
| 151 | + echo "---" >> $GITHUB_STEP_SUMMARY |
| 152 | + |
| 153 | + if [[ "$HAS_ISSUES" == "true" ]]; then |
| 154 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 155 | + echo "## 📝 Note" >> $GITHUB_STEP_SUMMARY |
| 156 | + echo "These suggestions are for reference only and won't block PR merging." >> $GITHUB_STEP_SUMMARY |
| 157 | + echo "Please refer to the project's .gitmessage template for detailed guidelines." >> $GITHUB_STEP_SUMMARY |
| 158 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 159 | + echo "❌ **Some commits have style suggestions - please review above.**" >> $GITHUB_STEP_SUMMARY |
| 160 | + exit 1 |
| 161 | + else |
| 162 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 163 | + echo "🎉 All commits follow the recommended guidelines!" >> $GITHUB_STEP_SUMMARY |
| 164 | + exit 0 |
| 165 | + fi |
0 commit comments