From 13f1a3cf0d3b7ce497241a923986cbcb869c3210 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Fri, 8 Jan 2021 15:45:41 +0100 Subject: [PATCH] dist/tools/commit-msg: annotate errors in Github Action --- dist/tools/commit-msg/check.sh | 51 +++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/dist/tools/commit-msg/check.sh b/dist/tools/commit-msg/check.sh index 52caa8de22..ba4cdb1489 100755 --- a/dist/tools/commit-msg/check.sh +++ b/dist/tools/commit-msg/check.sh @@ -6,9 +6,34 @@ # General Public License v2.1. See the file LICENSE in the top level # directory for more details. +. "$(dirname "$0")/../ci/github_annotate.sh" + +github_annotate_setup + MSG_MAX_LENGTH=50 MSG_STRETCH_LENGTH=72 +if tput colors 2> /dev/null > /dev/null && [ $(tput colors) -ge 8 ]; then + CERROR1="\033[1;31m" + CWARN1="\033[1;33m" + CERROR2="\033[0;31m" + CWARN2="\033[0;33m" + CRESET="\033[0m" +else + CERROR1= + CWARN1= + CERROR2= + CWARN2= + CRESET= +fi +if [ -n "${BASH_VERSION}" ]; then + # workaround when included in bash to escape newlines and carriage returns + # properly in _escape + ECHO_ESC='echo -e' +else + ECHO='echo' +fi + # If no branch but an option is given, unset BRANCH. # Otherwise, consume this parameter. BRANCH="${1}" @@ -35,18 +60,36 @@ ERROR="$(git log \ msg_length=$(echo "${msg}" | awk '{print length($0)}') if [ ${msg_length} -gt ${MSG_MAX_LENGTH} ]; then + ERROR=0 if [ ${msg_length} -gt ${MSG_STRETCH_LENGTH} ]; then - MSG="Error: Commit message is longer than ${MSG_STRETCH_LENGTH} characters:" + MSG="Commit message is longer than ${MSG_STRETCH_LENGTH} characters:" + ERROR=1 echo "error" else - MSG="Warning: Commit message is longer than ${MSG_MAX_LENGTH}" + MSG="Commit message is longer than ${MSG_MAX_LENGTH}" MSG="${MSG} (but < ${MSG_STRETCH_LENGTH}) characters:" fi - echo "${MSG}" >&2 - echo " \"${msg}\"" >&2 + if github_annotate_is_on; then + if [ ${ERROR} -eq 1 ]; then + github_annotate_error_no_file "${MSG} \"${msg}\"" + else + github_annotate_warning_no_file "${MSG} \"${msg}\"" + fi + else + if [ ${ERROR} -eq 1 ]; then + ${ECHO_ESC} "${CERROR1}Error:${CERROR2} ${MSG}${CRESET}" >&2 + else + ${ECHO_ESC} "${CWARN1}Warning:${CWARN2} ${MSG}${CRESET}" >&2 + fi + echo " \"${msg}\"" >&2 + fi fi done)" +github_annotate_teardown + +github_annotate_report_last_run + if [ -n "${ERROR}" ]; then exit 1 fi