|
| 1 | +name: Tests |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [master] |
| 6 | + pull_request: |
| 7 | + branches: [master] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + test: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + strategy: |
| 17 | + matrix: |
| 18 | + python-version: ["3.13"] |
| 19 | + |
| 20 | + steps: |
| 21 | + - uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + |
| 25 | + - name: Set up Python ${{ matrix.python-version }} |
| 26 | + uses: actions/setup-python@v5 |
| 27 | + with: |
| 28 | + python-version: ${{ matrix.python-version }} |
| 29 | + |
| 30 | + - name: Install dependencies |
| 31 | + run: | |
| 32 | + python -m pip install --upgrade pip |
| 33 | + pip install -e ".[test]" |
| 34 | +
|
| 35 | + - name: Get commit info |
| 36 | + id: commit-info |
| 37 | + run: | |
| 38 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 39 | + COMMIT_SHA="${{ github.event.pull_request.head.sha }}" |
| 40 | + else |
| 41 | + COMMIT_SHA="${{ github.sha }}" |
| 42 | + fi |
| 43 | + echo "sha=${COMMIT_SHA}" >> "$GITHUB_OUTPUT" |
| 44 | + echo "short_sha=$(echo "$COMMIT_SHA" | cut -c1-7)" >> "$GITHUB_OUTPUT" |
| 45 | + echo "message=$(git log -1 --pretty=%s "$COMMIT_SHA")" >> "$GITHUB_OUTPUT" |
| 46 | +
|
| 47 | + - name: Run tests with coverage |
| 48 | + run: | |
| 49 | + set -o pipefail |
| 50 | + python -m pytest tests/ -v --tb=short \ |
| 51 | + --cov=autohive_integrations_sdk \ |
| 52 | + --cov-report=term-missing \ |
| 53 | + --cov-report=xml:coverage.xml \ |
| 54 | + | tee pytest-coverage.txt |
| 55 | +
|
| 56 | + - name: Build coverage comment |
| 57 | + id: coverage |
| 58 | + run: | |
| 59 | + TOTAL=$(grep '^TOTAL' pytest-coverage.txt | awk '{print $(NF-1)}') |
| 60 | + echo "total=${TOTAL}" >> "$GITHUB_OUTPUT" |
| 61 | +
|
| 62 | + REPO_URL="${{ github.server_url }}/${{ github.repository }}" |
| 63 | + SHA="${{ steps.commit-info.outputs.sha }}" |
| 64 | + SHORT_SHA="${{ steps.commit-info.outputs.short_sha }}" |
| 65 | + COMMIT_LINK="[\`${SHORT_SHA}\`](${REPO_URL}/commit/${SHA})" |
| 66 | + TITLE="Coverage — ${COMMIT_LINK} (${{ steps.commit-info.outputs.message }}) by @${{ github.actor }}" |
| 67 | +
|
| 68 | + # Convert "308, 315-320, 347" into linked line numbers |
| 69 | + linkify_lines() { |
| 70 | + local file="$1" missing="$2" |
| 71 | + if [ -z "$missing" ]; then |
| 72 | + echo "" |
| 73 | + return |
| 74 | + fi |
| 75 | + local result="" |
| 76 | + IFS=', ' read -ra PARTS <<< "$missing" |
| 77 | + for part in "${PARTS[@]}"; do |
| 78 | + [ -z "$part" ] && continue |
| 79 | + if [[ "$part" == *-* ]]; then |
| 80 | + local start="${part%-*}" end="${part#*-}" |
| 81 | + local link="[${part}](${REPO_URL}/blob/${SHA}/${file}#L${start}-L${end})" |
| 82 | + else |
| 83 | + local link="[${part}](${REPO_URL}/blob/${SHA}/${file}#L${part})" |
| 84 | + fi |
| 85 | + if [ -n "$result" ]; then |
| 86 | + result="${result}, ${link}" |
| 87 | + else |
| 88 | + result="${link}" |
| 89 | + fi |
| 90 | + done |
| 91 | + echo "$result" |
| 92 | + } |
| 93 | +
|
| 94 | + { |
| 95 | + echo "<!-- coverage-comment -->" |
| 96 | + echo "### ${TITLE}" |
| 97 | + echo "" |
| 98 | + echo "**Total coverage: ${TOTAL}**" |
| 99 | + echo "" |
| 100 | + echo "| File | Stmts | Miss | Cover | Missing |" |
| 101 | + echo "|------|-------|------|-------|---------|" |
| 102 | + grep '^src/' pytest-coverage.txt | while IFS= read -r line; do |
| 103 | + FILE=$(echo "$line" | awk '{print $1}') |
| 104 | + STMTS=$(echo "$line" | awk '{print $2}') |
| 105 | + MISS=$(echo "$line" | awk '{print $3}') |
| 106 | + COV=$(echo "$line" | awk '{print $4}') |
| 107 | + MISSING=$(echo "$line" | awk '{for(i=5;i<=NF;i++) printf "%s ", $i; print ""}' | xargs) |
| 108 | + LINKED=$(linkify_lines "$FILE" "$MISSING") |
| 109 | + echo "| \`${FILE}\` | ${STMTS} | ${MISS} | ${COV} | ${LINKED} |" |
| 110 | + done |
| 111 | + } > coverage-comment.md |
| 112 | +
|
| 113 | + - name: Post coverage comment on PR |
| 114 | + if: github.event_name == 'pull_request' |
| 115 | + uses: actions/github-script@v7 |
| 116 | + with: |
| 117 | + script: | |
| 118 | + const fs = require('fs'); |
| 119 | + const body = fs.readFileSync('coverage-comment.md', 'utf8'); |
| 120 | + const marker = '<!-- coverage-comment -->'; |
| 121 | +
|
| 122 | + const { data: comments } = await github.rest.issues.listComments({ |
| 123 | + owner: context.repo.owner, |
| 124 | + repo: context.repo.repo, |
| 125 | + issue_number: context.issue.number, |
| 126 | + }); |
| 127 | + const existing = comments.find(c => c.body.includes(marker)); |
| 128 | +
|
| 129 | + if (existing) { |
| 130 | + await github.rest.issues.updateComment({ |
| 131 | + owner: context.repo.owner, |
| 132 | + repo: context.repo.repo, |
| 133 | + comment_id: existing.id, |
| 134 | + body, |
| 135 | + }); |
| 136 | + } else { |
| 137 | + await github.rest.issues.createComment({ |
| 138 | + owner: context.repo.owner, |
| 139 | + repo: context.repo.repo, |
| 140 | + issue_number: context.issue.number, |
| 141 | + body, |
| 142 | + }); |
| 143 | + } |
| 144 | +
|
| 145 | + - name: Update coverage badge |
| 146 | + if: github.ref == 'refs/heads/master' && github.event_name == 'push' |
| 147 | + uses: schneegans/dynamic-badges-action@v1.7.0 |
| 148 | + with: |
| 149 | + auth: ${{ secrets.GIST_SECRET }} |
| 150 | + gistID: e8adf35c8508876ab8ba09422ddc2535 |
| 151 | + filename: coverage-badge.json |
| 152 | + label: coverage |
| 153 | + message: ${{ steps.coverage.outputs.total }} |
| 154 | + valColorRange: ${{ steps.coverage.outputs.total }} |
| 155 | + minColorRange: 50 |
| 156 | + maxColorRange: 100 |
0 commit comments