|
| 1 | +name: Test and Lint |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: # Safer than pull_request_target for untrusted code |
| 5 | + branches: [ main, strands ] |
| 6 | + types: [opened, synchronize, reopened, ready_for_review, review_requested, review_request_removed] |
| 7 | + push: |
| 8 | + branches: [ main ] # Also run on direct pushes to main |
| 9 | + |
| 10 | +jobs: |
| 11 | + check-approval: |
| 12 | + name: Check if PR has contributor approval |
| 13 | + runs-on: ubuntu-latest |
| 14 | + permissions: |
| 15 | + pull-requests: read |
| 16 | + # Skip this check for direct pushes to main |
| 17 | + if: github.event_name == 'pull_request' |
| 18 | + outputs: |
| 19 | + approved: ${{ steps.check-approval.outputs.approved }} |
| 20 | + steps: |
| 21 | + - name: Check if PR has been approved by a contributor |
| 22 | + id: check-approval |
| 23 | + uses: actions/github-script@v7 |
| 24 | + with: |
| 25 | + script: | |
| 26 | + const APPROVED_ASSOCIATION = ['COLLABORATOR', 'CONTRIBUTOR', 'MEMBER', 'OWNER'] |
| 27 | + const PR_AUTHOR_ASSOCIATION = context.payload.pull_request.author_association; |
| 28 | + const { data: reviews } = await github.rest.pulls.listReviews({ |
| 29 | + owner: context.repo.owner, |
| 30 | + repo: context.repo.repo, |
| 31 | + pull_number: context.issue.number, |
| 32 | + }); |
| 33 | + |
| 34 | + const isApprovedContributor = APPROVED_ASSOCIATION.includes(PR_AUTHOR_ASSOCIATION); |
| 35 | + |
| 36 | + // Check if any contributor has approved |
| 37 | + const isApproved = reviews.some(review => |
| 38 | + review.state === 'APPROVED' && APPROVED_ASSOCIATION.includes(review.author_association) |
| 39 | + ) || isApprovedContributor; |
| 40 | + |
| 41 | + core.setOutput('approved', isApproved); |
| 42 | + |
| 43 | + if (!isApproved) { |
| 44 | + core.notice('This PR does not have approval from a Contributor. Workflow will not run test jobs.'); |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + return true; |
| 49 | +
|
| 50 | + unit-test: |
| 51 | + name: Unit Tests - Python ${{ matrix.python-version }} - ${{ matrix.os-name }} |
| 52 | + needs: check-approval |
| 53 | + permissions: |
| 54 | + contents: read |
| 55 | + # Only run if PR is approved or this is a direct push to main |
| 56 | + if: github.event_name == 'push' || needs.check-approval.outputs.approved == 'true' |
| 57 | + strategy: |
| 58 | + matrix: |
| 59 | + include: |
| 60 | + # Linux |
| 61 | + - os: ubuntu-latest |
| 62 | + os-name: linux |
| 63 | + python-version: "3.10" |
| 64 | + - os: ubuntu-latest |
| 65 | + os-name: linux |
| 66 | + python-version: "3.11" |
| 67 | + - os: ubuntu-latest |
| 68 | + os-name: linux |
| 69 | + python-version: "3.12" |
| 70 | + - os: ubuntu-latest |
| 71 | + os-name: linux |
| 72 | + python-version: "3.13" |
| 73 | + # Windows |
| 74 | + - os: windows-latest |
| 75 | + os-name: windows |
| 76 | + python-version: "3.10" |
| 77 | + - os: windows-latest |
| 78 | + os-name: windows |
| 79 | + python-version: "3.11" |
| 80 | + - os: windows-latest |
| 81 | + os-name: windows |
| 82 | + python-version: "3.12" |
| 83 | + - os: windows-latest |
| 84 | + os-name: windows |
| 85 | + python-version: "3.13" |
| 86 | + # MacOS - latest only; not enough runners for MacOS |
| 87 | + - os: macos-latest |
| 88 | + os-name: macos |
| 89 | + python-version: "3.13" |
| 90 | + fail-fast: false |
| 91 | + runs-on: ${{ matrix.os }} |
| 92 | + env: |
| 93 | + LOG_LEVEL: DEBUG |
| 94 | + steps: |
| 95 | + - name: Checkout code |
| 96 | + uses: actions/checkout@v4 |
| 97 | + with: |
| 98 | + ref: ${{ github.event.pull_request.head.sha }} # Explicitly define which commit to checkout |
| 99 | + persist-credentials: false # Don't persist credentials for subsequent actions |
| 100 | + - name: Set up Python |
| 101 | + uses: actions/setup-python@v5 |
| 102 | + with: |
| 103 | + python-version: ${{ matrix.python-version }} |
| 104 | + - name: Install dependencies |
| 105 | + run: | |
| 106 | + pip install --no-cache-dir hatch |
| 107 | + - name: Run Unit tests |
| 108 | + id: tests |
| 109 | + run: hatch test tests --cover |
| 110 | + continue-on-error: false |
| 111 | + |
| 112 | + lint: |
| 113 | + name: Lint |
| 114 | + runs-on: ubuntu-latest |
| 115 | + needs: check-approval |
| 116 | + permissions: |
| 117 | + contents: read |
| 118 | + if: github.event_name == 'push' || needs.check-approval.outputs.approved == 'true' |
| 119 | + steps: |
| 120 | + - name: Checkout code |
| 121 | + uses: actions/checkout@v4 |
| 122 | + with: |
| 123 | + ref: ${{ github.event.pull_request.head.sha }} |
| 124 | + persist-credentials: false |
| 125 | + |
| 126 | + - name: Set up Python |
| 127 | + uses: actions/setup-python@v5 |
| 128 | + with: |
| 129 | + python-version: '3.10' |
| 130 | + cache: 'pip' |
| 131 | + |
| 132 | + - name: Install dependencies |
| 133 | + run: | |
| 134 | + pip install --no-cache-dir hatch |
| 135 | +
|
| 136 | + - name: Run lint |
| 137 | + id: lint |
| 138 | + run: hatch run test-lint |
| 139 | + continue-on-error: false |
0 commit comments