Skip to content

Commit cff6b28

Browse files
committed
fix: isolate write permissions and add format check for PRs
- Set workflow-level permissions to contents: read (least-privilege for PR runs) - Split auto-format/commit into a separate job gated to push only, with job-level contents: write - Remove ref: github.head_ref from checkout (broken on push events and fork PRs) - Add prettier --check step to quality-check job so formatting is validated on PRs
1 parent 4545459 commit cff6b28

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

.github/workflows/code-quality.yml

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ on:
1717
branches: [main, develop]
1818

1919
permissions:
20-
contents: write
20+
contents: read
2121

2222
jobs:
2323
quality-check:
@@ -27,9 +27,6 @@ jobs:
2727
steps:
2828
- name: Checkout code
2929
uses: actions/checkout@v6
30-
with:
31-
ref: ${{ github.head_ref }}
32-
token: ${{ secrets.GITHUB_TOKEN }}
3330

3431
- name: Setup Node.js
3532
uses: actions/setup-node@v6
@@ -40,17 +37,36 @@ jobs:
4037
- name: Install dependencies
4138
run: npm ci
4239

43-
# Auto-fix formatting on push to main/develop (not on PRs)
44-
# Exclude .github/ to avoid workflow permission issues
45-
- name: Format code
46-
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop')
47-
run: npx prettier --write . --ignore-path .prettierignore '!.github/**'
40+
- name: Check formatting
41+
run: npx prettier --check . --ignore-path .prettierignore '!.github/**'
4842

4943
- name: Run ESLint
5044
run: npm run lint
5145

46+
auto-format:
47+
name: Auto Format
48+
if: github.event_name == 'push'
49+
runs-on: ubuntu-latest
50+
timeout-minutes: 10
51+
permissions:
52+
contents: write
53+
steps:
54+
- name: Checkout code
55+
uses: actions/checkout@v6
56+
57+
- name: Setup Node.js
58+
uses: actions/setup-node@v6
59+
with:
60+
node-version: '20'
61+
cache: 'npm'
62+
63+
- name: Install dependencies
64+
run: npm ci
65+
66+
- name: Format code
67+
run: npx prettier --write . --ignore-path .prettierignore '!.github/**'
68+
5269
- name: Commit formatting changes
53-
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop')
5470
uses: stefanzweifel/git-auto-commit-action@v7
5571
with:
5672
commit_message: 'style: fix code formatting [skip ci]'

0 commit comments

Comments
 (0)