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..72dc8ffa 100644 --- a/CONTRIBUTING +++ b/CONTRIBUTING @@ -30,4 +30,19 @@ 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 **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 + + # 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