diff --git a/.github/workflows/ruff-check.yml b/.github/workflows/ruff-check.yml index 8e5cbbce..104738a9 100644 --- a/.github/workflows/ruff-check.yml +++ b/.github/workflows/ruff-check.yml @@ -1,38 +1,64 @@ -name: Ruff check for python code +name: Ruff check on changed files only on: - push: - branches: - - main - paths: - - 'api-server/**' - - 'state-manager/**' - pull_request: - branches: - - main - paths: - - 'api-server/**' - - 'state-manager/**' - workflow_dispatch: + push: + branches: [main] + pull_request: + branches: [main] + workflow_dispatch: permissions: - contents: read - id-token: write - pages: write + contents: read jobs: - check: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: '3.12' - - name: Ruff Check - uses: astral-sh/ruff-action@v3 - with: - src: './api-server' - - name: Ruff Check - uses: astral-sh/ruff-action@v3 - with: - src: './state-manager' + 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 in api-server/ and state-manager/ + - 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.sha }} + else + BASE_SHA=${{ github.event.before }} + HEAD_SHA=${{ github.event.after }} + fi + + # List all changed .py files in target directories + FILES=$(git diff --name-only --diff-filter=ACMRT $BASE_SHA $HEAD_SHA \ + | grep -E '^(api-server|state-manager)/.*\.py$' || true) + + echo "files=$FILES" >> "$GITHUB_OUTPUT" + + + + + # 4. Install and Run Ruff on changed files, if any + - name: Run Ruff on changed files + if: steps.changed-files.outputs.files != '' + run: | + pip install ruff + echo "Linting the following Python files:" + echo "${{ steps.changed-files.outputs.files }}" + ruff check ${{ steps.changed-files.outputs.files }} + + # 5. Skip Ruff when no relevant files changed + - name: Skip Ruff if no files changed + if: steps.changed-files.outputs.files == '' + run: | + echo "No Python files changed in api-server/ or state-manager/. Skipping Ruff."