From 3b49c417c9102cdbfaf3920e5eaacb0cf109855f Mon Sep 17 00:00:00 2001 From: Dan Dye Date: Wed, 25 Jun 2025 22:15:22 -0400 Subject: [PATCH 1/5] Add GitHub Actions workflow to check for trailing spaces - Add check-trailing-spaces.yml workflow that runs on pull requests - Only checks lines that were added/modified in the PR using git diff -U0 - Checks Python, Markdown, JSON, YAML, and other text files - Provides clear error messages and fix instructions for changed lines - Update CONTRIBUTING guide with code quality requirements Fixes #104 --- .github/workflows/check-trailing-spaces.yml | 99 +++++++++++++++++++++ CONTRIBUTING | 15 +++- 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/check-trailing-spaces.yml diff --git a/.github/workflows/check-trailing-spaces.yml b/.github/workflows/check-trailing-spaces.yml new file mode 100644 index 00000000..ca9de790 --- /dev/null +++ b/.github/workflows/check-trailing-spaces.yml @@ -0,0 +1,99 @@ +name: Check Trailing Spaces + +on: + pull_request: + types: [opened, synchronize, reopened] + +jobs: + check-trailing-spaces: + runs-on: ubuntu-latest + name: Check for trailing spaces + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Fetch full history for git diff + + - name: Check for trailing spaces in changed lines + run: | + echo "Checking for trailing spaces in changed lines..." + + # Get the base branch (usually main or master) + BASE_BRANCH="${{ github.event.pull_request.base.ref }}" + echo "Base branch: $BASE_BRANCH" + + # Get the list of changed files with specific extensions + echo "Getting changed files..." + CHANGED_FILES=$(git diff -U0 --name-only origin/$BASE_BRANCH...HEAD | grep -E '\.(py|md|json|yaml|yml|txt|sh|js|ts|jsx|tsx|css|html|xml)$' || true) + + if [ -z "$CHANGED_FILES" ]; then + echo "✅ No relevant files changed in this PR" + exit 0 + fi + + echo "Changed files to check:" + echo "$CHANGED_FILES" + echo "" + + # Initialize variables + files_with_trailing_spaces="" + has_trailing_spaces=false + + # Check each changed file + for file in $CHANGED_FILES; do + if [ ! -f "$file" ]; then + echo "Skipping deleted file: $file" + continue + fi + + echo "Checking changed lines in: $file" + + # Get the diff for added/modified lines only (lines starting with +) + # and check if any of them have trailing spaces + LINES_WITH_TRAILING_SPACES=$(git diff -U0 origin/$BASE_BRANCH...HEAD "$file" | \ + grep '^+' | \ + grep -v '^+++' | \ + grep '[[:space:]]$' || true) + + if [ -n "$LINES_WITH_TRAILING_SPACES" ]; then + echo "❌ Trailing spaces found in changed lines of: $file" + files_with_trailing_spaces="${files_with_trailing_spaces}$file\n" + has_trailing_spaces=true + + # Show the problematic lines + echo " Changed lines with trailing spaces:" + echo "$LINES_WITH_TRAILING_SPACES" | head -5 + echo "" + + # Show specific line numbers in the file + echo " Line numbers in $file:" + git diff -U0 origin/$BASE_BRANCH...HEAD "$file" | \ + grep -n '^+.*[[:space:]]$' | \ + grep -v '^[0-9]*:+++ ' | \ + head -5 + echo "" + else + echo "✅ No trailing spaces in changed lines of: $file" + fi + done + + # Check if any trailing spaces were found + if [ "$has_trailing_spaces" = true ]; then + echo "::error::Trailing spaces detected in changed lines of the following files:" + echo -e "$files_with_trailing_spaces" + echo "" + echo "Please remove trailing spaces from the lines you modified." + echo "You can use the following command to remove trailing spaces:" + echo " sed -i 's/[[:space:]]*$//' " + echo "" + echo "Or to remove trailing spaces from specific files:" + for file in $CHANGED_FILES; do + if [ -f "$file" ]; then + echo " sed -i 's/[[:space:]]*$//' $file" + fi + done + exit 1 + else + echo "✅ No trailing spaces found in changed lines!" + fi diff --git a/CONTRIBUTING b/CONTRIBUTING index bc23aaed..90bc4a69 100644 --- a/CONTRIBUTING +++ b/CONTRIBUTING @@ -30,4 +30,17 @@ This project follows All submissions, including submissions by project members, require review. We use GitHub pull requests for this purpose. Consult [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more -information on using pull requests. \ No newline at end of file +information on using pull requests. + +### Code quality checks + +All pull requests must pass automated code quality checks, including: + +- **Trailing spaces check**: Our CI pipeline will automatically check for trailing spaces in code files. If trailing spaces are detected, the check will fail and provide guidance on how to fix them. To remove trailing spaces from your files before submitting: + ```bash + # Remove trailing spaces from a specific file + sed -i 's/[[:space:]]*$//' filename + + # Remove trailing spaces from all relevant files + find . -type f \( -name '*.py' -o -name '*.md' -o -name '*.json' -o -name '*.yaml' -o -name '*.yml' \) -exec sed -i 's/[[:space:]]*$//' {} + + ``` \ No newline at end of file From f0130df73e5f13fc8feeb44529045472f665a95a Mon Sep 17 00:00:00 2001 From: Dan Dye Date: Wed, 25 Jun 2025 22:30:32 -0400 Subject: [PATCH 2/5] Update CONTRIBUTING to clarify CI only checks changed lines - Clarify that trailing spaces check only applies to changed lines using git diff -U0 - Remove suggestion to fix entire files to avoid unrelated changes - Add command to see which changed lines have trailing spaces - Add note explaining the focused approach --- CONTRIBUTING | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING b/CONTRIBUTING index 90bc4a69..72dc8ffa 100644 --- a/CONTRIBUTING +++ b/CONTRIBUTING @@ -36,11 +36,13 @@ information on using pull requests. All pull requests must pass automated code quality checks, including: -- **Trailing spaces check**: Our CI pipeline will automatically check for trailing spaces in code files. If trailing spaces are detected, the check will fail and provide guidance on how to fix them. To remove trailing spaces from your files before submitting: +- **Trailing spaces check**: Our CI pipeline will automatically check for trailing spaces in **changed lines only** (using `git diff -U0`). If trailing spaces are detected in lines you modified, the check will fail and provide guidance on how to fix them. To remove trailing spaces from your changes before submitting: ```bash # Remove trailing spaces from a specific file sed -i 's/[[:space:]]*$//' filename - # Remove trailing spaces from all relevant files - find . -type f \( -name '*.py' -o -name '*.md' -o -name '*.json' -o -name '*.yaml' -o -name '*.yml' \) -exec sed -i 's/[[:space:]]*$//' {} + - ``` \ No newline at end of file + # To see which lines have trailing spaces in your changes: + git diff -U0 | grep '^+' | grep -v '^+++' | grep '[[:space:]]$' + ``` + + **Note**: Our CI only checks the lines you've modified, not the entire file. This keeps your pull request focused on your changes and avoids introducing unrelated formatting changes. \ No newline at end of file From 85e32a08d62dce95eb7fa5f7dfa102f3e8826397 Mon Sep 17 00:00:00 2001 From: Dan Dye Date: Sun, 30 Aug 2026 02:48:24 +0000 Subject: [PATCH 3/5] chore(ci): remove emojis from check-trailing-spaces workflow --- .github/workflows/check-trailing-spaces.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/check-trailing-spaces.yml b/.github/workflows/check-trailing-spaces.yml index ca9de790..8df658f7 100644 --- a/.github/workflows/check-trailing-spaces.yml +++ b/.github/workflows/check-trailing-spaces.yml @@ -28,7 +28,7 @@ jobs: CHANGED_FILES=$(git diff -U0 --name-only origin/$BASE_BRANCH...HEAD | grep -E '\.(py|md|json|yaml|yml|txt|sh|js|ts|jsx|tsx|css|html|xml)$' || true) if [ -z "$CHANGED_FILES" ]; then - echo "✅ No relevant files changed in this PR" + echo "No relevant files changed in this PR" exit 0 fi @@ -57,7 +57,7 @@ jobs: grep '[[:space:]]$' || true) if [ -n "$LINES_WITH_TRAILING_SPACES" ]; then - echo "❌ Trailing spaces found in changed lines of: $file" + echo "Trailing spaces found in changed lines of: $file" files_with_trailing_spaces="${files_with_trailing_spaces}$file\n" has_trailing_spaces=true @@ -74,7 +74,7 @@ jobs: head -5 echo "" else - echo "✅ No trailing spaces in changed lines of: $file" + echo "No trailing spaces in changed lines of: $file" fi done @@ -95,5 +95,5 @@ jobs: done exit 1 else - echo "✅ No trailing spaces found in changed lines!" + echo "No trailing spaces found in changed lines!" fi From d1d18a6c24bad105db30d08b1ace92ca6d8e081d Mon Sep 17 00:00:00 2001 From: Dan Dye Date: Sun, 30 Aug 2026 02:50:47 +0000 Subject: [PATCH 4/5] fix(ci): add explicit permissions and pass BASE_BRANCH via env for zizmor --- .github/workflows/check-trailing-spaces.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/check-trailing-spaces.yml b/.github/workflows/check-trailing-spaces.yml index 8df658f7..b09bb442 100644 --- a/.github/workflows/check-trailing-spaces.yml +++ b/.github/workflows/check-trailing-spaces.yml @@ -4,6 +4,9 @@ on: pull_request: types: [opened, synchronize, reopened] +permissions: + contents: read + jobs: check-trailing-spaces: runs-on: ubuntu-latest @@ -16,11 +19,10 @@ jobs: fetch-depth: 0 # Fetch full history for git diff - name: Check for trailing spaces in changed lines + env: + BASE_BRANCH: ${{ github.event.pull_request.base.ref }} run: | echo "Checking for trailing spaces in changed lines..." - - # Get the base branch (usually main or master) - BASE_BRANCH="${{ github.event.pull_request.base.ref }}" echo "Base branch: $BASE_BRANCH" # Get the list of changed files with specific extensions From 0351169c89abc5eca675bf021198c04a017236b5 Mon Sep 17 00:00:00 2001 From: Dan Dye Date: Sun, 30 Aug 2026 02:52:01 +0000 Subject: [PATCH 5/5] fix(ci): pin checkout action commit SHA and set persist-credentials false --- .github/workflows/check-trailing-spaces.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/check-trailing-spaces.yml b/.github/workflows/check-trailing-spaces.yml index b09bb442..33152e39 100644 --- a/.github/workflows/check-trailing-spaces.yml +++ b/.github/workflows/check-trailing-spaces.yml @@ -14,9 +14,10 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v4 + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: fetch-depth: 0 # Fetch full history for git diff + persist-credentials: false - name: Check for trailing spaces in changed lines env: