Skip to content

Commit fb11d54

Browse files
committed
Add Github action that verify PR and 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]. Signed-off-by: Daniel Pawlik <[email protected]>
1 parent e97a352 commit fb11d54

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
# Get the latest commit message file
20+
TMP_MSG_FILE=$(mktemp)
21+
git log -1 --pretty=format:"%s%n%n%b" > "$TMP_MSG_FILE"
22+
23+
echo "Checking latest commit message:"
24+
cat "$TMP_MSG_FILE"
25+
26+
# Detect changed roles
27+
CHANGED_ROLES=$(git diff --cached --name-only || true)
28+
if [ -z "$CHANGED_ROLES" ]; then
29+
CHANGED_ROLES=$(git diff HEAD~1 --name-only || true)
30+
fi
31+
32+
CHANGED_ROLES=$(echo "$CHANGED_ROLES" | grep '^roles/' | cut -d'/' -f2 | sort -u | xargs | sed 's/ /|/g')
33+
34+
if [ -z "$CHANGED_ROLES" ]; then
35+
echo "No roles modified - skipping check..."
36+
exit 0
37+
fi
38+
39+
echo "Detected changed roles: $CHANGED_ROLES"
40+
MSG=$(head -n 1 "$TMP_MSG_FILE")
41+
if ! grep -qE "^\[($CHANGED_ROLES)\]" <<<"$MSG"; then
42+
echo "::error::Commit message must start with one of: [$CHANGED_ROLES]"
43+
echo "Example: [reproducer] fix task or [cifmw_helpers] improve code"
44+
exit 1
45+
fi
46+
47+
echo "Commit message prefix is valid."

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ repos:
1212
- id: check-symlinks
1313
- id: debug-statements
1414

15+
# - repo: local
16+
# hooks:
17+
# - id: check-gh-commit-role-prefix
18+
# name: Check role prefix is in commit message
19+
# entry: bash scripts/check-role-prefix.sh
20+
# language: system
21+
# # stages: [commit-msg]
22+
1523
# First execution of shellcheck reports all findings without failing
1624
# Second execution outputs nothing and fails if any finding meets the
1725
# defined severity.

scripts/check-role-prefix.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
MSG_FILE="$1"
6+
MSG=$(head -n 1 "$MSG_FILE")
7+
CHANGED_ROLES=$(git diff --cached --name-only | grep '^roles/' | cut -d'/' -f2 | sort -u | xargs | sed 's/ /|/g')
8+
9+
if [ -z "$CHANGED_ROLES" ]; then
10+
exit 0
11+
fi
12+
13+
if ! grep -qE "^\[($CHANGED_ROLES)\]" <<<"$MSG"; then
14+
echo -e "\nError: Commit message must start with one of role: [$CHANGED_ROLES]"
15+
exit 1
16+
fi

0 commit comments

Comments
 (0)