Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,39 +1,54 @@
// Creates or updates the benchmark comment on a PR. Used by actions/github-script
// from the `benchmark` workflow (PRs from this repository) and from the
// `benchmark comment` workflow (PRs from forks and dependabot).
// Creates or updates a report comment on a PR (one comment per marker, kept up
// to date). Used by actions/github-script from the `benchmark` workflow (PRs
// from this repository) and from the `benchmark comment` workflow (PRs from
// forks and dependabot).
const fs = require('node:fs');

const MARKER = '<!-- rregex-benchmark -->';
const MAX_BODY = 65536;

/**
* @param {object} options
* @param {import('@octokit/rest').Octokit} options.github
* @param {object} options.context
* @param {object} options.core
* @param {string} options.marker HTML comment that identifies the comment
* @param {string} options.title heading of the failure message
* @param {number} options.pr PR number
* @param {string} options.sha head commit of the PR
* @param {string} options.runUrl URL of the benchmark run
* @param {boolean} options.success whether the benchmark run succeeded
* @param {string} options.runUrl URL of the workflow run
* @param {boolean} options.success whether the job that wrote the report succeeded
* @param {string} [options.reportPath] Markdown report (required on success)
* @param {boolean} [options.create] false: only update an existing comment
*/
module.exports = async ({ github, context, core, pr, sha, runUrl, success, reportPath }) => {
module.exports = async ({
github,
context,
core,
marker,
title,
pr,
sha,
runUrl,
success,
reportPath,
create = true,
}) => {
const { owner, repo } = context.repo;
const short = sha.slice(0, 7);

let report = success
? fs.readFileSync(reportPath, 'utf8')
: [
'## ⏱️ Benchmark results',
`## ${title}`,
'',
'> [!CAUTION]',
`> The benchmark failed for \`${short}\`. See the [workflow run](${runUrl}).`,
`> The check failed for \`${short}\`. See the [workflow run](${runUrl}).`,
].join('\n');

const footer = `\n\n<sub>[Workflow run](${runUrl}) · updated for \`${short}\`</sub>`;
const limit = MAX_BODY - MARKER.length - footer.length - 1;
const limit = MAX_BODY - marker.length - footer.length - 1;
if (report.length > limit) report = report.slice(0, limit - 20) + '\n\n…(truncated)';
const body = `${MARKER}\n${report}${footer}`;
const body = `${marker}\n${report}${footer}`;

const comments = await github.paginate(github.rest.issues.listComments, {
owner,
Expand All @@ -42,14 +57,16 @@ module.exports = async ({ github, context, core, pr, sha, runUrl, success, repor
per_page: 100,
});
const existing = comments.find(
(c) => c.user?.login === 'github-actions[bot]' && c.body?.startsWith(MARKER),
(c) => c.user?.login === 'github-actions[bot]' && c.body?.startsWith(marker),
);

if (existing) {
await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body });
core.info(`Updated comment ${existing.html_url}`);
} else {
} else if (create) {
const { data } = await github.rest.issues.createComment({ owner, repo, issue_number: pr, body });
core.info(`Created comment ${data.html_url}`);
} else {
core.info('Nothing to report and no comment to update, skipping.');
}
};
52 changes: 40 additions & 12 deletions .github/workflows/benchmark-comment.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: benchmark comment

# Posts the report of the `benchmark` workflow on PRs from forks and dependabot,
# which get a read-only token in `pull_request` (PRs from this repository are
# handled by the `comment` job of `benchmark`). It runs from the default
# branch with a write token, so it never checks out or runs code from the PR:
# the artifact is only text, and the PR is resolved from the run metadata.
# Posts the reports of the `benchmark` workflow (benchmarks and public API) on
# PRs from forks and dependabot, which get a read-only token in `pull_request`
# (PRs from this repository are handled by the `comment` and `api_comment`
# jobs of `benchmark`). It runs from the default branch with a write token, so
# it never checks out or runs code from the PR: the artifacts are only text
# and JSON data, and the PR is resolved from the run metadata.

on:
workflow_run:
Expand Down Expand Up @@ -44,9 +45,22 @@ jobs:
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}

# the api job can succeed when a benchmark fails: no artifact when it failed
- uses: actions/download-artifact@v8
id: api
continue-on-error: true
with:
name: api
path: ${{ runner.temp }}/api
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}

- uses: actions/github-script@v9
env:
API: ${{ steps.api.outcome == 'success' }}
with:
script: |
const fs = require('node:fs');
const run = context.payload.workflow_run;
const { owner, repo } = context.repo;

Expand All @@ -67,13 +81,27 @@ jobs:
return;
}

await require('./.github/scripts/benchmark-comment.cjs')({
github,
context,
core,
pr,
sha: run.head_sha,
runUrl: run.html_url,
const comment = require('./.github/scripts/pr-comment.cjs');
const common = { github, context, core, pr, sha: run.head_sha, runUrl: run.html_url };

await comment({
...common,
marker: '<!-- rregex-benchmark -->',
title: '⏱️ Benchmark results',
success: run.conclusion === 'success',
reportPath: `${process.env.RUNNER_TEMP}/benchmark/report.md`,
});

// only a boolean is read from the JSON of the PR run
if (process.env.API === 'true') {
const dir = `${process.env.RUNNER_TEMP}/api`;
const { changed } = JSON.parse(fs.readFileSync(`${dir}/api.json`, 'utf8'));
await comment({
...common,
marker: '<!-- rregex-api -->',
title: '🧩 Public API changes',
success: true,
reportPath: `${dir}/report.md`,
create: changed === true,
});
}
Loading