Repository navigation
58 lines (52 loc) · 2.22 KB
/
Copy pathprettify_code.yml
File metadata and controls
58 lines (52 loc) · 2.22 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
name: Prettify Code
on:
pull_request:
push:
branches:
- main
jobs:
prettier:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Fetch base branch
if: github.event_name == 'pull_request'
run: git fetch origin ${{ github.event.pull_request.base.ref }}
- name: Get changed files
id: changed-files
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
BASE_SHA="origin/${{ github.event.pull_request.base.ref }}"
else
BASE_SHA="${{ github.event.before }}"
fi
# Get files and store as space-separated string
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMR -z $BASE_SHA HEAD | \
grep -zE '\.(js|md)$' | \
xargs -0 -r)
if [ -n "$CHANGED_FILES" ]; then
echo "files=$CHANGED_FILES" >> $GITHUB_OUTPUT
echo "has_files=true" >> $GITHUB_OUTPUT
echo "Changed files: $CHANGED_FILES"
else
echo "has_files=false" >> $GITHUB_OUTPUT
fi
# Prettier is invoked directly rather than through
# creyD/prettier_action@v4.6, which accepts a prettier_version input but
# does not honour it: with 2.8.8 declared, the action installed 3.9.6
# ("The following package was not found and will be installed:
# prettier@3.9.6"). So CI silently enforced a different formatting
# contract than CONTRIBUTING and .prettierrc.json describe, and the two
# could drift apart without anyone noticing — a file formatted by hand to
# match the documented version could fail here, and vice versa.
#
# `npx prettier@<version>` pins what actually runs, so the version in this
# file is the version that checks the code. 2.8.8 is kept because it is
# what the repository is formatted with; 3.x reflows markdown differently
# and moving to it is a deliberate migration, not a CI detail.
- name: Check code formatting
if: steps.changed-files.outputs.has_files == 'true'
run: npx --yes prettier@2.8.8 --check ${{ steps.changed-files.outputs.files }}