|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | +# |
| 13 | +name: 'Commit Message Check' |
| 14 | +on: |
| 15 | + pull_request: |
| 16 | + types: [opened, synchronize, reopened, labeled, unlabeled] |
| 17 | + |
| 18 | +jobs: |
| 19 | + commits-check: |
| 20 | + name: Check commit messages |
| 21 | + runs-on: ubuntu-latest |
| 22 | + steps: |
| 23 | + - uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Validate commit message style |
| 28 | + shell: bash |
| 29 | + run: | |
| 30 | + set -e |
| 31 | + has_errors=0 |
| 32 | + COMMIT_URL="https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md" |
| 33 | +
|
| 34 | + # Determine commit range for PR or fallback to origin/master |
| 35 | + if [ -n "${GITHUB_BASE_REF}" ]; then |
| 36 | + base_ref="origin/${GITHUB_BASE_REF}" |
| 37 | + else |
| 38 | + base_ref="origin/master" |
| 39 | + fi |
| 40 | +
|
| 41 | + base_commit=$(git merge-base HEAD ${base_ref}) |
| 42 | +
|
| 43 | + echo "Checking commits from ${base_commit} to HEAD" |
| 44 | +
|
| 45 | + for commit in $(git rev-list --no-merges ${base_commit}..HEAD); do |
| 46 | + short_sha=$(git rev-parse --short=7 $commit) |
| 47 | + subject=$(git log -1 --pretty=format:%s $commit) |
| 48 | + body="$(git log -1 --pretty=format:%b $commit | sed '/^Signed-off-by:/d')" |
| 49 | + signedoff="$(git log -1 --pretty=format:%b $commit | sed -n '/^Signed-off-by:/p')" |
| 50 | + blacklist_regex='/VELAPLATO-|WIP:/Ip' |
| 51 | + blacklist="$(git log -1 --pretty=format:%s%b $commit | sed -n -E $blacklist_regex)" |
| 52 | +
|
| 53 | + # check for commit length |
| 54 | + if [ ${#subject} -gt 72 ]; then |
| 55 | + echo "::error::Commit $short_sha subject too long (${#subject} > 72):" |
| 56 | + echo "$subject" |
| 57 | + has_errors=1 |
| 58 | + fi |
| 59 | +
|
| 60 | + # check for subsystem |
| 61 | + if [[ "$subject" != *:* ]]; then |
| 62 | + echo "::error::Commit $short_sha subject missing colon (e.g. 'subsystem: msg')" |
| 63 | + echo "$subject" |
| 64 | + has_errors=1 |
| 65 | + fi |
| 66 | +
|
| 67 | + # check for Signed-off-by line |
| 68 | + if [ -z "$signedoff" ]; then |
| 69 | + echo "::error::Signed-off-by line is missing" |
| 70 | + has_errors=1 |
| 71 | + fi |
| 72 | +
|
| 73 | + # check for blacklisted expressions |
| 74 | + if [ "$blacklist" ]; then |
| 75 | + echo "::error::Blacklisted expression found" |
| 76 | + echo "$blacklist" |
| 77 | + has_errors=1 |
| 78 | + fi |
| 79 | +
|
| 80 | + # check for commit message |
| 81 | + if [ -z "$body" ]; then |
| 82 | + echo "::error::Commit $short_sha body is missing" |
| 83 | + has_errors=1 |
| 84 | + else |
| 85 | + line_num=0 |
| 86 | + while IFS= read -r line; do |
| 87 | + line_num=$((line_num + 1)) |
| 88 | + if [ ${#line} -gt 72 ]; then |
| 89 | + echo "::error::Commit $short_sha body line $line_num too long (${#line} > 72):" |
| 90 | + echo "$line" |
| 91 | + has_errors=1 |
| 92 | + fi |
| 93 | + done <<< "$body" |
| 94 | + fi |
| 95 | +
|
| 96 | + echo "" |
| 97 | + done |
| 98 | +
|
| 99 | + if [ "$has_errors" -eq 1 ]; then |
| 100 | + echo "::error::Commit message check failed." |
| 101 | + echo "For contributing guidelines, see:" |
| 102 | + echo " $COMMIT_URL" |
| 103 | + exit 1 |
| 104 | + else |
| 105 | + echo "All commit messages pass style rules." |
| 106 | + fi |
0 commit comments