Skip to content

Commit

Permalink
Merge pull request google#385 from jsoref/fix-coverage-workflow
Browse files Browse the repository at this point in the history
Fix coverage workflow
  • Loading branch information
ianspektor committed Mar 8, 2024
2 parents 9bd384c + 8c765eb commit 05e2a26
Showing 1 changed file with 26 additions and 9 deletions.
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:

# limit default permissions to just read-only checkouts
permissions:
contents: read

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)) || '' }}

- 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()
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)
}

0 comments on commit 05e2a26

Please sign in to comment.