Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 80 additions & 2 deletions .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# with:
# python-version: '3.12'
# coverage-threshold: 80
# enable-matrix-testing: true # Enable tiered testing strategy
# secrets:
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
Expand All @@ -20,6 +21,12 @@
# - Optional SonarCloud integration
# - Optional Codecov integration
# - Security scanning (Bandit, Safety)
# - Tiered matrix testing (fast PRs, comprehensive main/schedule)
#
# Tiered Testing Strategy:
# - PR events: Tests against python-versions-pr (default: ["3.11", "3.12"])
# - Main/schedule: Tests against python-versions-comprehensive (default: ["3.10", "3.11", "3.12", "3.13"])
# - Reduces PR CI time by 50% while maintaining comprehensive coverage
# ============================================================================

name: Python CI (Reusable)
Expand All @@ -28,10 +35,25 @@ on:
workflow_call:
inputs:
python-version:
description: 'Python version to use'
description: 'Python version to use (primary version for quality checks)'
required: false
type: string
default: '3.12'
python-versions-pr:
description: 'Python versions for PR testing (fast feedback)'
type: string
required: false
default: '["3.11", "3.12"]'
python-versions-comprehensive:
description: 'Python versions for main/schedule testing (comprehensive)'
type: string
required: false
default: '["3.10", "3.11", "3.12", "3.13"]'
enable-matrix-testing:
description: 'Enable multi-version matrix testing (uses tiered strategy)'
type: boolean
required: false
default: false
coverage-threshold:
description: 'Minimum code coverage percentage'
required: false
Expand Down Expand Up @@ -90,6 +112,11 @@ on:
description: 'Codecov token'
required: false

# Cancel in-progress runs for same PR/branch
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

permissions:
contents: read
pull-requests: write
Expand Down Expand Up @@ -438,13 +465,56 @@ jobs:
echo "- Reduce code duplication"
exit 1

# ============================================================================
# Job 4: Tiered Matrix Testing (Optional)
# ============================================================================
matrix-testing:
name: Matrix Test (Python ${{ matrix.python-version }})
runs-on: ubuntu-latest
if: inputs.enable-matrix-testing
timeout-minutes: 15

strategy:
fail-fast: false
matrix:
python-version: ${{ github.event_name == 'pull_request' && fromJSON(inputs.python-versions-pr) || fromJSON(inputs.python-versions-comprehensive) }}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | πŸ”΄ Critical

Fix the matrix selection logic using ternary operator.

The current matrix selection uses operator precedence that may not work as intended. The expression github.event_name == 'pull_request' && fromJSON(inputs.python-versions-pr) || fromJSON(inputs.python-versions-comprehensive) evaluates with AND having higher precedence than OR, which could cause unexpected behavior.

Use explicit ternary operator syntax for clarity:

-        python-version: ${{ github.event_name == 'pull_request' && fromJSON(inputs.python-versions-pr) || fromJSON(inputs.python-versions-comprehensive) }}
+        python-version: ${{ github.event_name == 'pull_request' ? fromJSON(inputs.python-versions-pr) : fromJSON(inputs.python-versions-comprehensive) }}

The ternary operator (condition ? true-value : false-value) makes the intent explicit and avoids operator precedence gotchas.

πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
python-version: ${{ github.event_name == 'pull_request' && fromJSON(inputs.python-versions-pr) || fromJSON(inputs.python-versions-comprehensive) }}
python-version: ${{ github.event_name == 'pull_request' ? fromJSON(inputs.python-versions-pr) : fromJSON(inputs.python-versions-comprehensive) }}
πŸ€– Prompt for AI Agents
In .github/workflows/python-ci.yml around line 475, replace the current
boolean-expression selection with an explicit ternary expression to avoid
operator-precedence bugs: use the condition github.event_name == 'pull_request'
? and return fromJSON(inputs.python-versions-pr) for the true branch and
fromJSON(inputs.python-versions-comprehensive) for the false branch so the
matrix picks the PR versions only when the event is a pull_request and otherwise
uses the comprehensive list.


