Skip to content

Commit 4d696ff

Browse files
committed
[lint apply] Add PR-only pre-commit workflow
Add .github/workflows/format.yml: a GitHub Actions workflow that runs pre-commit only for changed files in PRs. It triggers on pull_request (opened, synchronize, reopened), uses a detect_changes job to checkout full history and compute the list of files changed against the base branch, and exposes that list as an output. A precommit job (guarded by a condition that changed files exist) checks out the PR branch, sets up Python 3.12, installs pre-commit, and runs pre-commit only on the changed files.
1 parent 35e0432 commit 4d696ff

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

.github/workflows/format.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: pre-commit (PR only on changed files)
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
jobs:
8+
9+
detect_changes:
10+
runs-on: ubuntu-latest
11+
12+
outputs:
13+
changed: ${{ steps.changed_files.outputs.changed }}
14+
15+
steps:
16+
- name: Checkout full history
17+
uses: actions/checkout@v6
18+
with:
19+
fetch-depth: 0
20+
21+
- name: Detect changed files
22+
id: changed_files
23+
run: |
24+
git fetch origin ${{ github.base_ref }}
25+
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD)
26+
echo "changed=$CHANGED_FILES" >> $GITHUB_OUTPUT
27+
28+
- name: Show changed files
29+
run: |
30+
echo "Changed files: ${{ steps.changed_files.outputs.changed }}"
31+
32+
33+
precommit:
34+
runs-on: ubuntu-latest
35+
needs: detect_changes
36+
37+
if: ${{ needs.detect_changes.outputs.changed != '' }}
38+
39+
steps:
40+
- name: Checkout PR branch
41+
uses: actions/checkout@v6
42+
with:
43+
fetch-depth: 0
44+
ref: ${{ github.head_ref }}
45+
46+
- name: Set up Python
47+
uses: actions/setup-python@v6
48+
with:
49+
python-version: "3.12"
50+
51+
- name: Install pre-commit
52+
run: pip install pre-commit
53+
54+
- name: Run pre-commit on changed files
55+
run: pre-commit run --files ${{ needs.detect_changes.outputs.changed }}

0 commit comments

Comments
 (0)