From 87df86005bfb7032496bb58fa0b21f095dd1f4bb Mon Sep 17 00:00:00 2001 From: Ranabir Chakraborty Date: Thu, 22 Aug 2024 10:41:28 +0530 Subject: [PATCH] WFCORE-6955 Add a CI job to check for non-i18n INFO/WARN/ERROR logging --- .github/workflows/check_logging.yml | 22 +++++++++++ check_logging.sh | 58 +++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 .github/workflows/check_logging.yml create mode 100644 check_logging.sh diff --git a/.github/workflows/check_logging.yml b/.github/workflows/check_logging.yml new file mode 100644 index 00000000000..779d0c19c60 --- /dev/null +++ b/.github/workflows/check_logging.yml @@ -0,0 +1,22 @@ +name: Check non-i18n logging + +on: + pull_request: + branches: + - main + - '*.x' + +jobs: + check-logging: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Run logging check + env: + GITHUB_BASE_REF: '${{ github.base_ref }}' + shell: bash + run: bash check_logging.sh diff --git a/check_logging.sh b/check_logging.sh new file mode 100644 index 00000000000..61bf9a5ed56 --- /dev/null +++ b/check_logging.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +BASE_BRANCH="${GITHUB_BASE_REF:-main}" +DIFF=$(git diff origin/"$BASE_BRANCH"...HEAD || true) + +if [ -z "$DIFF" ]; then + echo "No diff found to analyze." + exit 0 +fi + +PATTERNS=( + ".info(" + ".infof(" + ".warn(" + ".warnf(" + ".error(" + ".errorf(" + ".fatal(" + ".fatalf(" + "System.out.print" + "System.err.print" + ".printStackTrace" +) + +ERRORS="" +CURRENT_FILE="" + +while IFS= read -r line; do + # Update the current file if a new diff starts + if [[ "$line" =~ ^diff\ --git\ a\/.*\ b\/.* ]]; then + CURRENT_FILE=$(echo "$line" | awk '{print $3}' | sed 's/^a\///') + fi + + if [[ "$line" =~ ^\+[^+] ]]; then + # Ignore lines in the test directories + if [[ "$CURRENT_FILE" != *"src/test/"* && "$CURRENT_FILE" != *"testsuite/"* && "$CURRENT_FILE" != *"check_logging.sh"* ]]; then + # Check for any of the patterns, ensuring "//" doesn't precede them + for pattern in "${PATTERNS[@]}"; do + if [[ "$line" == *"$pattern"* ]]; then + # Ensure the pattern is not commented out with "//" + if [[ "$line" != *"//"*"$pattern"* ]]; then + # Capture the error line and its context + ERRORS+="$line\nFile: $CURRENT_FILE\n\n" + fi + fi + done + fi + fi +done <<< "$DIFF" + +if [ -n "$ERRORS" ]; then + echo -e "Logging statements found that should be internationalized or converted to a lower log level:\n" + echo -e "$ERRORS" + exit 1 +else + echo "No problematic logging statements found." + exit 0 +fi