Skip to content

Commit 24f38a9

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 662c0ca commit 24f38a9

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-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+
echo "exit_code=$?" >> $GITHUB_OUTPUT
29+
cat result.log
30+
31+
- name: Comment PR
32+
# if: steps.check.outputs.exit_code != '0'
33+
uses: mshick/add-pr-comment@v2
34+
with:
35+
message: |
36+
Hello world !
37+
issue: ${{ github.event.pull_request.number }}
38+
repo-token: ${{ secrets.GITHUB_TOKEN }}

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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 changed roles: $CHANGED_ROLES"
23+
MSG=$(head -n 1 "$TMP_MSG_FILE")
24+
if ! grep -qE "^\[($CHANGED_ROLES)\]" <<<"$MSG"; then
25+
echo -e "\nERROR: Commit message must start with one of: [$CHANGED_ROLES]\n"
26+
echo "Example: [reproducer] fix task or [cifmw_helpers] improve code"
27+
exit 1
28+
fi
29+
30+
echo "Commit message prefix is valid."

0 commit comments

Comments
 (0)