Skip to content

Commit

Permalink
chore(lint): split test coverage commenter
Browse files Browse the repository at this point in the history
See comment on test.yml for explanation.
  • Loading branch information
MaxMustermann2 committed Mar 12, 2024
1 parent 86ec57b commit 3bc2eee
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 17 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/test-comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# This workflow runs after test.yml and comments the test coverage on the pull request.
name: Comment test coverage on the pull request

on:
workflow_run:
workflows: ["Tests"]
types:
- completed

jobs:
upload:
runs-on: ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
steps:
- name: 'Download artifact'
uses: actions/github-script@v3.1.0
with:
script: |
var artifacts = await github.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id }},
});
var matchArtifact = artifacts.data.artifacts.find((artifact) => {
return artifact.name == "result"
})[0];
if (!matchArtifact) {
var core = require('@actions/core');
core.setFailed('Artifact "result" not found.');
return;
}
var download = await github.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
var fs = require('fs');
fs.writeFileSync('${{github.workspace}}/result.zip', Buffer.from(download.data));
- run: unzip result.zip -d result
- name: Read PR Number and Coverage
id: read_files
run: |
pr_number=$(cat result/pr_number.txt)
coverage=$(cat result/coverage.txt)
echo "PR_NUMBER=${pr_number}" >> $GITHUB_ENV
echo "COVERAGE=${coverage}" >> $GITHUB_ENV
- name: Find comment
id: find-comment
uses: peter-evans/find-comment@v2
with:
issue-number: ${{ env.PR_NUMBER }}
comment-author: 'github-actions[bot]'
- name: Comment coverage on PR
uses: peter-evans/create-or-update-comment@v3
with:
issue-number: ${{ env.PR_NUMBER }}
comment-id: ${{ steps.find-comment.outputs.comment-id }}
body: |
Coverage as of ${{ github.sha }}: ${{ env.COVERAGE }}%
edit-mode: append
35 changes: 18 additions & 17 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# This workflow runs on every push and pull request to the repository.
# It then calculates the unit test coverage and checks if it's above a certain threshold.
# this information is passed on to another workflow as artifacts for commenting on the PR.
# This is because the `pull_request` event does not have the commenting permissions.
# We could switch to `pull_request_target` which does have them, however, it
# opens a security hole. See:
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
name: Tests
on:
# for write permission, use pull_request_target and not pull_request.
pull_request_target:
pull_request:
push:
branches:
- develop
Expand All @@ -10,8 +16,9 @@ on:
- release/**

permissions:
# for uploading artifacts
contents: write
pull-requests: write
pull-requests: read

# Automatically cancel run if another commit to the same ref is detected.
concurrency:
Expand All @@ -30,7 +37,6 @@ jobs:
- uses: technote-space/get-diff-action@v6.1.2
with:
PATTERNS: |
**/**.sol
**/**.go
go.mod
go.sum
Expand All @@ -47,19 +53,14 @@ jobs:
# TODO: increase this threshold with time to 80
threshold-total: 10
if: env.GIT_DIFF
- name: Find comment
id: find-comment
uses: peter-evans/find-comment@v2
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
- name: Generate artifact for PR
run: |
mkdir -p ./result/
echo ${{ steps.output-coverage.outputs.total-coverage }} > ./result/coverage.txt
echo ${{ github.event.pull_request.number }} > ./result/pr_number.txt
if: env.GIT_DIFF && github.event_name == 'pull_request'
- name: Comment coverage on PR
uses: peter-evans/create-or-update-comment@v3
- uses: actions/upload-artifact@v2
with:
issue-number: ${{ github.event.pull_request.number }}
comment-id: ${{ steps.find-comment.outputs.comment-id }}
body: |
Coverage as of ${{ github.sha }}: ${{ steps.output-coverage.outputs.total-coverage }}%
edit-mode: append
name: result
path: result/
if: env.GIT_DIFF && github.event_name == 'pull_request'

0 comments on commit 3bc2eee

Please sign in to comment.