-
Notifications
You must be signed in to change notification settings - Fork 283
70 lines (61 loc) · 2.14 KB
/
prettier-label-action.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
name: Prettier Formatting on Label
# Explicitly set permissions
permissions:
contents: write
pull-requests: write
on:
pull_request:
types: [labeled]
jobs:
prettier-format:
# Only run if the 'prettier' label is added
if: ${{ contains(github.event.pull_request.labels.*.name, 'prettier') }}
runs-on: ubuntu-latest
steps:
# Checkout the PR branch
- uses: actions/checkout@v4
with:
# Use the full PR head reference
ref: ${{ github.event.pull_request.head.ref }}
# Use the full repository name
repository: ${{ github.event.pull_request.head.repo.full_name }}
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
# Setup Node.js (required for Prettier)
- uses: actions/setup-node@v4
with:
node-version: "lts/*"
# Install Prettier
- name: Install Prettier
run: npm install --save-dev --save-exact prettier
# Run Prettier using the repo's config
- name: Run Prettier
run: npx prettier --write .
# Check if there are any changes
- name: Check for changes
id: check-changes
run: |
git diff --quiet || echo "changes=true" >> $GITHUB_OUTPUT
# Commit and push changes if any
- name: Commit changes
if: steps.check-changes.outputs.changes == 'true'
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add .
git commit -m "[chore] automatic prettier formatting"
git push origin HEAD:${{ github.event.pull_request.head.ref }}
# Remove the label after formatting
# todo: label removal is flaky, disabled for now
# - name: Remove formatting label
# if: always()
# uses: actions/github-script@v7
# with:
# github-token: ${{ secrets.GITHUB_TOKEN }}
# script: |
# github.rest.issues.removeLabel({
# owner: context.repo.owner,
# repo: context.repo.repo,
# issue_number: context.issue.number,
# name: 'prettier'
# })