-
-
Notifications
You must be signed in to change notification settings - Fork 3
262 lines (228 loc) · 8.04 KB
/
prettier-code-format-check.yml
File metadata and controls
262 lines (228 loc) · 8.04 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
name: Prettier Code Format Check
on:
push:
branches: [main, develop]
paths:
- "**/*.js"
- "**/*.ts"
- "**/*.jsx"
- "**/*.tsx"
- "**/*.css"
- "**/*.json"
- "**/*.md"
- ".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"
- ".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)
# "false" => enforce formatting (fail on violations)
ALLOW_WARNINGS: "true"
PRETTIER_REPORT: "prettier-report.txt"
PRETTIER_SUMMARY: "prettier-summary.md"
NPM_DEBUG_LOG: "npm-debug.log"
# Enterprise behavior:
# If Prettier is not installed in devDependencies, install a pinned version just for CI.
# (You can later remove this and commit Prettier to package.json when ready.)
BOOTSTRAP_PRETTIER_IF_MISSING: "true"
PRETTIER_VERSION: "3.3.3"
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
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 -v
pnpm install --frozen-lockfile
elif [[ "$mgr" == "yarn" ]]; then
corepack enable
yarn -v
yarn install --frozen-lockfile
else
if [[ -f package-lock.json ]]; then
npm ci
else
echo "::warning::No lockfile found. Falling back to 'npm install' (non-deterministic)."
npm install
fi
fi
- name: 🧰 Ensure Prettier Available (CI bootstrap)
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 devDependencies. Bootstrapping Prettier v${PRETTIER_VERSION} for CI 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
id: prettier
shell: bash
run: |
set -euo pipefail
report="${PRETTIER_REPORT}"
prettier_present="${{ steps.ensure-prettier.outputs.prettier_present }}"
if [[ "${prettier_present}" != "true" ]]; then
echo "Prettier not available. Skipping formatting check."
printf '%s\n' "Prettier not available in CI run." > "$report"
echo "exit_code=2" >> "$GITHUB_OUTPUT"
exit 0
fi
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 GitHub Job Summary
if: always()
shell: bash
run: |
set -euo pipefail
exit_code="${{ steps.prettier.outputs.exit_code }}"
prettier_present="${{ steps.ensure-prettier.outputs.prettier_present }}"
prettier_source="${{ steps.ensure-prettier.outputs.prettier_source }}"
{
echo "### 💅 Prettier Format Summary"
echo
echo "- **Ref:** \`${GITHUB_REF}\`"
echo "- **Commit:** \`${GITHUB_SHA}\`"
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. This run did not enforce formatting."
echo
echo "**Recommended fix (preferred):** commit Prettier into devDependencies:"
echo
echo '```bash'
echo 'npm i -D prettier'
echo '```'
echo
echo "**CI fallback used:** \`${BOOTSTRAP_PRETTIER_IF_MISSING}\` (version \`${PRETTIER_VERSION}\`)."
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 full report._"
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 (optional enforcement)
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.prettier.outputs.exit_code }}"
if [[ "${prettier_present}" != "true" ]]; then
echo "❌ Prettier is not available. Enforced mode requires it."
exit 1
fi
if [[ "$exit_code" != "0" ]]; then
echo "❌ Failing due to Prettier formatting violations."
exit 1
fi
echo "✅ Prettier passed."