This action extends actions/github-script and makes it easy to quickly write
a script in your workflow that uses the GitHub API and the workflow run context
then posts comment to Pull Request and GitHub Summary.
To use this action, provide an input named script that contains the body of an asynchronous JavaScript function call.
The following arguments will be provided:
githubA pre-authenticated octokit/rest.js client with pagination pluginscontextAn object containing the context of the workflow runcoreA reference to the @actions/core packageglobA reference to the @actions/glob packageioA reference to the @actions/io packageexecA reference to the @actions/exec packagerequireA proxy wrapper around the normal Node.jsrequireto enable requiring relative paths (relative to the current working directory) and requiring npm packages installed in the current working directory. If for some reason you need the non-wrappedrequire, there is an escape hatch available:__original_require__is the original value ofrequirewithout our wrapping applied.
Since the script is just a function body, these values will already be
defined, so you don't have to import them (see examples below). You script must
return a string that will be included in comment on Pull Request and GitHub
Summary.
See actions/github-script for documentation.
| Input | Required | Description |
|---|---|---|
| script | Yes | JavaScript code to execute. |
| label | No | Label to identify the type of comment (e.g. "lint"). |
WARNING: Actions expressions are evaluated before the script is passed to the action, so the result of any expressions will be evaluated as JavaScript code.
It's highly recommended to not evaluate expressions directly in the script to avoid
script injections
and potential SyntaxErrors when the expression is not valid JavaScript code (particularly when it comes to improperly escaped strings).
This examples will retrieve coverage/coverage-summary.json results from prior task and pretty print test coverage results to GitHub pull request (if exists) and GitHub Summary. The label is used to provide logging context.
- name: Add Test Coverage Comment on PR and GitHub Summary
uses: aguilita1/github-script-post-comment@v1
env:
JEST_THRESHOLD_LINES: 75
with:
label: "Test Coverage"
script: |
const fs = require('fs');
const thresholdLines = parseInt(process.env.JEST_THRESHOLD_LINES, 10);
if (isNaN(thresholdLines)) {
throw new Error('JEST_THRESHOLD_LINES environment variable is not set or invalid');
}
const coverage = JSON.parse(fs.readFileSync('coverage/coverage-summary.json'));
const pct = Math.round(coverage.total.lines.pct);
const status = pct >= thresholdLines ? '✅' : '❌';
let body = `## Test Coverage Report ${status}\n\n`
+ `**Coverage Found:** ${pct}%\n`
+ `**Expected:** ${thresholdLines}%\n\n`
+ `- Lines: ${coverage.total.lines.pct}%\n`
+ `- Functions: ${coverage.total.functions.pct}%\n`
+ `- Branches: ${coverage.total.branches.pct}%\n`
+ `- Statements: ${coverage.total.statements.pct}%`;
return body;