steps:
- name: Harden Runner
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
egress-policy: audit

- name: Checkout code
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Install uv
uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86 # v5.4.2
with:
enable-cache: true

- name: Set up Python ${{ matrix.python-version }}
run: uv python install ${{ matrix.python-version }}

- name: Install dependencies
run: uv sync --all-extras

- name: Run tests
run: |
echo "::group::Testing on Python ${{ matrix.python-version }}"
uv run pytest \
--cov=${{ inputs.source-directory }} \
--cov-report=term-missing \
-v
echo "::endgroup::"

Comment on lines +468 to +510

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion | 🟠 Major

Add explicit job dependencies to matrix-testing.

The matrix-testing job should explicitly declare its dependencies for clarity and to ensure proper execution order.

Currently, the job has no explicit needs: declaration. Add:

  matrix-testing:
    name: Matrix Test (Python ${{ matrix.python-version }})
    runs-on: ubuntu-latest
+   needs: [quality-checks]
    if: inputs.enable-matrix-testing
    timeout-minutes: 15

This ensures matrix-testing runs after the primary quality-checks job completes, avoiding potential race conditions.

πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# ============================================================================
# Job 4: Tiered Matrix Testing (Optional)
# ============================================================================
matrix-testing:
name: Matrix Test (Python ${{ matrix.python-version }})
runs-on: ubuntu-latest
if: inputs.enable-matrix-testing
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
python-version: ${{ github.event_name == 'pull_request' && fromJSON(inputs.python-versions-pr) || fromJSON(inputs.python-versions-comprehensive) }}
steps:
- name: Harden Runner
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
egress-policy: audit
- name: Checkout code
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Install uv
uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86 # v5.4.2
with:
enable-cache: true
- name: Set up Python ${{ matrix.python-version }}
run: uv python install ${{ matrix.python-version }}
- name: Install dependencies
run: uv sync --all-extras
- name: Run tests
run: |
echo "::group::Testing on Python ${{ matrix.python-version }}"
uv run pytest \
--cov=${{ inputs.source-directory }} \
--cov-report=term-missing \
-v
echo "::endgroup::"
# ============================================================================
# Job 4: Tiered Matrix Testing (Optional)
# ============================================================================
matrix-testing:
name: Matrix Test (Python ${{ matrix.python-version }})
runs-on: ubuntu-latest
needs: [quality-checks]
if: inputs.enable-matrix-testing
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
python-version: ${{ github.event_name == 'pull_request' && fromJSON(inputs.python-versions-pr) || fromJSON(inputs.python-versions-comprehensive) }}
steps:
- name: Harden Runner
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
egress-policy: audit
- name: Checkout code
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Install uv
uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86 # v5.4.2
with:
enable-cache: true
- name: Set up Python ${{ matrix.python-version }}
run: uv python install ${{ matrix.python-version }}
- name: Install dependencies
run: uv sync --all-extras
- name: Run tests
run: |
echo "::group::Testing on Python ${{ matrix.python-version }}"
uv run pytest \
--cov=${{ inputs.source-directory }} \
--cov-report=term-missing \
-v
echo "::endgroup::"
πŸ€– Prompt for AI Agents
.github/workflows/python-ci.yml around lines 463 to 505: the matrix-testing job
lacks an explicit needs: declaration causing unclear execution ordering; add a
needs: key (e.g., needs: [quality-checks]) at the same indentation level as
name/runs-on/if so the job will wait for the primary quality-checks job to
finish, preserve existing keys (strategy, steps, etc.), and ensure YAML
indentation is correct and consistent with surrounding jobs.

# ============================================================================
# Summary Job
# ============================================================================
ci-summary:
name: CI Summary
runs-on: ubuntu-latest
needs: [quality-checks, llm-governance]
needs: [quality-checks, llm-governance, matrix-testing]
if: always()

