Skip to content

Commit 961ea94

Browse files
authored
Enhance Prettify Code workflow for changed files
Updated the GitHub Actions workflow to fetch the base branch and check code formatting only for changed files in pull requests. Signed-off-by: Manas Manohar <[email protected]>
1 parent c37eea7 commit 961ea94

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed
Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
name: Prettify Code
2-
# This action works with pull requests and pushes
32
on:
43
pull_request:
54
push:
@@ -9,18 +8,41 @@ on:
98
jobs:
109
prettier:
1110
runs-on: ubuntu-latest
12-
1311
steps:
1412
- name: Checkout
1513
uses: actions/checkout@v4
1614
with:
1715
fetch-depth: 0
1816

19-
- name: Prettify code
17+
- name: Fetch base branch
18+
if: github.event_name == 'pull_request'
19+
run: git fetch origin ${{ github.event.pull_request.base.ref }}
20+
21+
- name: Get changed files
22+
id: changed-files
23+
run: |
24+
if [ "${{ github.event_name }}" == "pull_request" ]; then
25+
BASE_SHA="origin/${{ github.event.pull_request.base.ref }}"
26+
else
27+
BASE_SHA="${{ github.event.before }}"
28+
fi
29+
30+
git diff --name-only --diff-filter=ACMR -z $BASE_SHA HEAD | \
31+
grep -zE '\.(js|md)$' | \
32+
xargs -0 -r echo > changed_files.txt
33+
34+
if [ -s changed_files.txt ]; then
35+
echo "has_files=true" >> $GITHUB_OUTPUT
36+
echo "Changed files:"
37+
cat changed_files.txt
38+
else
39+
echo "has_files=false" >> $GITHUB_OUTPUT
40+
fi
41+
42+
- name: Check code formatting
43+
if: steps.changed-files.outputs.has_files == 'true'
2044
uses: creyD/[email protected]
2145
with:
22-
# This part is also where you can pass other options, for example:
2346
prettier_version: 2.8.8
24-
prettier_options: --write **/*.{js,md}
25-
same_commit: true
26-
47+
prettier_options: --check $(cat changed_files.txt)
48+
dry_run: true

0 commit comments

Comments
 (0)