-
Notifications
You must be signed in to change notification settings - Fork 0
64 lines (55 loc) · 2.07 KB
/
commit-msg-check.yml
File metadata and controls
64 lines (55 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
name: Commit message check
on:
push:
branches: [main]
pull_request:
workflow_dispatch:
permissions:
contents: read
jobs:
check:
name: Reject double-stamped commit subjects
runs-on: ubuntu-latest
timeout-minutes: 2
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Validate commit messages in the pushed range
env:
BEFORE_SHA: ${{ github.event.before }}
AFTER_SHA: ${{ github.event.after }}
PR_BASE_SHA: ${{ github.event.pull_request.base.sha }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
set -euo pipefail
if [ -n "${PR_HEAD_SHA:-}" ] && [ -n "${PR_BASE_SHA:-}" ]; then
range="${PR_BASE_SHA}..${PR_HEAD_SHA}"
elif [ "${BEFORE_SHA:-}" = "0000000000000000000000000000000000000000" ] || [ -z "${BEFORE_SHA:-}" ]; then
range="${AFTER_SHA}~1..${AFTER_SHA}"
else
range="${BEFORE_SHA}..${AFTER_SHA}"
fi
echo "Validating commits in range: $range"
pattern='\([0-9]{4}\.[0-9]+\.[0-9]+\.[0-9]+(-([A-Fa-f0-9]{4}|beta))?\)'
failures=0
while IFS= read -r line; do
sha="${line%% *}"
subject="${line#* }"
count=$( ( printf '%s\n' "$subject" | grep -oE "$pattern" || true ) | grep -c . || true )
count=${count//[^0-9]/}
count=${count:-0}
if [ "$count" -gt 1 ]; then
echo "::error::Commit $sha has $count build-version stamps in its subject:"
echo "::error:: $subject"
failures=$((failures + 1))
fi
done < <(git log --format='%H %s' "$range")
if [ "$failures" -gt 0 ]; then
echo ""
echo "::error::$failures commit(s) carry more than one build-version stamp."
echo "Reword the offending commits to drop stale version trailers."
exit 1
fi
echo "All commit subjects in $range carry at most one build-version stamp."