|
| 1 | +name: Trailing Whitespace Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +jobs: |
| 8 | + check-trailing-whitespace: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + permissions: |
| 11 | + contents: read |
| 12 | + |
| 13 | + steps: |
| 14 | + - name: Checkout code |
| 15 | + uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + fetch-depth: 0 |
| 18 | + |
| 19 | + - name: Check for trailing whitespace |
| 20 | + run: | |
| 21 | + echo "Checking for trailing whitespace in changed files..." |
| 22 | +
|
| 23 | + # Get the base branch |
| 24 | + BASE_SHA="${{ github.event.pull_request.base.sha }}" |
| 25 | + HEAD_SHA="${{ github.event.pull_request.head.sha }}" |
| 26 | +
|
| 27 | + # Get list of changed files (excluding deleted files) |
| 28 | + CHANGED_FILES=$(git diff --name-only --diff-filter=d "$BASE_SHA" "$HEAD_SHA") |
| 29 | +
|
| 30 | + if [ -z "$CHANGED_FILES" ]; then |
| 31 | + echo "No files to check." |
| 32 | + exit 0 |
| 33 | + fi |
| 34 | +
|
| 35 | + # File patterns to check (text files) |
| 36 | + PATTERNS="\.cs$|\.csproj$|\.sln$|\.ts$|\.html$|\.css$|\.scss$" |
| 37 | +
|
| 38 | + # Directories and file patterns to exclude |
| 39 | + EXCLUDE_PATTERNS="(^|\/)(\.|node_modules|bin|obj|artifacts|packages|\.vs|\.nuke\/temp)($|\/)" |
| 40 | +
|
| 41 | + ERRORS_FOUND=0 |
| 42 | + TEMP_FILE=$(mktemp) |
| 43 | +
|
| 44 | + while IFS= read -r file; do |
| 45 | + # Skip if file doesn't exist (shouldn't happen with --diff-filter=d, but just in case) |
| 46 | + if [ ! -f "$file" ]; then |
| 47 | + continue |
| 48 | + fi |
| 49 | +
|
| 50 | + # Check if file matches patterns to check |
| 51 | + if ! echo "$file" | grep -qE "$PATTERNS"; then |
| 52 | + continue |
| 53 | + fi |
| 54 | +
|
| 55 | + # Check if file should be excluded |
| 56 | + if echo "$file" | grep -qE "$EXCLUDE_PATTERNS"; then |
| 57 | + continue |
| 58 | + fi |
| 59 | +
|
| 60 | + # Find trailing whitespace lines, excluding XML doc placeholder lines that are exactly "/// " (one space) |
| 61 | + MATCHES=$(grep -n '[[:space:]]$' "$file" | grep -vE '^[0-9]+:[[:space:]]*/// $' || true) |
| 62 | +
|
| 63 | + if [ -n "$MATCHES" ]; then |
| 64 | + echo "❌ Trailing whitespace found in: $file" |
| 65 | + echo "$MATCHES" | head -10 |
| 66 | + TOTAL=$(echo "$MATCHES" | wc -l) |
| 67 | + if [ "$TOTAL" -gt 10 ]; then |
| 68 | + echo " ... and $(($TOTAL - 10)) more lines" |
| 69 | + fi |
| 70 | + echo "1" >> "$TEMP_FILE" |
| 71 | + fi |
| 72 | + done <<< "$CHANGED_FILES" |
| 73 | +
|
| 74 | + ERRORS_FOUND=$(wc -l < "$TEMP_FILE" 2>/dev/null || echo "0") |
| 75 | + rm -f "$TEMP_FILE" |
| 76 | +
|
| 77 | + if [ "$ERRORS_FOUND" -gt 0 ]; then |
| 78 | + echo "" |
| 79 | + echo "❌ Found trailing whitespace in $ERRORS_FOUND file(s)." |
| 80 | + echo "Please remove trailing whitespace from the files listed above." |
| 81 | + exit 1 |
| 82 | + else |
| 83 | + echo "✅ No trailing whitespace found in changed files." |
| 84 | + exit 0 |
| 85 | + fi |
0 commit comments