Skip to content

Commit d019604

Browse files
committed
CI: add commit message style check workflow
This commit introduces a new GitHub Actions workflow that checks commit messages in pull requests against recommended style guidelines. The workflow verifies: - Title length (50 characters or less) - Title starts with uppercase letter - Body is not empty - Body lines don't exceed 72 characters The check provides helpful suggestions to contributors without preventing PR merging.
1 parent d0e9daf commit d019604

1 file changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
jobs:
48+
check-commit-message:
49+
runs-on: ubuntu-latest
50+
steps:
51+
- uses: actions/checkout@v4
52+
with:
53+
fetch-depth: 0
54+
55+
- name: Check commit messages
56+
shell: bash
57+
run: |
58+
PR_COMMITS=$(git log --format="%H" ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }})
59+
60+
echo "## Commit Message Style Check" >> $GITHUB_STEP_SUMMARY
61+
echo "Non-mandatory suggestions for reference only" >> $GITHUB_STEP_SUMMARY
62+
echo "" >> $GITHUB_STEP_SUMMARY
63+
64+
HAS_ISSUES=false
65+
66+
for COMMIT in $PR_COMMITS; do
67+
TITLE=$(git log --format="%s" -n 1 $COMMIT)
68+
BODY=$(git log --format="%b" -n 1 $COMMIT)
69+
RAW=$(git log --format="%B" -n 1 $COMMIT)
70+
71+
echo "### Checking commit: ${COMMIT:0:8}" >> $GITHUB_STEP_SUMMARY
72+
echo '```' >> $GITHUB_STEP_SUMMARY
73+
echo "$RAW" >> $GITHUB_STEP_SUMMARY
74+
echo '```' >> $GITHUB_STEP_SUMMARY
75+
76+
ISSUES=()
77+
78+
# Check title length and capitalization
79+
if [[ ${#TITLE} -gt 50 ]]; then
80+
ISSUES+=("- Title length is ${#TITLE} characters (recommended: 50 or less)")
81+
fi
82+
83+
if [[ ! $TITLE =~ ^[A-Z] ]]; then
84+
ISSUES+=("- Title should start with an uppercase letter")
85+
fi
86+
87+
# Check body is not empty
88+
if [[ -z "$BODY" || "$BODY" =~ ^[[:space:]]*$ ]]; then
89+
ISSUES+=("- Commit body should not be empty")
90+
fi
91+
92+
# Check body line length
93+
if [[ -n "$BODY" ]]; then
94+
LONG_LINES=0
95+
while IFS= read -r line; do
96+
if [[ ${#line} -gt 72 && -n "$line" ]]; then
97+
((LONG_LINES++))
98+
fi
99+
done <<< "$BODY"
100+
101+
if [[ $LONG_LINES -gt 0 ]]; then
102+
ISSUES+=("- Found $LONG_LINES line(s) in body exceeding 72 characters")
103+
fi
104+
fi
105+
106+
# Output issues
107+
if [[ ${#ISSUES[@]} -gt 0 ]]; then
108+
HAS_ISSUES=true
109+
echo "#### ⚠️ Suggestions:" >> $GITHUB_STEP_SUMMARY
110+
for issue in "${ISSUES[@]}"; do
111+
echo "$issue" >> $GITHUB_STEP_SUMMARY
112+
done
113+
echo "" >> $GITHUB_STEP_SUMMARY
114+
else
115+
echo "✅ Commit message follows recommended style" >> $GITHUB_STEP_SUMMARY
116+
echo "" >> $GITHUB_STEP_SUMMARY
117+
fi
118+
done
119+
120+
if [[ "$HAS_ISSUES" == "true" ]]; then
121+
echo "### Note" >> $GITHUB_STEP_SUMMARY
122+
echo "These suggestions are for reference only and won't block PR merging. Consider using the project's .gitmessage template." >> $GITHUB_STEP_SUMMARY
123+
fi

0 commit comments

Comments
 (0)