|
| 1 | +# aguilita1/github-script-post-comment |
| 2 | + |
| 3 | +This action extends `actions/github-script` and makes it easy to quickly write |
| 4 | +a script in your workflow that uses the GitHub API and the workflow run context |
| 5 | +then posts comment to Pull Request and GitHub Summary. |
| 6 | + |
| 7 | +### This action |
| 8 | + |
| 9 | +To use this action, provide an input named `script` that contains the body of an asynchronous JavaScript function call. |
| 10 | +The following arguments will be provided: |
| 11 | + |
| 12 | +- `github` A pre-authenticated |
| 13 | + [octokit/rest.js](https://octokit.github.io/rest.js) client with pagination plugins |
| 14 | +- `context` An object containing the [context of the workflow |
| 15 | + run](https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts) |
| 16 | +- `core` A reference to the [@actions/core](https://github.com/actions/toolkit/tree/main/packages/core) package |
| 17 | +- `glob` A reference to the [@actions/glob](https://github.com/actions/toolkit/tree/main/packages/glob) package |
| 18 | +- `io` A reference to the [@actions/io](https://github.com/actions/toolkit/tree/main/packages/io) package |
| 19 | +- `exec` A reference to the [@actions/exec](https://github.com/actions/toolkit/tree/main/packages/exec) package |
| 20 | +- `require` A proxy wrapper around the normal Node.js `require` to enable |
| 21 | + requiring relative paths (relative to the current working directory) and |
| 22 | + requiring npm packages installed in the current working directory. If for |
| 23 | + some reason you need the non-wrapped `require`, there is an escape hatch |
| 24 | + available: `__original_require__` is the original value of `require` without |
| 25 | + our wrapping applied. |
| 26 | + |
| 27 | +Since the `script` is just a function body, these values will already be |
| 28 | +defined, so you don't have to import them (see examples below). You script must |
| 29 | +return a `string` that will be included in comment on Pull Request and GitHub |
| 30 | +Summary. |
| 31 | + |
| 32 | +See [actions/github-script](https://github.com/marketplace/actions/github-script) for |
| 33 | +documentation. |
| 34 | + |
| 35 | +## Inputs |
| 36 | + |
| 37 | +| Input | Required | Description | |
| 38 | +|--------|----------|-------------------------------------------------------| |
| 39 | +| script | Yes | JavaScript code to execute. | |
| 40 | +| label | No | Label to identify the type of comment (e.g. "lint"). | |
| 41 | + |
| 42 | +*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*. |
| 43 | + |
| 44 | +It's highly recommended to *not* evaluate expressions directly in the `script` to avoid |
| 45 | +[script injections](https://docs.github.com/actions/security-for-github-actions/security-guides/security-hardening-for-github-actions#understanding-the-risk-of-script-injections) |
| 46 | +and potential `SyntaxError`s when the expression is not valid JavaScript code (particularly when it comes to improperly escaped strings). |
| 47 | + |
| 48 | +## Usage example |
| 49 | + |
| 50 | +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. |
| 51 | + |
| 52 | +```yaml |
| 53 | +- name: Add Test Coverage Comment on PR and GitHub Summary |
| 54 | + uses: aguilita1/github-script-post-comment |
| 55 | + env: |
| 56 | + JEST_THRESHOLD_LINES: 75 |
| 57 | + with: |
| 58 | + label: "Test Coverage" |
| 59 | + script: | |
| 60 | + const fs = require('fs'); |
| 61 | + const thresholdLines = parseInt(process.env.JEST_THRESHOLD_LINES, 10); |
| 62 | + if (isNaN(thresholdLines)) { |
| 63 | + throw new Error('JEST_THRESHOLD_LINES environment variable is not set or invalid'); |
| 64 | + } |
| 65 | +
|
| 66 | + const coverage = JSON.parse(fs.readFileSync('coverage/coverage-summary.json')); |
| 67 | + const pct = Math.round(coverage.total.lines.pct); |
| 68 | + const status = pct >= thresholdLines ? '✅' : '❌'; |
| 69 | +
|
| 70 | + let body = `## Test Coverage Report ${status}\n\n` |
| 71 | + + `**Coverage Found:** ${pct}%\n` |
| 72 | + + `**Expected:** ${thresholdLines}%\n\n` |
| 73 | + + `- Lines: ${coverage.total.lines.pct}%\n` |
| 74 | + + `- Functions: ${coverage.total.functions.pct}%\n` |
| 75 | + + `- Branches: ${coverage.total.branches.pct}%\n` |
| 76 | + + `- Statements: ${coverage.total.statements.pct}%`; |
| 77 | +
|
| 78 | + return body; |
| 79 | +``` |
0 commit comments