-
Notifications
You must be signed in to change notification settings - Fork 1
Add GitHub Action to check trailing whitespace on PRs #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: softworkz <4985349+softworkz@users.noreply.github.com>
Co-authored-by: softworkz <4985349+softworkz@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a few changes in files which create trailing whitespace
Co-authored-by: softworkz <4985349+softworkz@users.noreply.github.com>
Added two demo files with trailing whitespace in commit 6f4e1f0:
These files will trigger the workflow check on this PR, demonstrating how it detects trailing whitespace. |
|
@copilot - Great! Now remove the extra files so that we can merge |
Co-authored-by: softworkz <4985349+softworkz@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds automated trailing whitespace detection for pull requests to maintain code consistency. The workflow checks only changed files against trailing whitespace issues and reports specific line numbers where issues are found.
Key Changes:
- Implements a GitHub Actions workflow that triggers on PR events (open, sync, reopen)
- Checks text files (
.cs,.csproj,.sln,.ts,.html,.css,.scss) for trailing whitespace - Excludes build artifacts and dependencies from checks
- Provides detailed output with line numbers and fails the check if issues are found
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if [ $(grep -c '[[:space:]]$' "$file") -gt 10 ]; then | ||
| echo " ... and $(($(grep -c '[[:space:]]$' "$file") - 10)) more lines" |
Copilot
AI
Nov 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The head -10 command limits the display to 10 lines but doesn't affect the count in line 64. This means if there are exactly 10 lines with trailing whitespace, line 64 will calculate (10 - 10) = 0 and display "... and 0 more lines", which is confusing.
Consider changing the condition to check if the count is greater than 10:
if [ $(grep -c '[[:space:]]$' "$file") -gt 10 ]; then
EXTRA=$(($(grep -c '[[:space:]]$' "$file") - 10))
echo " ... and $EXTRA more lines"
fiOr simplify by checking if EXTRA is greater than 0.
| if [ $(grep -c '[[:space:]]$' "$file") -gt 10 ]; then | |
| echo " ... and $(($(grep -c '[[:space:]]$' "$file") - 10)) more lines" | |
| EXTRA=$(($(grep -c '[[:space:]]$' "$file") - 10)) | |
| if [ "$EXTRA" -gt 0 ]; then | |
| echo " ... and $EXTRA more lines" |
| echo "❌ Trailing whitespace found in: $file" | ||
| grep -n '[[:space:]]$' "$file" | head -10 | ||
| if [ $(grep -c '[[:space:]]$' "$file") -gt 10 ]; then | ||
| echo " ... and $(($(grep -c '[[:space:]]$' "$file") - 10)) more lines" |
Copilot
AI
Nov 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The grep command on line 64 is executed multiple times within the same if block, which is inefficient. Consider storing the count in a variable first:
TRAILING_COUNT=$(grep -c '[[:space:]]$' "$file")
echo "❌ Trailing whitespace found in: $file"
grep -n '[[:space:]]$' "$file" | head -10
if [ "$TRAILING_COUNT" -gt 10 ]; then
echo " ... and $(($TRAILING_COUNT - 10)) more lines"
fiThis avoids running grep multiple times on the same file.
| echo "❌ Trailing whitespace found in: $file" | |
| grep -n '[[:space:]]$' "$file" | head -10 | |
| if [ $(grep -c '[[:space:]]$' "$file") -gt 10 ]; then | |
| echo " ... and $(($(grep -c '[[:space:]]$' "$file") - 10)) more lines" | |
| TRAILING_COUNT=$(grep -c '[[:space:]]$' "$file") | |
| echo "❌ Trailing whitespace found in: $file" | |
| grep -n '[[:space:]]$' "$file" | head -10 | |
| if [ "$TRAILING_COUNT" -gt 10 ]; then | |
| echo " ... and $(($TRAILING_COUNT - 10)) more lines" |
Adds automated trailing whitespace detection for pull requests to maintain code consistency.
Implementation
.github/workflows/trailing-whitespace-check.yml.cs,.csproj,.sln,.ts,.html,.css,.scssbin/,obj/,artifacts/), dependencies (node_modules/,packages/), hidden dirs, IDE filesSecurity
contents: readExample Output
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.