From 16f9b46fd893796e5bb3e10be19070bdc1513c44 Mon Sep 17 00:00:00 2001 From: Jason Naylor Date: Wed, 27 Mar 2024 11:57:04 -0700 Subject: [PATCH] This adds a commit message checker, which should fail on this commit subject too long Change-Id: I47d33b272fff7d93838560ee5e1225b26c956a15 --- .github/workflows/CI.yml | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index e18de745b8..bf3de6c384 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -40,6 +40,53 @@ jobs: 2.1.x 3.1.x 5.0.x + - name: Warn of commit message violations + id: commit_message_check + shell: bash + run: | + # This script checks that the commit message is formatted according to the standard: + # - First line: no more than 62 chars and not ending with period (or if it is the + # only line: no more than 70 chars) + # - Second line: empty + # - Following lines: no more than 70 chars + FIRSTLINE_LEN=62 + LINE_LEN=70 + ERRORHEADER="⚠️ [POLICY] Your commit message is not formatted correctly." + OutputError() + { + echo $ERRORHEADER >> $GITHUB_STEP_SUMMARY + echo $1 >> $GITHUB_STEP_SUMMARY + } + # git strips trailing whitespace in the commit message prior to calling this hook + COUNT=0 + cat "$1" | grep --invert-match -E "^#" | while read LINE + do + COUNT=$((COUNT + 1)) + LEN=$(echo "$LINE" | tr -d "\r\n" | wc -c) # don't count \r\n + case "$COUNT" in + 1) + if [ $(cat "$1" | sed '/^Change-Id: /d;/^\s*$/d' | wc -l) -gt 1 ]; then + THIS_LINE_LEN=$FIRSTLINE_LEN + MSG="The first line (subject) should be no more than $FIRSTLINE_LEN characters." + else + THIS_LINE_LEN=$LINE_LEN + MSG="The first line (subject) should be no more than $LINE_LEN characters if it is the only line." + fi + if [ $LEN -gt $THIS_LINE_LEN ]; then + OutputError "$MSG" 1 + elif [ "${LINE:$LEN-1}" = "." ]; then + OutputError "The first line (subject) should not end with a period." 2 + fi + 2) + if [ $LEN -gt 0 ]; then + OutputError "The second line should be empty." 3 + fi + *) + if [ $LEN -gt $LINE_LEN ]; then + OutputError "Subsequent lines (body) should be no more than $LINE_LEN characters." 4 + fi + esac + done - name: Prepare for build shell: cmd