-
Notifications
You must be signed in to change notification settings - Fork 4
256 lines (213 loc) · 7.99 KB
/
ci.yaml
File metadata and controls
256 lines (213 loc) · 7.99 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
name: CI
permissions:
contents: read
pull-requests: write
checks: write
on:
pull_request:
types: [opened, synchronize, reopened]
push:
branches: [main]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
name: Install pnpm
with:
version: 10.15.1
run_install: false
- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"
- name: Install Dependencies
run: pnpm install --frozen-lockfile
- name: Check TypeScript Types
run: npx turbo check-types
lint-changed-files:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: pnpm/action-setup@v4
name: Install pnpm
with:
version: 10.15.1
run_install: false
- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"
- name: Install Dependencies
run: pnpm install --frozen-lockfile
- name: Setup Reviewdog
uses: reviewdog/action-setup@v1
# Lint only files changed in the PR and annotate results in the PR check.
- name: Lint Changed Files
shell: bash
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BASE_REF: ${{ github.base_ref }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
set -euo pipefail
# Compute a stable merge-base for the PR diff while keeping fetch depth bounded.
BASE_SHA=""
for fetch_depth in 1 10 50 200; do
git fetch origin "$BASE_REF" --depth="$fetch_depth"
BASE_SHA="$(git merge-base "origin/$BASE_REF" "$PR_HEAD_SHA" || true)"
if [ -n "$BASE_SHA" ]; then
break
fi
done
if [ -z "$BASE_SHA" ]; then
echo "Unable to determine merge-base within depth limit (max depth: 200) for base ref '$BASE_REF'."
echo "Increase the depth sequence in CI for unusually long-lived branches."
exit 1
fi
HEAD_SHA="$PR_HEAD_SHA"
mapfile -t changed_files < <(git diff --name-only --diff-filter=ACMR "$BASE_SHA" "$HEAD_SHA")
if [ ${#changed_files[@]} -eq 0 ]; then
echo "No files changed in this pull request."
exit 0
fi
# Keep only lintable files and group them by workspace so each file is linted
# from the directory that owns its ESLint flat config.
declare -A workspace_to_files
lintable_count=0
for file in "${changed_files[@]}"; do
case "$file" in
*.js|*.jsx|*.ts|*.tsx|*.mjs|*.cjs) ;;
*) continue ;;
esac
if [ ! -f "$file" ]; then
continue
fi
IFS='/' read -r top_level workspace_name _ <<< "$file"
if [ -z "${top_level:-}" ] || [ -z "${workspace_name:-}" ]; then
continue
fi
workspace="${top_level}/${workspace_name}"
if [ ! -f "${workspace}/eslint.config.mjs" ]; then
continue
fi
relative_file="${file#${workspace}/}"
if [ "$relative_file" = "$file" ]; then
continue
fi
workspace_to_files["$workspace"]+="${relative_file}"$'\n'
lintable_count=$((lintable_count + 1))
done
if [ "$lintable_count" -eq 0 ]; then
echo "No changed lintable files in workspaces with ESLint configs."
exit 0
fi
# Lint every touched workspace and fail once at the end so all findings are reported.
lint_failed=0
for workspace in "${!workspace_to_files[@]}"; do
echo "Linting changed files in ${workspace}"
mapfile -t files < <(printf '%s' "${workspace_to_files[$workspace]}" | sed '/^$/d')
eslint_output="$(mktemp)"
eslint_exit_code=0
# Capture ESLint output and let reviewdog decide pass/fail after diff filtering.
# This avoids failing a PR for pre-existing issues elsewhere in touched files.
pnpm --dir "$workspace" exec eslint --no-color --format stylish "${files[@]}" >"$eslint_output" 2>&1 || eslint_exit_code=$?
if [ "$eslint_exit_code" -ge 2 ]; then
cat "$eslint_output"
rm -f "$eslint_output"
lint_failed=1
continue
fi
if ! reviewdog \
-f=eslint \
-name="eslint (${workspace})" \
-reporter=github-pr-check \
-filter-mode=added \
-level=warning \
-fail-level=warning < "$eslint_output"; then
lint_failed=1
fi
rm -f "$eslint_output"
done
exit $lint_failed
# Guardrail: fail PRs when files touched by the PR are not Prettier-formatted.
format-changed-files:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: pnpm/action-setup@v4
name: Install pnpm
with:
version: 10.15.1
run_install: false
- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"
- name: Install Dependencies
run: pnpm install --frozen-lockfile
- name: Check formatting on changed files
shell: bash
env:
BASE_REF: ${{ github.base_ref }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
set -euo pipefail
# Compute a stable merge-base for the PR diff while keeping fetch depth bounded.
BASE_SHA=""
for fetch_depth in 1 10 50 200; do
git fetch origin "$BASE_REF" --depth="$fetch_depth"
BASE_SHA="$(git merge-base "origin/$BASE_REF" "$PR_HEAD_SHA" || true)"
if [ -n "$BASE_SHA" ]; then
break
fi
done
if [ -z "$BASE_SHA" ]; then
echo "Unable to determine merge-base within depth limit (max depth: 200) for base ref '$BASE_REF'."
echo "Increase the depth sequence in CI for unusually long-lived branches."
exit 1
fi
HEAD_SHA="$PR_HEAD_SHA"
# Scope checks to files introduced or modified by the PR.
mapfile -t changed_files < <(git diff --name-only --diff-filter=ACMR "$BASE_SHA" "$HEAD_SHA")
if [ ${#changed_files[@]} -eq 0 ]; then
echo "No files changed in this pull request."
exit 0
fi
# Skip paths removed in the PR (deleted files cannot be formatted).
mapfile -t existing_changed_files < <(
for file in "${changed_files[@]}"; do
if [ -f "$file" ]; then
printf '%s\n' "$file"
fi
done
)
if [ ${#existing_changed_files[@]} -eq 0 ]; then
echo "No changed files exist in the checkout."
exit 0
fi
format_failed=0
# Chunk file lists to avoid command-line length limits on very large PRs.
chunk_size=100
for ((i=0; i<${#existing_changed_files[@]}; i+=chunk_size)); do
chunk=( "${existing_changed_files[@]:i:chunk_size}" )
# --ignore-unknown lets us pass all changed files safely; Prettier only checks supported types.
if ! pnpm exec prettier --check --ignore-unknown "${chunk[@]}"; then
format_failed=1
fi
done
# Exit non-zero if any chunk had formatting violations.
exit $format_failed