-
Notifications
You must be signed in to change notification settings - Fork 671
80 lines (74 loc) · 2.74 KB
/
prettier.yml
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
name: Prettier
on:
pull_request:
paths:
- 'src/**/*.js'
- '.github/workflows/prettier.yml'
types:
- opened
- synchronize
- reopened
- ready_for_review
workflow_dispatch:
permissions:
contents: write
pull-requests: write
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 'latest'
- name: Install Node dependencies
run: npm ci
- name: Run Prettier on changed files
env:
GH_TOKEN: ${{ github.token }}
run: |
git fetch origin main
# Check for changed JavaScript files.
CHANGED_FILES=$(git diff --name-only origin/main...HEAD | grep -E '\.js$|\.jsx$|\.ts$|\.tsx$' || echo "")
# If JavaScript files have changed, run Prettier.
if [ -n "$CHANGED_FILES" ]; then
echo "Fixing the following files:"
echo "$CHANGED_FILES"
# Run Prettier on the changed files.
echo "$CHANGED_FILES" | xargs npx prettier --write
# Join the filenames into a single line with space separation.
JOINED_FILES=$(echo $CHANGED_FILES | tr '\n' ' ')
echo "CHANGED_FILES=$JOINED_FILES" >> $GITHUB_ENV
else
echo "No files to format"
fi
- name: Commit and push Prettier changes
env:
GH_TOKEN: ${{ github.token }}
run: |
# Did we make changes when we ran Prettier?
if [ -n "$(git status --porcelain)" ]; then
git config user.name "Pantheon Bot"
git config user.email "[email protected]"
git add .
git commit -m "Apply Prettier formatting"
git push origin HEAD:${{ github.head_ref }}
DIFF_OUTPUT=$(git diff HEAD~1 HEAD)
echo "DIFF_OUTPUT<<EOF" >> $GITHUB_ENV
echo "$DIFF_OUTPUT" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
else
echo "No changes to commit"
fi
- name: Set Prettier diff output
env:
GH_TOKEN: ${{ github.token }}
run: |
if [ -n "${{ env.DIFF_OUTPUT }}" ]; then
CURRENT_COMMIT=$(git rev-parse --short HEAD)
gh pr comment ${{ github.event.pull_request.number }} --body "$(echo -e "Hi from your friendly robot! 🤖\n\nI've applied Prettier formatting to the following files in $CURRENT_COMMIT:\n\n\`${{ env.CHANGED_FILES }}\`\n\nThe full diff is below. Please review the changes.\n\n<details>\n<summary>Click to expand</summary>\n\n\`\`\`diff\n$DIFF_OUTPUT\n\`\`\`\n</details>")"
else
echo "No Prettier changes"
fi