diff --git a/.github/workflows/commit-message-validator.yml b/.github/workflows/commit-message-validator.yml new file mode 100644 index 0000000000..075a47e580 --- /dev/null +++ b/.github/workflows/commit-message-validator.yml @@ -0,0 +1,44 @@ +name: Check if commit message body is not too short + +on: + pull_request: + types: [opened, synchronize, edited, reopened] + +jobs: + verify-body-length: + runs-on: ubuntu-latest + # set as non-voting for now. + continue-on-error: true + + permissions: + contents: write + pull-requests: write + repository-projects: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Dump commit message to file + run: | + git fetch origin ${{ github.event.pull_request.head.sha }} + git log -1 --pretty=format:"%B" ${{ github.event.pull_request.head.sha }} > commit-message-file + + - name: Run commit message check + id: bodylength + run: | + set +e + ./scripts/git-check-commit-body-length.sh commit-message-file > result.log 2>&1 + EXIT_CODE=$? + echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT + cat result.log + + - name: Comment on PR if body length check failed + if: steps.bodylength.outputs.exit_code != '0' + uses: peter-evans/create-or-update-comment@v5 + with: + issue-number: ${{ github.event.pull_request.number }} + body-path: ./result.log + reactions: confused diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ed71f2c698..39df571fa1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -45,6 +45,19 @@ Here is an example, based on a common use-case, on how to use those variables oc get openstackdataplane -n {{ cifmw_install_yamls_defaults['NAMESPACE'] }} ~~~ +## A few words about using Git + +Before you make a pull request, make sure that: + +* the title of your git commit message begins with the role + name in brackets: `[my_wonderful_role]` or `(my_wonderful_role)` +* the git commit body message is longer than 10 characters and describes + the reason why you added this change +* sign your git commit using the `Signed-Off-By` option by + adding: `--signoff` or `-s` when using the command: `git commit`. +* if you already make a commit, and you want to add `Signed-Off-By`, + use command: `git commit --amend --signoff` + ### Documentation A new role must get proper documentation. Please edit the README.md located in diff --git a/scripts/git-check-commit-body-length.sh b/scripts/git-check-commit-body-length.sh new file mode 100755 index 0000000000..2ce9e64ea9 --- /dev/null +++ b/scripts/git-check-commit-body-length.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +MSG_FILE="$1" +MIN_BODY_LEN=10 + +# If no file provided, get latest commit message +if [ -z "$MSG_FILE" ]; then + TMP_FILE=$(mktemp) + git log -1 --pretty=format:"%B" >"$TMP_FILE" + MSG_FILE="$TMP_FILE" +fi + +# print commit message +echo -e "Processing commit message:\n" +cat "$MSG_FILE" +echo -e "\nEnd of commit message" + +# 0 = pass, 1 = fail +FAIL_LENGTH=0 +FAIL_SIGNED_OFF_BY=0 + +BODY=$(tail -n +3 "$MSG_FILE" | sed '/^\s*#/d' | sed '/^\s*$/d') +BODY_LEN=$(echo -n "$BODY" | wc -m) + +if [ "$BODY_LEN" -lt "$MIN_BODY_LEN" ]; then + echo -e "\n\n**WARNING: Commit message body is too short (has $BODY_LEN chars, minimum $MIN_BODY_LEN required).**\n" >&2 + echo "Please add a detailed explanation after the subject line." >&2 + FAIL_LENGTH=1 +fi + +if ! grep -qi '^Signed-off-by:' "$MSG_FILE"; then + echo -e "\n\n**WARNING: Missing 'Signed-off-by:' line in commit message.**\n" >&2 + echo "Add: Signed-off-by: Your Name " >&2 + FAIL_SIGNED_OFF_BY=1 +fi + +[ -n "$TMP_FILE" ] && rm -f "$TMP_FILE" + +if [ "$FAIL_LENGTH" -eq 0 ] && [ "$FAIL_SIGNED_OFF_BY" -eq 0 ]; then + echo "Commit message passes all checks." + exit 0 +else + echo -e "\nSome checks failed. See warnings above.\n" + exit 1 +fi