From d46a362e5ab73d85699452b0d679c6859b1b69a0 Mon Sep 17 00:00:00 2001 From: jwrunner <7559478+jwrunner@users.noreply.github.com> Date: Thu, 22 Apr 2021 07:18:38 -0700 Subject: [PATCH 1/3] Update README.md --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fc3afc7..7540acf 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# Comment Test Coverage +# Comment Test Coverage from a json-summary file -A GitHub action to comment on a PR on GitHub with a simple test coverage summary. +A GitHub action to comment on a PR on GitHub with a simple test coverage summary table that edits itself on successive pushes to the same PR. -## Usage with Karma + Angular +## How to use with Karma + Angular 1. Add `"codeCoverage": true,` under test > options in angular.json 2. In your karma.conf.js set coverageIstanbulReporter.reports to include `json-summary` and save it to the /coverage directory if using the sample setup below 3. Use in your workflow as illustrated below: @@ -28,7 +28,7 @@ jobs: title: Karma Test Coverage ``` -## Usage with Jest +## How to use with Jest 1. Add `"codeCoverage": true,` under test > options in angular.json 2. In your jest.config.js set coverageReporters to include `json-summary` and set coverageDirectory to 'coverage' if using the path in the sample setup above. 3. Use in your workflow as illustrated above in the Karma example. @@ -39,7 +39,8 @@ jobs: - `path` (**required**) - Path to your coverage-summary.json file - `title` (**optional**) - Title of comment in PR (defaults to "Test Coverage") -## How to edit action +## How to edit the action +Feel free to submit a PR to this repo and ask me to update the action, but if you'd like to create your own action: 1. Clone down repo, `npm install`, and make changes 2. Run `npm run package` 3. Commit changes From 1ce6e07d458833595b269726cf49edee31a681f9 Mon Sep 17 00:00:00 2001 From: jwrunner <7559478+jwrunner@users.noreply.github.com> Date: Thu, 22 Apr 2021 07:30:38 -0700 Subject: [PATCH 2/3] compile changes --- dist/index.js | 59 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/dist/index.js b/dist/index.js index e9e51c5..9e830dd 100644 --- a/dist/index.js +++ b/dist/index.js @@ -230,6 +230,10 @@ const core = __webpack_require__(470); const github = __webpack_require__(469); const fs = __webpack_require__(747); +const originMeta = { + commentFrom: 'Comment Test Coverage as table', +} + async function run() { try { const inputs = { @@ -256,17 +260,27 @@ async function run() { const data = fs.readFileSync(`${process.env.GITHUB_WORKSPACE}/${inputs.path}`, 'utf8'); const json = JSON.parse(data); - const coverage = `==== **${inputs.title}** ==== -Statements: ${json.total.statements.pct}% ( ${json.total.statements.covered}/${json.total.statements.total} ) -Branches : ${json.total.branches.pct}% ( ${json.total.branches.covered} /${json.total.branches.total} ) -Functions : ${json.total.functions.pct}% ( ${json.total.functions.covered} /${json.total.functions.total} ) -Lines : ${json.total.lines.pct}% ( ${json.total.lines.covered} /${json.total.lines.total} )` + const coverage = ` +|${inputs.title}| % | values | +|---------------|:---------------------------:|:-------------------------------------------------------------------:| +|Statements |${json.total.statements.pct}%|( ${json.total.statements.covered} / ${json.total.statements.total} )| +|Branches |${json.total.branches.pct}% |( ${json.total.branches.covered} / ${json.total.branches.total} ) | +|Functions |${json.total.functions.pct}% |( ${json.total.functions.covered} / ${json.total.functions.total} ) | +|Lines |${json.total.lines.pct}% |( ${json.total.lines.covered} / ${json.total.lines.total} ) | +`; + + await deletePreviousComments({ + issueNumber, + octokit, + owner, + repo, + }); await octokit.issues.createComment({ owner, repo, issue_number: issueNumber, - body: eval('`' + coverage + '`') + body: coverage, }); } catch (error) { core.debug(inspect(error)); @@ -274,8 +288,39 @@ Lines : ${json.total.lines.pct}% ( ${json.total.lines.covered} /${j } } +async function deletePreviousComments({ owner, repo, octokit, issueNumber }) { + const onlyPreviousCoverageComments = (comment) => { + const regexMarker = /^/; + const extractMetaFromMarker = (body) => JSON.parse(body.replace(/^(.|\n|\r)*$/g, '')); + + if (comment.user.type !== 'Bot') return false; + if (!regexMarker.test(comment.body)) return false; + + const meta = extractMetaFromMarker(comment.body); + + return meta.commentFrom === originMeta.commentFrom; + } + + const asyncDeleteComment = (comment) => { + return octokit.issues.deleteComment({ owner, repo, comment_id: comment.id }); + } + + const commentList = await octokit.issues.listComments({ + owner, + repo, + issue_number: issueNumber, + }).then(response => response.data); + + await Promise.all( + commentList + .filter(onlyPreviousCoverageComments) + .map(asyncDeleteComment) + ); +} + run(); + /***/ }), /***/ 127: @@ -5992,4 +6037,4 @@ exports.checkBypass = checkBypass; /***/ }) -/******/ }); \ No newline at end of file +/******/ }); From 56e05d2d2af1770c3015e58373a165e43cae008c Mon Sep 17 00:00:00 2001 From: jwrunner <7559478+jwrunner@users.noreply.github.com> Date: Thu, 22 Apr 2021 07:31:19 -0700 Subject: [PATCH 3/3] update action description --- action.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 558b4b1..9fbd244 100644 --- a/action.yml +++ b/action.yml @@ -1,11 +1,11 @@ name: 'Comment Test Coverage' -description: 'Read a Test Coverage coverage-summary.json test report and comment stats on to PR' +description: 'Read a Test Coverage json-summary test report and add stats on to PR using a table that rewrites itself on successive pushes' inputs: token: description: 'The GitHub authentication token' required: true path: - description: 'Filepath to coverage-summary.json' + description: 'Filepath to json-summary file' required: true title: description: 'Comment title in PR conversation'