Skip to content

Commit

Permalink
Merge pull request #7 from AthleticNet/clarify-usage-and-document-cha…
Browse files Browse the repository at this point in the history
…nges-from-vhoyer

Update README to clarify-usage-and-document-changes-from-vhoyer
  • Loading branch information
jacob-8 authored Apr 22, 2021
2 parents e9be94b + 56e05d2 commit 9751424
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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.
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
59 changes: 52 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -256,26 +260,67 @@ 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 = `<!--json:${JSON.stringify(originMeta)}-->
|${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));
core.setFailed(error.message);
}
}

async function deletePreviousComments({ owner, repo, octokit, issueNumber }) {
const onlyPreviousCoverageComments = (comment) => {
const regexMarker = /^<!--json:{.*?}-->/;
const extractMetaFromMarker = (body) => JSON.parse(body.replace(/^<!--json:|-->(.|\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:
Expand Down Expand Up @@ -5992,4 +6037,4 @@ exports.checkBypass = checkBypass;

/***/ })

/******/ });
/******/ });

0 comments on commit 9751424

Please sign in to comment.