diff --git a/.github/utils/github.js b/.github/utils/github.js new file mode 100644 index 000000000..b3b61670b --- /dev/null +++ b/.github/utils/github.js @@ -0,0 +1,54 @@ +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) { + const targetRegex = new RegExp(`(\\
${target}:)([\\s\\S]+?)(table\\>\\n<\\/details\\>)`); + let newComment; + if (targetRegex.test(comment.body)) { + newComment = comment.body.replace(targetRegex, report); + } else { + newComment = comment.body + report; + } + await updateComment(comment.id, newComment); + } else { + await createComment(REPORT_TITLE + report); + } +} \ No newline at end of file diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 000000000..0ccb669c4 --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,44 @@ +name: Coverage Report + +on: [push] + +env: + FOUNDRY_PROFILE: ci + +jobs: + check: + strategy: + fail-fast: true + + name: Foundry project + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + submodules: recursive + + - 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: | + echo "::set-output name=coverage::$(forge coverage --report summary > coverage.out)" + id: coverage + + - name: Publish report + uses: actions/github-script@v6 + with: + script: | + const { updateComment } = require("../utils/github"); + + const report = + await updateComment(${{ steps.coverage.outputs.coverage }}); + diff --git a/.gitignore b/.gitignore index 3ab99ec09..bd2073f29 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ broadcast/DeployRootContracts.s.sol/2500/ broadcast/DeployChildContracts.s.sol/2501/ node_modules/ + +# MacOS +.DS_Store