-
-
Notifications
You must be signed in to change notification settings - Fork 227
106 lines (91 loc) · 3.62 KB
/
Copy pathcheck-whitespace.yml
File metadata and controls
106 lines (91 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
name: Whitespace Check
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
check-whitespace:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- name: Check for whitespace errors
id: check
run: |
# Compare PR branch against base branch
BASE_SHA=${{ github.event.pull_request.base.sha }}
HEAD_SHA=${{ github.event.pull_request.head.sha }}
echo "Checking commits from $BASE_SHA to $HEAD_SHA"
# git diff --check returns non-zero if whitespace errors exist
if git diff --check "$BASE_SHA" "$HEAD_SHA" > errors.txt 2>&1; then
echo "has_errors=false" >> $GITHUB_OUTPUT
echo "### No Whitespace Issues" >> $GITHUB_STEP_SUMMARY
else
echo "has_errors=true" >> $GITHUB_OUTPUT
# Build summary
{
echo "### Whitespace Issues Found"
echo ""
echo "Files with whitespace errors (trailing spaces, spaces before tabs):"
echo ""
echo '```'
cat errors.txt
echo '```'
echo ""
echo "**To fix:** Run \`git rebase --whitespace=fix $BASE_SHA\` then force push."
} >> $GITHUB_STEP_SUMMARY
# Save for PR comment (using heredoc for multiline)
{
echo 'ERRORS<<EOF'
cat errors.txt
echo 'EOF'
} >> $GITHUB_ENV
fi
# Always look for existing comment (to update or delete)
- name: Find existing comment
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3.1.0
id: find-comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: 'whitespace errors'
# Delete old failure comment when check passes
- name: Delete old comment if check passes
if: steps.check.outputs.has_errors == 'false' && steps.find-comment.outputs.comment-id != ''
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ steps.find-comment.outputs.comment-id }}
});
# Create or update failure comment
- name: Create or update PR comment
if: steps.check.outputs.has_errors == 'true'
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
edit-mode: replace
body: |
## Whitespace Check Failed
Files in this PR have whitespace errors (trailing spaces, spaces before tabs, etc.):
```
${{ env.ERRORS }}
```
### How to fix
```bash
# Automatically fix whitespace in your branch
git rebase --whitespace=fix ${{ github.event.pull_request.base.sha }}
git push --force-with-lease
```
---
*Checked at: ${{ github.event.pull_request.head.sha }}*
- name: Fail if errors
if: steps.check.outputs.has_errors == 'true'
run: exit 1