-
-
Notifications
You must be signed in to change notification settings - Fork 3
275 lines (241 loc) · 8.48 KB
/
prettier-code-format-check.yml
File metadata and controls
275 lines (241 loc) · 8.48 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
name: Prettier Code Format Check
on:
push:
branches: [main, develop]
paths:
- "**/*.js"
- "**/*.ts"
- "**/*.jsx"
- "**/*.tsx"
- "**/*.css"
- "**/*.json"
- "**/*.md"
- "**/*.yml"
- "**/*.yaml"
- ".prettierrc"
- ".prettierignore"
- "package.json"
- "package-lock.json"
- "pnpm-lock.yaml"
- "yarn.lock"
pull_request:
branches: [main, develop]
paths:
- "**/*.js"
- "**/*.ts"
- "**/*.jsx"
- "**/*.tsx"
- "**/*.css"
- "**/*.json"
- "**/*.md"
- "**/*.yml"
- "**/*.yaml"
- ".prettierrc"
- ".prettierignore"
- "package.json"
- "package-lock.json"
- "pnpm-lock.yaml"
- "yarn.lock"
workflow_dispatch:
concurrency:
group: prettier-check-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
jobs:
prettier:
name: 💅 Prettier Code Format Validation
runs-on: ubuntu-latest
timeout-minutes: 15
env:
# "true" => report-only (never fail CI)
# "false" => enforce formatting (fail CI on issues)
ALLOW_WARNINGS: "true"
# If Prettier is missing from devDependencies, CI can install a pinned version temporarily.
# Recommended long-term: add prettier to devDependencies.
BOOTSTRAP_PRETTIER_IF_MISSING: "true"
PRETTIER_VERSION: "3.3.3"
PRETTIER_REPORT: "prettier-report.txt"
PRETTIER_SUMMARY: "prettier-summary.md"
NPM_DEBUG_LOG: "npm-debug.log"
steps:
- name: 📦 Checkout Repository
uses: actions/checkout@v4
- name: ⚙️ Setup Node.js (LTS)
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "npm"
- name: 🧾 Detect Package Manager
id: pm
shell: bash
run: |
set -euo pipefail
if [[ -f pnpm-lock.yaml ]]; then
echo "manager=pnpm" >> "$GITHUB_OUTPUT"
elif [[ -f yarn.lock ]]; then
echo "manager=yarn" >> "$GITHUB_OUTPUT"
elif [[ -f package-lock.json ]]; then
echo "manager=npm" >> "$GITHUB_OUTPUT"
else
# No lockfile -> still attempt npm install, but warn in summary.
echo "manager=npm" >> "$GITHUB_OUTPUT"
fi
- name: 📥 Install Dependencies (deterministic when possible)
id: install
shell: bash
run: |
set -euo pipefail
mgr="${{ steps.pm.outputs.manager }}"
if [[ "$mgr" == "pnpm" ]]; then
corepack enable
pnpm --version
pnpm install --frozen-lockfile
elif [[ "$mgr" == "yarn" ]]; then
corepack enable
yarn --version
# Yarn Berry uses --immutable; Yarn classic ignores it but won't break
yarn install --immutable || yarn install
else
if [[ -f package-lock.json ]]; then
npm ci
else
echo "::warning::No lockfile found (package-lock.json). Using 'npm install' (non-deterministic)."
npm install
fi
fi
- name: 🧰 Ensure Prettier Available
id: ensure_prettier
shell: bash
run: |
set -euo pipefail
if [[ -x "node_modules/.bin/prettier" ]]; then
echo "prettier_present=true" >> "$GITHUB_OUTPUT"
echo "prettier_source=project" >> "$GITHUB_OUTPUT"
exit 0
fi
if [[ "${BOOTSTRAP_PRETTIER_IF_MISSING}" != "true" ]]; then
echo "prettier_present=false" >> "$GITHUB_OUTPUT"
echo "prettier_source=missing" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "::notice::Prettier not found in node_modules. Bootstrapping Prettier v${PRETTIER_VERSION} for CI-only run."
# CI-only install. This will NOT commit anything (runner workspace only).
npm install --no-save "prettier@${PRETTIER_VERSION}"
if [[ -x "node_modules/.bin/prettier" ]]; then
echo "prettier_present=true" >> "$GITHUB_OUTPUT"
echo "prettier_source=ci-bootstrap" >> "$GITHUB_OUTPUT"
else
echo "prettier_present=false" >> "$GITHUB_OUTPUT"
echo "prettier_source=bootstrap-failed" >> "$GITHUB_OUTPUT"
fi
- name: 🔍 Run Prettier Check
id: run_prettier
shell: bash
run: |
set -euo pipefail
report="${PRETTIER_REPORT}"
: > "$report"
prettier_present="${{ steps.ensure_prettier.outputs.prettier_present }}"
if [[ "${prettier_present}" != "true" ]]; then
echo "Prettier not available in this run." > "$report"
echo "exit_code=2" >> "$GITHUB_OUTPUT"
exit 0
fi
# Run local prettier directly (avoid npx ambiguity)
set +e
node_modules/.bin/prettier --check . > "$report" 2>&1
ec="$?"
set -e
echo "exit_code=$ec" >> "$GITHUB_OUTPUT"
echo "Prettier exit code: $ec"
- name: 🧾 Capture npm debug log (if any)
if: always()
shell: bash
run: |
set -euo pipefail
if compgen -G "/home/runner/.npm/_logs/*-debug-0.log" > /dev/null; then
latest="$(ls -t /home/runner/.npm/_logs/*-debug-0.log | head -n 1)"
cp "$latest" "${NPM_DEBUG_LOG}" || true
else
echo "No npm debug log found." > "${NPM_DEBUG_LOG}"
fi
- name: 📄 Generate Job Summary
if: always()
shell: bash
run: |
set -euo pipefail
exit_code="${{ steps.run_prettier.outputs.exit_code }}"
prettier_present="${{ steps.ensure_prettier.outputs.prettier_present }}"
prettier_source="${{ steps.ensure_prettier.outputs.prettier_source }}"
mgr="${{ steps.pm.outputs.manager }}"
{
echo "### 💅 Prettier Format Summary"
echo
echo "- **Ref:** \`${GITHUB_REF}\`"
echo "- **Commit:** \`${GITHUB_SHA}\`"
echo "- **Package manager:** \`${mgr}\`"
echo "- **Exit code:** \`${exit_code}\`"
echo "- **ALLOW_WARNINGS:** \`${ALLOW_WARNINGS}\`"
echo "- **Prettier available:** \`${prettier_present}\`"
echo "- **Prettier source:** \`${prettier_source}\`"
echo
if [[ "${prettier_present}" != "true" ]]; then
echo "❗ Prettier was not available, so formatting was not checked."
echo
echo "**Fix (recommended):** add Prettier to devDependencies:"
echo
echo '```bash'
echo 'npm i -D prettier'
echo '```'
elif [[ "$exit_code" != "0" ]]; then
echo "⚠️ Formatting issues detected."
echo
echo "**Top 80 lines:**"
echo
echo '```text'
head -n 80 "${PRETTIER_REPORT}" || true
echo '```'
echo
echo "_Output truncated. Download artifacts for the full report._"
echo
echo "**Fix locally:**"
echo
echo '```bash'
echo 'npx prettier --write .'
echo '```'
else
echo "✅ No formatting issues found."
fi
} | tee "${PRETTIER_SUMMARY}" >> "$GITHUB_STEP_SUMMARY"
- name: 📦 Upload Artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: prettier-format-report
path: |
${{ env.PRETTIER_REPORT }}
${{ env.PRETTIER_SUMMARY }}
${{ env.NPM_DEBUG_LOG }}
retention-days: 30
- name: ✅ Policy Gate
if: always()
shell: bash
run: |
set -euo pipefail
if [[ "${ALLOW_WARNINGS}" == "true" ]]; then
echo "ALLOW_WARNINGS=true -> report-only mode. Passing."
exit 0
fi
prettier_present="${{ steps.ensure_prettier.outputs.prettier_present }}"
exit_code="${{ steps.run_prettier.outputs.exit_code }}"
if [[ "${prettier_present}" != "true" ]]; then
echo "❌ Enforced mode requires Prettier available in node_modules."
echo " Add it: npm i -D prettier"
exit 1
fi
if [[ "$exit_code" != "0" ]]; then
echo "❌ Failing due to Prettier formatting violations."
exit 1
fi
echo "✅ Prettier passed."