Skip to content

Commit

Permalink
Introduced a workflow to calculate the test coverage of the codebase …
Browse files Browse the repository at this point in the history
…and post it as a comment in the PR.
  • Loading branch information
jp-imx committed Nov 13, 2023
1 parent 81697c0 commit 953fd85
Show file tree
Hide file tree
Showing 9 changed files with 16,139 additions and 373 deletions.
12 changes: 12 additions & 0 deletions .github/actions/coverage/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
name: 'Publish coverage report'
description: 'Create or update PR comment with coverage report'

inputs:
coverage:
description: The coverage report to publish
required: true

runs:
using: 'node16'
main: 'index.js'
16 changes: 16 additions & 0 deletions .github/actions/coverage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const core = require('@actions/core');
const fs = require('fs');

const inputs = {
coverage: core.getInput('coverage'),
target: core.getInput('target'),
};


(async () => {
const { updateComment } = require("../../utils/github");

const data = fs.readFileSync(inputs.coverage, { encoding: 'utf8', flag: 'r' });
await updateComment(inputs.target, data);
})();

21 changes: 21 additions & 0 deletions .github/actions/node-cache/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: "Cache Node modules"
description: "Cache Node modules"

runs:
using: "composite"
steps:
- name: Restore node modules from cache
id: cache-nodemodules
uses: actions/cache/restore@v3
with:
# cache whole node_modules instead of ./npm, means `npm ci` will be skipped if cache hit, https://www.voorhoede.nl/en/blog/super-fast-npm-install-on-github-actions/
path: ./node_modules
key: ${{ runner.os }}-nodemodules-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-nodemodules-
- name: Install dependencies
if: steps.cache-nodemodules.outputs.cache-hit != 'true'
shell: bash
run: yarn install --ignore-scripts
47 changes: 47 additions & 0 deletions .github/utils/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const { getOctokit, context } = require("@actions/github");
const { EOL } = require('os');
const githubToken = process.env.GITHUB_TOKEN;
const octokit = githubToken && getOctokit(githubToken);

const OWNER = 'immutable';
const REPO = 'zkevm-bridge-contracts';
const REPORT_TITLE = `# 📃CI Report${EOL}`;
const pr = context.payload.number;

const getExistingReportComment = async () => getExistingComment(REPORT_TITLE);

const getExistingComment = async (head) => {
const { data: comments } = await octokit.rest.issues.listComments({
owner: OWNER,
repo: REPO,
issue_number: pr,
});
return comments.find(comment => comment.body.includes(head));
}

const createComment = (report) => {
return octokit.rest.issues.createComment({
owner: OWNER,
repo: REPO,
issue_number: pr,
body: report
});
}

const updateComment = (id, report) => {
return octokit.rest.issues.updateComment({
owner: OWNER,
repo: REPO,
comment_id: id,
body: report
});
}

exports.updateComment = async (target, report) => {
const comment = await getExistingReportComment();
if (comment) {
await updateComment(comment.id, REPORT_TITLE + report);
} else {
await createComment(REPORT_TITLE + report);
}
}
50 changes: 50 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Coverage Report

on:
pull_request:

env:
FOUNDRY_PROFILE: ci
GITHUB_TOKEN: ${{ secrets.PLATFORM_SA_GITHUB_TOKEN }}

jobs:
check:
strategy:
fail-fast: true

name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- name: Restore node modules from cache
uses: ./.github/actions/node-cache

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Run Forge build
run: |
forge --version
forge build --sizes
id: build

- name: Run Forge coverage
run: |
forge coverage --report summary > ./coverage.out
id: coverage

- name: Debug coverage report
run: |
cat ./coverage.out
- name: Publish report
uses: ./.github/actions/coverage
with:
coverage: ./coverage.out
target: coverage

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ broadcast/DeployRootContracts.s.sol/2500/
broadcast/DeployChildContracts.s.sol/2501/

node_modules/

# MacOS
.DS_Store
Loading

0 comments on commit 953fd85

Please sign in to comment.