Skip to content

Commit 4913ce2

Browse files
danpawlikevallesp
authored andcommitted
Add git-commit-msg-hook script
The script is verifying if the minimum body length contains 10 characters. Also raise a warning if there is no: "Signed-Off-By". Signed-off-by: Daniel Pawlik <[email protected]>
1 parent e6ccd23 commit 4913ce2

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Check if commit message body is not too short
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, edited, reopened]
6+
7+
jobs:
8+
verify-body-length:
9+
runs-on: ubuntu-latest
10+
# set as non-voting for now.
11+
continue-on-error: true
12+
13+
permissions:
14+
contents: write
15+
pull-requests: write
16+
repository-projects: write
17+
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Dump commit message to file
25+
run: |
26+
git fetch origin ${{ github.event.pull_request.head.sha }}
27+
git log -1 --pretty=format:"%B" ${{ github.event.pull_request.head.sha }} > commit-message-file
28+
29+
- name: Run commit message check
30+
id: bodylength
31+
run: |
32+
set +e
33+
./scripts/git-check-commit-body-length.sh commit-message-file > result.log 2>&1
34+
EXIT_CODE=$?
35+
echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT
36+
cat result.log
37+
38+
- name: Comment on PR if body length check failed
39+
if: steps.bodylength.outputs.exit_code != '0'
40+
uses: peter-evans/create-or-update-comment@v5
41+
with:
42+
issue-number: ${{ github.event.pull_request.number }}
43+
body-path: ./result.log
44+
reactions: confused

CONTRIBUTING.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ Here is an example, based on a common use-case, on how to use those variables
4545
oc get openstackdataplane -n {{ cifmw_install_yamls_defaults['NAMESPACE'] }}
4646
~~~
4747

48+
## A few words about using Git
49+
50+
Before you make a pull request, make sure that:
51+
52+
* the title of your git commit message begins with the role
53+
name in brackets: `[my_wonderful_role]` or `(my_wonderful_role)`
54+
* the git commit body message is longer than 10 characters and describes
55+
the reason why you added this change
56+
* sign your git commit using the `Signed-Off-By` option by
57+
adding: `--signoff` or `-s` when using the command: `git commit`.
58+
* if you already make a commit, and you want to add `Signed-Off-By`,
59+
use command: `git commit --amend --signoff`
60+
4861
### Documentation
4962

5063
A new role must get proper documentation. Please edit the README.md located in
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
3+
MSG_FILE="$1"
4+
MIN_BODY_LEN=10
5+
6+
# If no file provided, get latest commit message
7+
if [ -z "$MSG_FILE" ]; then
8+
TMP_FILE=$(mktemp)
9+
git log -1 --pretty=format:"%B" >"$TMP_FILE"
10+
MSG_FILE="$TMP_FILE"
11+
fi
12+
13+
# print commit message
14+
echo -e "Processing commit message:\n"
15+
cat "$MSG_FILE"
16+
echo -e "\nEnd of commit message"
17+
18+
# 0 = pass, 1 = fail
19+
FAIL_LENGTH=0
20+
FAIL_SIGNED_OFF_BY=0
21+
22+
BODY=$(tail -n +3 "$MSG_FILE" | sed '/^\s*#/d' | sed '/^\s*$/d')
23+
BODY_LEN=$(echo -n "$BODY" | wc -m)
24+
25+
if [ "$BODY_LEN" -lt "$MIN_BODY_LEN" ]; then
26+
echo -e "\n\n**WARNING: Commit message body is too short (has $BODY_LEN chars, minimum $MIN_BODY_LEN required).**\n" >&2
27+
echo "Please add a detailed explanation after the subject line." >&2
28+
FAIL_LENGTH=1
29+
fi
30+
31+
if ! grep -qi '^Signed-off-by:' "$MSG_FILE"; then
32+
echo -e "\n\n**WARNING: Missing 'Signed-off-by:' line in commit message.**\n" >&2
33+
echo "Add: Signed-off-by: Your Name <[email protected]>" >&2
34+
FAIL_SIGNED_OFF_BY=1
35+
fi
36+
37+
[ -n "$TMP_FILE" ] && rm -f "$TMP_FILE"
38+
39+
if [ "$FAIL_LENGTH" -eq 0 ] && [ "$FAIL_SIGNED_OFF_BY" -eq 0 ]; then
40+
echo "Commit message passes all checks."
41+
exit 0
42+
else
43+
echo -e "\nSome checks failed. See warnings above.\n"
44+
exit 1
45+
fi

0 commit comments

Comments
 (0)