Skip to content

Commit cfcb453

Browse files
evallespclaude
authored andcommitted
Update check-role-prefix script
Now the script can handle a PR containing more than one commit where each commit might update or not roles. Now instead of checking last commit, it checks the whole commits modified and iterates over them. Signed-off-by: Enrique Vallespi Gil <evallesp@redhat.com> Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Enrique Vallespi Gil <evallesp@redhat.com>
1 parent bd5a332 commit cfcb453

4 files changed

Lines changed: 62 additions & 41 deletions

File tree

.github/workflows/verify-pr-prefix.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ jobs:
2020
with:
2121
fetch-depth: 0
2222

23-
- name: Dump commit message to file
24-
run: |
25-
git fetch origin ${{ github.event.pull_request.head.sha }}
26-
git log -1 --pretty=format:"%B" ${{ github.event.pull_request.head.sha }} | head -n1 > commit-message-file
27-
28-
- name: Run commit message check
23+
- name: Check all commits in PR
2924
id: prefixcheck
3025
run: |
31-
./scripts/check-role-prefix.sh commit-message-file
26+
./scripts/check-role-prefix.sh

scripts/check-role-prefix.sh

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,73 @@
11
#!/bin/bash
22

3-
# Get the latest commit message file
4-
TMP_MSG_FILE="$1"
3+
# Validate that each commit in the PR has the correct role prefix
4+
# based on the roles modified in that specific commit.
55

6-
if [ -z "$TMP_MSG_FILE" ]; then
7-
TMP_MSG_FILE=$(mktemp)
8-
git log -1 --pretty=format:"%B" | head -n1
6+
if [ -z "$GITHUB_BASE_REF" ]; then
7+
echo "Not running in GitHub Actions - skipping check"
8+
exit 0
99
fi
1010

11-
echo "Checking latest commit message:"
12-
cat "$TMP_MSG_FILE"
11+
echo "Checking all commits in PR against base: origin/${GITHUB_BASE_REF}"
12+
echo ""
1313

14-
if [ -n "$GITHUB_BASE_REF" ]; then
15-
CHANGED_ROLES=$(git diff "origin/${GITHUB_BASE_REF}" --name-only || true)
16-
else
17-
CHANGED_ROLES=$(git diff --cached --name-only || true)
18-
fi
14+
# Get all commits in the PR
15+
COMMITS=$(git rev-list origin/${GITHUB_BASE_REF}..HEAD)
1916

20-
CHANGED_ROLES=$(echo "$CHANGED_ROLES" | grep '^roles/' | cut -d'/' -f2 | sort -u | xargs | sed 's/ /|/g')
21-
if [ -z "$CHANGED_ROLES" ]; then
22-
echo "No roles modified - skipping check..."
17+
if [ -z "$COMMITS" ]; then
18+
echo "No commits to check"
2319
exit 0
2420
fi
2521

26-
echo -e "\n\nDetected changes in roles: **$CHANGED_ROLES**"
27-
MSG=$(head -n 1 "$TMP_MSG_FILE")
28-
ROLE_COUNT=$(echo "$CHANGED_ROLES" | tr '|' '\n' | wc -l)
22+
FAILED=0
2923

30-
if [ "$ROLE_COUNT" -eq 1 ]; then
31-
# shellcheck disable=SC2016
32-
ESCAPED_ROLE=$(printf '%s\n' "$CHANGED_ROLES" | sed 's/[]\.*^$()+?{|]/\\&/g')
33-
PATTERN="^[[(]${ESCAPED_ROLE}[])]"
34-
else
35-
PATTERN="^[[(](multiple)[])]"
36-
fi
24+
# Check each commit individually
25+
while IFS= read -r COMMIT; do
26+
MSG=$(git log -1 --pretty=format:"%s" "$COMMIT")
27+
echo "Checking commit ${COMMIT:0:8}: $MSG"
28+
29+
# Get roles changed in THIS commit only
30+
CHANGED_ROLES=$(git diff-tree --no-commit-id --name-only -r "$COMMIT" | grep '^roles/' | cut -d'/' -f2 | sort -u | xargs | sed 's/ /|/g')
31+
32+
if [ -z "$CHANGED_ROLES" ]; then
33+
echo " No roles modified - skipping"
34+
echo ""
35+
continue
36+
fi
37+
38+
echo " Changed roles: $CHANGED_ROLES"
39+
40+
ROLE_COUNT=$(echo "$CHANGED_ROLES" | tr '|' '\n' | wc -l)
41+
42+
if [ "$ROLE_COUNT" -eq 1 ]; then
43+
# shellcheck disable=SC2016
44+
ESCAPED_ROLE=$(printf '%s\n' "$CHANGED_ROLES" | sed 's/[]\.*^$()+?{|]/\\&/g')
45+
PATTERN="^[[(]${ESCAPED_ROLE}[])]"
46+
else
47+
PATTERN="^[[(](multiple)[])]"
48+
fi
49+
50+
if ! grep -qE "$PATTERN" <<<"$MSG"; then
51+
echo ""
52+
echo " **ERROR: Commit message must start with:**"
53+
if [ "$ROLE_COUNT" -eq 1 ]; then
54+
echo " [$CHANGED_ROLES]"
55+
else
56+
echo " (multiple)"
57+
fi
58+
echo ""
59+
FAILED=1
60+
else
61+
echo " ✓ Valid prefix"
62+
fi
63+
echo ""
64+
done <<< "$COMMITS"
3765

38-
if ! grep -qE "$PATTERN" <<<"$MSG"; then
39-
echo -e "\n**ERROR: Commit message must start with:**\n"
40-
if [ "$ROLE_COUNT" -eq 1 ]; then echo -e "\t[$CHANGED_ROLES]\n"; fi
41-
if [ "$ROLE_COUNT" -gt 1 ]; then echo -e "\t(multiple)\n\n"; fi
42-
echo -e "Example commit header:\n"
43-
echo -e "\t-[reproducer] fix task something\n"
44-
echo -e "\t-(cifmw_helpers) improve code\n"
45-
echo -e "\t-[multiple] updated default value"
66+
if [ $FAILED -eq 1 ]; then
67+
echo "Example commit messages:"
68+
echo " [reproducer] fix task something"
69+
echo " (multiple) updated default value"
4670
exit 1
4771
fi
4872

49-
echo "Commit message prefix is valid."
73+
echo "Each commit message prefix is valid."

zuul.d/edpm.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- ^CONTRIBUTING.md
2323
- ^docs/*
2424
- ^OWNERS*
25+
- ^scripts/check-role-prefix.sh
2526
- roles/.*/molecule/.*
2627
vars:
2728
crc_parameters: "--memory 32000 --disk-size 240 --cpus 12"

zuul.d/edpm_multinode.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@
323323
- ^CONTRIBUTING.md
324324
- ^docs/*
325325
- ^OWNERS*
326+
- ^scripts/check-role-prefix.sh
326327
- roles/.*/molecule/.*
327328
vars:
328329
cifmw_extras:

0 commit comments

Comments
 (0)