Skip to content

Commit 344120f

Browse files
committed
add ci prettier changed-file check
Issue: CLDSRV-587
1 parent 611c55f commit 344120f

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

.github/workflows/tests.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ on:
88
branches-ignore:
99
- 'q/*/**'
1010

11+
pull_request:
12+
branches-ignore:
13+
- 'q/*/**'
14+
1115
env:
1216
# Secrets
1317
azurebackend_AZURE_STORAGE_ACCESS_KEY: >-
@@ -80,6 +84,8 @@ jobs:
8084
steps:
8185
- name: Checkout
8286
uses: actions/checkout@v4
87+
with:
88+
fetch-depth: 0
8389
- uses: actions/setup-node@v4
8490
with:
8591
node-version: '22'
@@ -89,6 +95,12 @@ jobs:
8995
run: yarn global add typescript@4.9.5
9096
- name: install dependencies
9197
run: yarn install --frozen-lockfile --network-concurrency 1
98+
- name: Prettier (changed files)
99+
run: scripts/format-changed.sh
100+
env:
101+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
102+
BASE_REF: ${{ github.event.pull_request.base.ref }}
103+
if: ${{ github.event_name == 'pull_request' }}
92104
- uses: actions/setup-python@v5
93105
with:
94106
python-version: '3.9'

scripts/format-changed.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Run Prettier only on files changed against a base ref/commit
5+
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
6+
cd "$ROOT_DIR"
7+
8+
# Inputs that can be provided by CI
9+
BASE_SHA="${BASE_SHA:-}"
10+
BASE_REF="${BASE_REF:-${GITHUB_BASE_REF:-}}"
11+
DEFAULT_BASE="${DEFAULT_BASE:-origin/main}"
12+
13+
if [[ -n "$BASE_SHA" ]]; then
14+
DIFF_BASE="$BASE_SHA"
15+
elif [[ -n "$BASE_REF" ]]; then
16+
DIFF_BASE="origin/${BASE_REF#origin/}"
17+
else
18+
DIFF_BASE="$DEFAULT_BASE"
19+
fi
20+
21+
# Ensure the base ref exists locally
22+
if ! git rev-parse --verify "${DIFF_BASE}^{commit}" >/dev/null 2>&1; then
23+
git fetch --no-tags --prune --depth=1 origin "${DIFF_BASE#origin/}" >/dev/null 2>&1 || true
24+
fi
25+
26+
# If still missing, bail out gracefully to avoid blocking CI
27+
if ! git rev-parse --verify "${DIFF_BASE}^{commit}" >/dev/null 2>&1; then
28+
echo "Base ref ${DIFF_BASE} not found; skipping Prettier check." >&2
29+
exit 0
30+
fi
31+
32+
mapfile -t CHANGED < <(git diff --name-only "${DIFF_BASE}...HEAD" --diff-filter=ACMRT \
33+
| grep -E '\\.(js|cjs|mjs|ts|tsx|json|ya?ml|md)$' || true)
34+
35+
if [[ ${#CHANGED[@]} -eq 0 ]]; then
36+
echo "No supported files changed; skipping Prettier check."
37+
exit 0
38+
fi
39+
40+
echo "Running Prettier check on ${#CHANGED[@]} file(s):"
41+
printf ' - %s\n' "${CHANGED[@]}"
42+
43+
yarn run --silent prettier --check "${CHANGED[@]}"

0 commit comments

Comments
 (0)