Skip to content

Commit bf98095

Browse files
danpawliksdatko
authored 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 1638776 commit bf98095

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
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

roles/cifmw_helpers/tasks/include_dir.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
# This is a workaround for reading Ansible yaml files,
33
# that instead of have clear values, it uses jinja2 variables,
4-
# so reading the file and parse as fact does not work.
4+
# so reading the file and parse as fact does not work
55

66
- name: Check directory is available
77
ansible.builtin.stat:

scripts/check-role-prefix.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
# shellcheck disable=SC1087
28+
PATTERN="^[\[(]$CHANGED_ROLES[\])]"
29+
else
30+
PATTERN="^[\[(](multiple)[\])]"
31+
fi
32+
33+
if ! grep -qE "$PATTERN" <<<"$MSG"; then
34+
echo -e "\n**ERROR: Commit message must start with:**\n"
35+
if [ "$ROLE_COUNT" -eq 1 ]; then echo -e "\t[$CHANGED_ROLES]\n"; fi
36+
if [ "$ROLE_COUNT" -gt 1 ]; then echo -e "\t(multiple)\n\n"; fi
37+
echo -e "Example commit header:\n"
38+
echo -e "\t-[reproducer] fix task something\n"
39+
echo -e "\t-(cifmw_helpers) improve code\n"
40+
echo -e "\t-[multiple] updated default value"
41+
exit 1
42+
fi
43+
44+
echo "Commit message prefix is valid."

0 commit comments

Comments
 (0)