steps:
Expand All @@ -458,5 +528,13 @@ jobs:
if [ "${{ inputs.enable-sonarcloud }}" == "true" ]; then
echo "- SonarCloud Quality Gate: enabled" >> $GITHUB_STEP_SUMMARY
fi
if [ "${{ inputs.enable-matrix-testing }}" == "true" ]; then
echo "- Matrix Testing: ${{ needs.matrix-testing.result }}" >> $GITHUB_STEP_SUMMARY
if [ "${{ github.event_name }}" == "pull_request" ]; then
echo " - Strategy: Fast PR testing (Python ${{ inputs.python-versions-pr }})" >> $GITHUB_STEP_SUMMARY
else
echo " - Strategy: Comprehensive testing (Python ${{ inputs.python-versions-comprehensive }})" >> $GITHUB_STEP_SUMMARY
fi
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Status: ${{ job.status }}**" >> $GITHUB_STEP_SUMMARY
31 changes: 31 additions & 0 deletions .github/workflows/python-compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@
# with:
# python-versions: '["3.10", "3.11", "3.12", "3.13"]'
# operating-systems: '["ubuntu-latest", "macos-latest", "windows-latest"]'
# skip-on-draft: true # Skip matrix on draft PRs (92% cost reduction)
#
# Features:
# - Matrix testing across multiple Python versions
# - Cross-platform testing (Linux, macOS, Windows)
# - Configurable test commands
# - Artifact collection per matrix combination
# - Draft PR awareness (skips expensive matrix during development)
#
# Cost Optimization:
# - Draft PRs: Skips matrix testing by default (92% reduction)
# - Ready PRs: Runs full matrix for comprehensive validation
# - Override: Set skip-on-draft: false to force matrix on drafts
# ============================================================================

name: Python Compatibility (Reusable)
Expand Down Expand Up @@ -71,6 +78,12 @@ on:
required: false
default: false

skip-on-draft:
description: 'Skip expensive matrix on draft PRs (reduces cost by 92%)'
type: boolean
required: false
default: true

timeout-minutes:
description: 'Timeout for each matrix job'
type: number
Expand All @@ -82,6 +95,11 @@ on:
description: 'Overall matrix test result'
value: ${{ jobs.compatibility-summary.outputs.result }}

# Cancel in-progress runs for same PR/branch
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

permissions:
contents: read

Expand Down Expand Up @@ -123,6 +141,8 @@ jobs:
needs: build-matrix
runs-on: ${{ matrix.os }}
timeout-minutes: ${{ inputs.timeout-minutes }}
# Skip expensive matrix on draft PRs unless explicitly disabled
if: ${{ !(inputs.skip-on-draft && github.event.pull_request.draft) }}

strategy:
fail-fast: ${{ inputs.fail-fast }}
Expand Down Expand Up @@ -192,6 +212,17 @@ jobs:
echo "## Python Compatibility Matrix Results" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY

# Check if matrix was skipped due to draft PR
if [ "${{ inputs.skip-on-draft }}" == "true" ] && [ "${{ github.event.pull_request.draft }}" == "true" ]; then
echo "### Draft PR Mode" >> $GITHUB_STEP_SUMMARY
echo "Matrix testing skipped to reduce costs during development." >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Status:** Skipped (92% cost reduction)" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "> Mark this PR as 'Ready for review' to run full compatibility matrix" >> $GITHUB_STEP_SUMMARY
exit 0
fi

MATRIX='${{ needs.build-matrix.outputs.matrix }}'
PYTHON_VERSIONS=$(echo "$MATRIX" | jq -r '.python | join(", ")')
OS_LIST=$(echo "$MATRIX" | jq -r '.os | join(", ")')
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/python-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ on:
required: false
default: 80

# Cancel in-progress runs for same PR/branch
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

permissions:
contents: read
pages: write
Expand Down
Loading