81 lines (71 loc) Β· 2.71 KB
/
coverage.yaml
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
name: coverage
on:
# this allows for `contents: write` and `pull-requests: write` from forks
pull_request_target:
# limit default permissions to just read-only checkouts
permissions:
contents: read
jobs:
coverage:
runs-on: ubuntu-latest
strategy:
matrix:
branch: [pr, main]
outputs:
cov-pr: ${{ steps.coverage.outputs.cov-pr }}
cov-main: ${{ steps.coverage.outputs.cov-main }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
# for safety, `pull_request_target` changes the default checkout to be the target branch, so we have to request the merge (we're limited to `permissions: {contents: read}`, so this is ok)
ref: ${{ matrix.branch == 'main' && 'main' || (github.event.pull_request && format('refs/pull/{0}/merge', github.event.pull_request.number)) || '' }}
- name: Bazel cache
id: bazel-cache
uses: actions/cache@v3
with:
path: ~/.cache/bazel
key: ${{ runner.os }}-3.10-bazel-cache-${{ hashFiles('**/BUILD', '.bazelrc', '.bazelversion', 'WORKSPACE') }}
restore-keys: ${{ runner.os }}-3.10-bazel-cache-
- name: Install poetry
run: pipx install poetry
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
cache: poetry
- name: Install dependencies
run: poetry install --no-interaction
- name: Run coverage
id: coverage
run: |
poetry run ./tools/coverage.sh
poetry run coverage json
content=$(cat ./coverage.json)
echo "cov-${{ matrix.branch }}=$content" >> $GITHUB_OUTPUT
comment:
runs-on: ubuntu-latest
needs: [coverage]
permissions:
# needed to add coverage comment to the pull request
pull-requests: write
steps:
- uses: actions/github-script@v6
with:
script: |
const pr = ${{fromJson(needs.coverage.outputs.cov-pr).totals.percent_covered}};
const main = ${{fromJson(needs.coverage.outputs.cov-main).totals.percent_covered}};
const diff = pr - main;
const report = `### Coverage report
**Main**: ${main.toFixed(2)}% | **PR**: ${pr.toFixed(2)}% | **Diff: ${diff.toFixed(2)} ${diff >= 0 ? 'β
' : 'β οΈ'}**`;
await core.summary.addRaw(report).write()
try {
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: report
})
} catch (e) {
console.log("Could not post comment to pull request", e)
}