Skip to content

Commit 3215fc3

Browse files
committed
Add Github action that verify commit message prefix
If the PR is related to some role, it would verify if in the title or commit message header there is prefix set. For example, if change was done for roles/cifmw_helpers, it should verify if the title or commit message header contains [cifmw_helpers]. Also there is a dedicated script: scripts/check-role-prefix.sh that gives possibility to test the commit message locally. Signed-off-by: Daniel Pawlik <[email protected]>
1 parent 662c0ca commit 3215fc3

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Check if commit message contains role prefix
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, edited, reopened]
6+
7+
jobs:
8+
verify-prefix:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@v4
14+
with:
15+
fetch-depth: 0
16+
17+
- name: Run commit message check
18+
run: |
19+
./scripts/check-role-prefix.sh

scripts/check-role-prefix.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# Get the latest commit message file
4+
TMP_MSG_FILE=$(mktemp)
5+
git log -1 --pretty=format:"%s%n%n%b" >"$TMP_MSG_FILE"
6+
7+
echo "Checking latest commit message:"
8+
cat "$TMP_MSG_FILE"
9+
10+
CHANGED_ROLES=$(git diff --cached --name-only || true)
11+
if [ -z "$CHANGED_ROLES" ]; then
12+
CHANGED_ROLES=$(git diff HEAD~1 --name-only || true)
13+
fi
14+
15+
CHANGED_ROLES=$(echo "$CHANGED_ROLES" | grep '^roles/' | cut -d'/' -f2 | sort -u | xargs | sed 's/ /|/g')
16+
if [ -z "$CHANGED_ROLES" ]; then
17+
echo "No roles modified - skipping check..."
18+
exit 0
19+
fi
20+
21+
echo -e "\n\nDetected changed roles: $CHANGED_ROLES"
22+
MSG=$(head -n 1 "$TMP_MSG_FILE")
23+
if ! grep -qE "^\[($CHANGED_ROLES)\]" <<<"$MSG"; then
24+
echo -e "\nERROR: Commit message must start with one of: [$CHANGED_ROLES]\n"
25+
echo "Example: [reproducer] fix task or [cifmw_helpers] improve code"
26+
exit 1
27+
fi
28+
29+
echo "Commit message prefix is valid."

0 commit comments

Comments
 (0)