-
Notifications
You must be signed in to change notification settings - Fork 43
78 lines (66 loc) · 2.36 KB
/
ruff-check.yml
File metadata and controls
78 lines (66 loc) · 2.36 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
name: Ruff check on changed files only
on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:
permissions:
contents: read
jobs:
ruff-changed-files:
runs-on: ubuntu-latest
steps:
# 1. Checkout entire history for accurate diffs
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
# 2. Set up Python for Ruff
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
# 3. Determine changed Python files
- name: Get changed Python files
id: changed-files
run: |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
BASE_SHA=${{ github.event.pull_request.base.sha }}
HEAD_SHA=${{ github.event.pull_request.head.sha }}
else
BASE_SHA=${{ github.event.before }}
HEAD_SHA=${{ github.sha }}
fi
echo "BASE_SHA: $BASE_SHA"
echo "HEAD_SHA: $HEAD_SHA"
# List all changed .py files
FILES=$(git diff --name-only --diff-filter=ACMRT $BASE_SHA $HEAD_SHA \
| grep -E '\.py$' || true)
# Handle multi-line output properly for GitHub Actions
if [ -n "$FILES" ]; then
{
echo 'files<<EOF'
echo "$FILES"
echo 'EOF'
} >> "$GITHUB_OUTPUT"
echo "has_files=true" >> "$GITHUB_OUTPUT"
else
echo "files=" >> "$GITHUB_OUTPUT"
echo "has_files=false" >> "$GITHUB_OUTPUT"
fi
# 4. Install and Run Ruff on changed files, if any
- name: Run Ruff on changed files
if: steps.changed-files.outputs.has_files == 'true'
run: |
pip install ruff
echo "Linting the following Python files:"
echo "${{ steps.changed-files.outputs.files }}"
# Convert multiline string to space-separated for ruff
FILES_ARGS=$(echo "${{ steps.changed-files.outputs.files }}" | tr '\n' ' ')
ruff check $FILES_ARGS
# 5. Skip Ruff when no relevant files changed
- name: Skip Ruff if no files changed
if: steps.changed-files.outputs.has_files == 'false'
run: |
echo "No Python files changed. Skipping Ruff."