-
Notifications
You must be signed in to change notification settings - Fork 0
156 lines (140 loc) · 5.32 KB
/
tests.yml
File metadata and controls
156 lines (140 loc) · 5.32 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
name: Tests
on:
push:
branches: [master]
pull_request:
branches: [master]
permissions:
contents: read
pull-requests: write
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.13"]
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e ".[test]"
- name: Get commit info
id: commit-info
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
COMMIT_SHA="${{ github.event.pull_request.head.sha }}"
else
COMMIT_SHA="${{ github.sha }}"
fi
echo "sha=${COMMIT_SHA}" >> "$GITHUB_OUTPUT"
echo "short_sha=$(echo "$COMMIT_SHA" | cut -c1-7)" >> "$GITHUB_OUTPUT"
echo "message=$(git log -1 --pretty=%s "$COMMIT_SHA")" >> "$GITHUB_OUTPUT"
- name: Run tests with coverage
run: |
set -o pipefail
python -m pytest tests/ -v --tb=short \
--cov=autohive_integrations_sdk \
--cov-report=term-missing \
--cov-report=xml:coverage.xml \
| tee pytest-coverage.txt
- name: Build coverage comment
id: coverage
run: |
TOTAL=$(grep '^TOTAL' pytest-coverage.txt | awk '{print $4}')
echo "total=${TOTAL}" >> "$GITHUB_OUTPUT"
REPO_URL="${{ github.server_url }}/${{ github.repository }}"
SHA="${{ steps.commit-info.outputs.sha }}"
SHORT_SHA="${{ steps.commit-info.outputs.short_sha }}"
COMMIT_LINK="[\`${SHORT_SHA}\`](${REPO_URL}/commit/${SHA})"
TITLE="Coverage — ${COMMIT_LINK} (${{ steps.commit-info.outputs.message }}) by @${{ github.actor }}"
# Convert "308, 315-320, 347" into linked line numbers
linkify_lines() {
local file="$1" missing="$2"
if [ -z "$missing" ]; then
echo ""
return
fi
local result=""
IFS=', ' read -ra PARTS <<< "$missing"
for part in "${PARTS[@]}"; do
[ -z "$part" ] && continue
if [[ "$part" == *-* ]]; then
local start="${part%-*}" end="${part#*-}"
local link="[${part}](${REPO_URL}/blob/${SHA}/${file}#L${start}-L${end})"
else
local link="[${part}](${REPO_URL}/blob/${SHA}/${file}#L${part})"
fi
if [ -n "$result" ]; then
result="${result}, ${link}"
else
result="${link}"
fi
done
echo "$result"
}
{
echo "<!-- coverage-comment -->"
echo "### ${TITLE}"
echo ""
echo "**Total coverage: ${TOTAL}**"
echo ""
echo "| File | Stmts | Miss | Cover | Missing |"
echo "|------|-------|------|-------|---------|"
grep '^src/' pytest-coverage.txt | while IFS= read -r line; do
FILE=$(echo "$line" | awk '{print $1}')
STMTS=$(echo "$line" | awk '{print $2}')
MISS=$(echo "$line" | awk '{print $3}')
COV=$(echo "$line" | awk '{print $4}')
MISSING=$(echo "$line" | awk '{for(i=5;i<=NF;i++) printf "%s ", $i; print ""}' | xargs)
LINKED=$(linkify_lines "$FILE" "$MISSING")
echo "| \`${FILE}\` | ${STMTS} | ${MISS} | ${COV} | ${LINKED} |"
done
} > coverage-comment.md
- name: Post coverage comment on PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const body = fs.readFileSync('coverage-comment.md', 'utf8');
const marker = '<!-- coverage-comment -->';
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existing = comments.find(c => c.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}
- name: Update coverage badge
if: github.ref == 'refs/heads/master' && github.event_name == 'push'
uses: schneegans/dynamic-badges-action@v1.7.0
with:
auth: ${{ secrets.GIST_SECRET }}
gistID: e8adf35c8508876ab8ba09422ddc2535
filename: coverage-badge.json
label: coverage
message: ${{ steps.coverage.outputs.total }}
valColorRange: ${{ steps.coverage.outputs.total }}
minColorRange: 50
maxColorRange: 100