|
| 1 | +name: 'Commit Message Check' |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + types: [opened, synchronize, reopened, labeled, unlabeled] |
| 5 | + |
| 6 | +jobs: |
| 7 | + commits-check: |
| 8 | + name: Check commit messages |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - uses: actions/checkout@v4 |
| 12 | + with: |
| 13 | + fetch-depth: 0 |
| 14 | + |
| 15 | + - name: Validate commit message style |
| 16 | + shell: bash |
| 17 | + run: | |
| 18 | + set -e |
| 19 | + has_errors=0 |
| 20 | + COMMIT_URL="https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md" |
| 21 | +
|
| 22 | + # Determine commit range for PR or fallback to origin/master |
| 23 | + if [ -n "${GITHUB_BASE_REF}" ]; then |
| 24 | + base_ref="origin/${GITHUB_BASE_REF}" |
| 25 | + else |
| 26 | + base_ref="origin/master" |
| 27 | + fi |
| 28 | +
|
| 29 | + base_commit=$(git merge-base HEAD ${base_ref}) |
| 30 | +
|
| 31 | + echo "Checking commits from ${base_commit} to HEAD" |
| 32 | +
|
| 33 | + for commit in $(git rev-list --no-merges ${base_commit}..HEAD); do |
| 34 | + short_sha=$(git rev-parse --short=7 $commit) |
| 35 | + subject=$(git log -1 --pretty=format:%s $commit) |
| 36 | + body="$(git log -1 --pretty=format:%b $commit | sed '/^Signed-off-by:/d')" |
| 37 | + signedoff="$(git log -1 --pretty=format:%b $commit | sed -n '/^Signed-off-by:/p')" |
| 38 | +
|
| 39 | + # check for commit length |
| 40 | + if [ ${#subject} -gt 72 ]; then |
| 41 | + echo "::error::Commit $short_sha subject too long (${#subject} > 72):" |
| 42 | + echo "$subject" |
| 43 | + has_errors=1 |
| 44 | + fi |
| 45 | +
|
| 46 | + # check for subsystem |
| 47 | + if [[ "$subject" != *:* ]]; then |
| 48 | + echo "::error::Commit $short_sha subject missing colon (e.g. 'subsystem: msg')" |
| 49 | + echo "$subject" |
| 50 | + has_errors=1 |
| 51 | + fi |
| 52 | +
|
| 53 | + # check for Signed-off-by line |
| 54 | + if [ -z "$signedoff" ]; then |
| 55 | + echo "::error::Signed-off-by line is missing" |
| 56 | + has_errors=1 |
| 57 | + fi |
| 58 | +
|
| 59 | + # check for commit message |
| 60 | + if [ -z "$body" ]; then |
| 61 | + echo "::error::Commit $short_sha body is missing" |
| 62 | + has_errors=1 |
| 63 | + else |
| 64 | + line_num=0 |
| 65 | + while IFS= read -r line; do |
| 66 | + line_num=$((line_num + 1)) |
| 67 | + if [ ${#line} -gt 72 ]; then |
| 68 | + echo "::error::Commit $short_sha body line $line_num too long (${#line} > 72):" |
| 69 | + echo "$line" |
| 70 | + has_errors=1 |
| 71 | + fi |
| 72 | + done <<< "$body" |
| 73 | + fi |
| 74 | +
|
| 75 | + echo "" |
| 76 | + done |
| 77 | +
|
| 78 | + if [ "$has_errors" -eq 1 ]; then |
| 79 | + echo "::error::Commit message check failed." |
| 80 | + echo "For contributing guidelines, see:" |
| 81 | + echo " $COMMIT_URL" |
| 82 | + exit 1 |
| 83 | + else |
| 84 | + echo "All commit messages pass style rules." |
| 85 | + fi |
0 commit comments