chore: fix Pyink GitHub Action to check on all changed .py files

Three fixes:
1. used "fetch-depth: 2" to ensure that files in all commits in the PR are checked
2. triggered by all changed ".py" files in the repo
3. merged the "check modified files" and "run Pyink" steps because $GITHUB_ENV cannot handle multi-line env vars without special treatment

PiperOrigin-RevId: 766793736
This commit is contained in:
Liang Wu 2025-06-03 13:18:33 -07:00 committed by Copybara-Service
parent 934af25343
commit ed2aef9120

View File

@ -17,8 +17,7 @@ name: Check Pyink Formatting
on: on:
pull_request: pull_request:
paths: paths:
- 'src/**/*.py' - '**.py'
- 'tests/**/*.py'
- 'pyproject.toml' - 'pyproject.toml'
jobs: jobs:
@ -28,6 +27,8 @@ jobs:
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Set up Python - name: Set up Python
uses: actions/setup-python@v5 uses: actions/setup-python@v5
@ -38,36 +39,31 @@ jobs:
run: | run: |
pip install pyink pip install pyink
- name: Detect changed Python files - name: Run pyink on changed files
id: detect_changes id: run_pyink
run: | run: |
git fetch origin ${{ github.base_ref }} git fetch origin ${{ github.base_ref }}
CHANGED_FILES=$(git diff --diff-filter=ACMR --name-only origin/${{ github.base_ref }}...HEAD | grep -E '\.py$' || true) CHANGED_FILES=$(git diff --diff-filter=ACMR --name-only origin/${{ github.base_ref }}...HEAD | grep -E '\.py$' || true)
echo "CHANGED_FILES=${CHANGED_FILES}" >> $GITHUB_ENV if [ -n "$CHANGED_FILES" ]; then
echo "Changed Python files:"
- name: Run pyink on changed files echo "$CHANGED_FILES"
if: env.CHANGED_FILES != ''
run: |
echo "Changed Python files:"
echo "$CHANGED_FILES"
# Run pyink --check
set +e
pyink --check --config pyproject.toml $CHANGED_FILES
RESULT=$?
set -e
if [ $RESULT -ne 0 ]; then
echo "" echo ""
echo "❌ Pyink formatting check failed!" FORMATTED_FILES=$(echo "$CHANGED_FILES" | tr '\n' ' ')
echo "👉 To fix formatting, run locally:"
echo "" # Run pyink --check
echo " pyink --config pyproject.toml $CHANGED_FILES" set +e
echo "" pyink --check --diff --config pyproject.toml $CHANGED_FILES
exit $RESULT RESULT=$?
set -e
if [ $RESULT -ne 0 ]; then
echo ""
echo "❌ Pyink formatting check failed!"
echo "👉 To fix formatting, run locally:"
echo ""
echo " pyink --config pyproject.toml $FORMATTED_FILES"
echo ""
exit $RESULT
fi
else
echo "No Python files changed. Skipping pyink check."
fi fi
- name: No changed Python files detected
if: env.CHANGED_FILES == ''
run: |
echo "No Python files changed. Skipping pyink check."