Skip to content

Commit b1bc5fe

Browse files
committed
feat: add comment-body output
1 parent f384866 commit b1bc5fe

5 files changed

Lines changed: 66 additions & 2 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Test comment-body
2+
3+
on:
4+
pull_request:
5+
push:
6+
7+
permissions:
8+
contents: read
9+
issues: write
10+
pull-requests: write
11+
12+
jobs:
13+
test:
14+
name: job
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v5
18+
with:
19+
fetch-depth: 0
20+
21+
- id: compressed-size
22+
uses: ./
23+
with:
24+
repo-token: ${{ secrets.GITHUB_TOKEN }}
25+
base-ref: master
26+
pattern: index.js
27+
comment-key: output-smoke
28+
29+
- name: Print comment-body output
30+
env:
31+
COMMENT_BODY: ${{ steps.compressed-size.outputs.comment-body }}
32+
run: |
33+
if [ -z "$COMMENT_BODY" ]; then
34+
echo "comment-body output is empty" >&2
35+
exit 1
36+
fi
37+
38+
printf '%s\n' "$COMMENT_BODY"

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ inputs:
5050
description: 'The column and direction to sort the results by. The format is "column:direction", where column is one of "Filename", "Size", or "Change" and direction is "asc" or "desc". For example, "Size:desc" sorts the table by file size in descending order.'
5151
default: 'Filename:asc'
5252

53+
outputs:
54+
comment-body:
55+
description: 'The full Markdown body the action posts to the PR, including the size report and footer.'
56+
5357
runs:
5458
using: 'node24'
5559
main: 'index.js'

index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getInput, setFailed, startGroup, endGroup, debug } from '@actions/core'
22
import { context, getOctokit } from '@actions/github';
33
import { exec } from '@actions/exec';
44
import { SizePlugin } from '@rschristian/size-plugin';
5-
import { getPackageManagerAndInstallScript, diffTable, toBool, stripHash, getSortOrder } from './utils.js';
5+
import { getPackageManagerAndInstallScript, diffTable, toBool, stripHash, getSortOrder, setOutput } from './utils.js';
66

77
/**
88
* @typedef {ReturnType<typeof import("@actions/github").getOctokit>} Octokit
@@ -170,6 +170,8 @@ async function run(octokit, context, token) {
170170
`\n\n<a href="https://github.com/preactjs/compressed-size-action"><sub>compressed-size-action${commentKey ? `::${commentKey}` : ''}</sub></a>`
171171
};
172172

173+
setOutput('comment-body', comment.body);
174+
173175
if (context.eventName !== 'pull_request' && context.eventName !== 'pull_request_target') {
174176
console.log('No PR associated with this action run. Not posting a check or comment.');
175177
outputRawMarkdown = false;

src/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'fs';
22
import path from 'path';
3+
import { EOL } from 'os';
34
import prettyBytes from 'pretty-bytes';
45

56
/**
@@ -269,3 +270,22 @@ export function getSortOrder(sortBy) {
269270
console.warn(`Invalid 'order-by' value '${sortBy}', defaulting to 'Filename:asc'`);
270271
return 'Filename:asc';
271272
}
273+
274+
/**
275+
*
276+
* @param {string} name
277+
* @param {string} value
278+
*/
279+
export function setOutput(name, value) {
280+
const outputPath = process.env.GITHUB_OUTPUT;
281+
282+
if (outputPath) {
283+
const delimiter = `ghadelimiter_${Date.now()}`;
284+
fs.appendFileSync(outputPath, `${name}<<${delimiter}${EOL}${value}${EOL}${delimiter}${EOL}`);
285+
return;
286+
}
287+
288+
console.log(
289+
`::set-output name=${name}::${String(value).replace(/\n/g, '%0A').replace(/\r/g, '%0D')}`
290+
);
291+
}

0 commit comments

Comments
 (0)