Skip to content

Commit c066472

Browse files
committed
ci(commitlint): improve checking of merge commits (#409)
1 parent 1561a4b commit c066472

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

.commitlintrc.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
rules:
22
body-case: [2, always, sentence-case]
3-
body-full-stop: [2, always]
3+
body-full-stop: [1, always]
44
body-leading-blank: [2, always]
55
body-max-line-length: [2, always, 72]
66
footer-leading-blank: [2, always]

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
- name: Check commit message compliance of the pull request
3434
if: github.event_name == 'pull_request'
3535
run: |
36-
./run-tests.sh --check-commitlint ${{ github.event.pull_request.head.sha }}~${{ github.event.pull_request.commits }} ${{ github.event.pull_request.head.sha }} ${{ github.event.pull_request.number }}
36+
./run-tests.sh --check-commitlint ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} ${{ github.event.pull_request.number }}
3737
3838
lint-shellcheck:
3939
runs-on: ubuntu-24.04

run-tests.sh

+20-7
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,28 @@ check_commitlint () {
1616
npx commitlint --from="$from" --to="$to"
1717
found=0
1818
while IFS= read -r line; do
19-
if echo "$line" | grep -qP "\(\#$pr\)$"; then
20-
true
21-
elif echo "$line" | grep -qP "^chore\(.*\): release"; then
22-
true
23-
else
24-
echo "✖ Headline does not end by '(#$pr)' PR number: $line"
19+
commit_hash=$(echo "$line" | cut -d ' ' -f 1)
20+
commit_title=$(echo "$line" | cut -d ' ' -f 2-)
21+
commit_number_of_parents=$(git rev-list --parents "$commit_hash" -n1 | awk '{print NF-1}')
22+
# (i) skip checking release commits generated by Release Please
23+
if [ "$commit_number_of_parents" -le 1 ] && echo "$commit_title" | grep -qP "^chore\(.*\): release"; then
24+
continue
25+
fi
26+
# (ii) check presence of PR number
27+
if ! echo "$commit_title" | grep -qP "\(\#$pr\)$"; then
28+
echo "✖ Headline does not end by '(#$pr)' PR number: $commit_title"
2529
found=1
2630
fi
27-
done < <(git log "$from..$to" --format="%s")
31+
# (iii) check absence of merge commits in feature branches
32+
if [ "$commit_number_of_parents" -gt 1 ]; then
33+
if echo "$commit_title" | grep -qP "^chore\(.*\): merge "; then
34+
break # skip checking maint-to-master merge commits
35+
else
36+
echo "✖ Merge commits are not allowed in feature branches: $commit_title"
37+
found=1
38+
fi
39+
fi
40+
done < <(git log "$from..$to" --format="%H %s")
2841
if [ $found -gt 0 ]; then
2942
exit 1
3043
fi

0 commit comments

Comments
 (0)