Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coverage workflow #385

Merged
merged 4 commits into from Mar 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 26 additions & 9 deletions .github/workflows/coverage.yaml
@@ -1,6 +1,12 @@
name: coverage

on: [push]
on:
# this allows for `contents: write` and `pull-requests: write` from forks
pull_request_target:
ianspektor marked this conversation as resolved.
Show resolved Hide resolved

# limit default permissions to just read-only checkouts
permissions:
contents: read
ianspektor marked this conversation as resolved.
Show resolved Hide resolved

jobs:
coverage:
Expand All @@ -15,7 +21,8 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ matrix.branch == 'main' && 'main' || '' }}
# 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)) || '' }}
ianspektor marked this conversation as resolved.
Show resolved Hide resolved

- name: Bazel cache
id: bazel-cache
Expand Down Expand Up @@ -48,17 +55,27 @@ jobs:
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;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `### Coverage report
**Main**: ${main.toFixed(2)}% | **PR**: ${pr.toFixed(2)}% | **Diff: ${diff.toFixed(2)} ${diff >= 0 ? '✅' : '⚠️'}**`
})
const report = `### Coverage report
**Main**: ${main.toFixed(2)}% | **PR**: ${pr.toFixed(2)}% | **Diff: ${diff.toFixed(2)} ${diff >= 0 ? '✅' : '⚠️'}**`;
await core.summary.addRaw(report).write()
jsoref marked this conversation as resolved.
Show resolved Hide resolved
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)
}