Skip to content

Commit 8e11bb3

Browse files
danpawliksdatko
andcommitted
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]. Co-Authored-By: Szymon Datko <[email protected]> Signed-off-by: Daniel Pawlik <[email protected]>
1 parent 9ccc1ee commit 8e11bb3

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
continue-on-error: true
11+
12+
permissions:
13+
contents: write
14+
pull-requests: write
15+
repository-projects: write
16+
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
23+
- name: Run commit message check
24+
id: prefixcheck
25+
run: |
26+
set +e
27+
./scripts/check-role-prefix.sh > result.log 2>&1
28+
EXIT_CODE=$?
29+
echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT
30+
cat result.log
31+
32+
- name: Comment on PR if prefix check failed
33+
if: steps.prefixcheck.outputs.exit_code != '0'
34+
uses: peter-evans/create-or-update-comment@v5
35+
with:
36+
issue-number: ${{ github.event.pull_request.number }}
37+
body-path: ./result.log
38+
reactions: confused

scripts/check-role-prefix.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
if [ -n "$GITHUB_BASE_REF" ]; then
11+
CHANGED_ROLES=$(git diff "origin/${GITHUB_BASE_REF}" --name-only || true)
12+
else
13+
CHANGED_ROLES=$(git diff --cached --name-only || true)
14+
fi
15+
16+
CHANGED_ROLES=$(echo "$CHANGED_ROLES" | grep '^roles/' | cut -d'/' -f2 | sort -u | xargs | sed 's/ /|/g')
17+
if [ -z "$CHANGED_ROLES" ]; then
18+
echo "No roles modified - skipping check..."
19+
exit 0
20+
fi
21+
22+
echo -e "\n\nDetected changes in roles: **$CHANGED_ROLES**"
23+
MSG=$(head -n 1 "$TMP_MSG_FILE")
24+
ROLE_COUNT=$(echo "$CHANGED_ROLES" | tr '|' '\n' | wc -l)
25+
26+
if [ "$ROLE_COUNT" -eq 1 ]; then
27+
PATTERN="^[\[(]$CHANGED_ROLES[\])]"
28+
else
29+
PATTERN="^[\[(](multiple)[\])]"
30+
fi
31+
32+
if ! grep -qE "$PATTERN" <<<"$MSG"; then
33+
echo -e "\n**ERROR: Commit message must start with:**\n"
34+
if [ "$ROLE_COUNT" -eq 1 ]; then echo -e "\t[$CHANGED_ROLES]\n"; fi
35+
if [ "$ROLE_COUNT" -gt 1 ]; then echo -e "\t(multiple)\n\n"; fi
36+
echo -e "Example commit header:\n"
37+
echo -e "\t-[reproducer] fix task something\n"
38+
echo -e "\t-(cifmw_helpers) improve code\n"
39+
echo -e "\t-[multiple] updated default value"
40+
exit 1
41+
fi
42+
43+
echo "Commit message prefix is valid."

0 commit comments

Comments
 (0)