Skip to content

Commit ed24707

Browse files
ci: add GitHub Action to validate commit message style
This workflow checks all commits between the current branch and upstream/master. It enforces subject length, colon usage in the subject line, and body line length. This helps maintain consistency and readability in the project's history.
1 parent 4659d19 commit ed24707

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

.github/workflows/compliance_check.yml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,73 @@ jobs:
106106
shell: bash
107107
run: |
108108
.github/check_doxygen.py
109+
110+
commits-check:
111+
name: Check commit messages
112+
runs-on: ubuntu-latest
113+
steps:
114+
- uses: actions/checkout@v3
115+
with:
116+
fetch-depth: 0
117+
118+
- name: Validate commit message style
119+
shell: bash
120+
run: |
121+
set -e
122+
has_errors=0
123+
COMMIT_URL="https://cwiki.apache.org/confluence/display/MYNEWT/Contributing+to+Apache+Mynewt"
124+
125+
# Determine commit range for PR or fallback to origin/master
126+
if [ -n "${GITHUB_BASE_REF}" ]; then
127+
base_ref="origin/${GITHUB_BASE_REF}"
128+
else
129+
base_ref="origin/master"
130+
fi
131+
132+
base_commit=$(git merge-base HEAD ${base_ref})
133+
134+
echo "Checking commits from ${base_commit} to HEAD"
135+
136+
for commit in $(git rev-list --no-merges ${base_commit}..HEAD); do
137+
short_sha=$(git rev-parse --short=7 $commit)
138+
subject=$(git log -1 --pretty=format:%s $commit)
139+
body=$(git log -1 --pretty=format:%b $commit)
140+
141+
if [ ${#subject} -gt 72 ]; then
142+
echo "Commit $short_sha subject too long (${#subject} > 72):"
143+
echo "$subject"
144+
has_errors=1
145+
fi
146+
147+
if [[ "$subject" != *:* ]]; then
148+
echo "Commit $short_sha subject missing colon (e.g. 'subsystem: msg')"
149+
echo "$subject"
150+
has_errors=1
151+
fi
152+
153+
if [ -z "$body" ]; then
154+
echo "Commit $short_sha body is missing"
155+
has_errors=1
156+
else
157+
line_num=0
158+
while IFS= read -r line; do
159+
line_num=$((line_num + 1))
160+
if [ ${#line} -gt 72 ]; then
161+
echo "Commit $short_sha body line $line_num too long (${#line} > 72):"
162+
echo "$line"
163+
has_errors=1
164+
fi
165+
done <<< "$body"
166+
fi
167+
168+
echo ""
169+
done
170+
171+
if [ "$has_errors" -eq 1 ]; then
172+
echo "Commit message check failed."
173+
echo "For contributing guidelines, see:"
174+
echo " $COMMIT_URL"
175+
exit 1
176+
else
177+
echo "All commit messages pass style rules."
178+
fi

0 commit comments

Comments
 (0)