diff --git a/README.md b/README.md index 6fee2ab..2519a4f 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,8 @@ permissions: pull-requests: write # To comment on pull requests ``` +The report is also written to the [job summary](https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-commands#adding-a-job-summary) of the workflow run, which doesn't need any extra permissions. + ## Artifact Mode By default, the action posts a comment directly to the pull request. This requires `pull-requests: write` permission in the workflow that runs the analysis, which typically means using `pull_request_target` for fork PRs. diff --git a/build/main.js b/build/main.js index bbec6f0..73c377e 100644 --- a/build/main.js +++ b/build/main.js @@ -19836,6 +19836,7 @@ var Summary = class { } }; var _summary = new Summary(); +var summary = _summary; // node_modules/@actions/core/lib/platform.js import os3 from "os"; @@ -25130,6 +25131,13 @@ async function findExistingComment(octokit, prNumber) { } return void 0; } +async function writeJobSummary(report) { + try { + await summary.addRaw(report).write(); + } catch (err) { + info(`Failed to write job summary: ${err}`); + } +} async function upsertComment(octokit, prNumber, body) { const { owner, repo } = context2.repo; const existingCommentId = await findExistingComment(octokit, prNumber); @@ -25348,12 +25356,10 @@ async function analyzeAndComment() { ); scanForReplacements(messages, baseDependencies, currentDependencies); } - const commentBody = messages.length === 0 ? `${COMMENT_TAG} -## e18e dependency analysis - -No dependency warnings found. -` : `${COMMENT_TAG} -${messages.join("\n\n")}`; + const report = messages.length === 0 ? "## e18e dependency analysis\n\nNo dependency warnings found.\n" : messages.join("\n\n"); + const commentBody = `${COMMENT_TAG} +${report}`; + await writeJobSummary(report); if (mode === "artifact") { const tmpDir = join2(baseWorkspace, ".e18e-tmp"); const outputPath = join2(tmpDir, ARTIFACT_FILENAME); diff --git a/src/github.ts b/src/github.ts index 30b18cb..81635b3 100644 --- a/src/github.ts +++ b/src/github.ts @@ -34,6 +34,14 @@ export async function findExistingComment( return undefined; } +export async function writeJobSummary(report: string): Promise { + try { + await core.summary.addRaw(report).write(); + } catch (err) { + core.info(`Failed to write job summary: ${err}`); + } +} + export async function upsertComment( octokit: Octokit, prNumber: number, diff --git a/src/main.ts b/src/main.ts index bd2a6e7..66318ef 100644 --- a/src/main.ts +++ b/src/main.ts @@ -28,7 +28,7 @@ import {scanForLicenses} from './checks/license.js'; import {scanForBundleSize} from './checks/bundle-size.js'; import {formatBytes, parseCommaList} from './common.js'; import {ARTIFACT_FILENAME, COMMENT_TAG} from './constants.js'; -import {findExistingComment, upsertComment} from './github.js'; +import {findExistingComment, upsertComment, writeJobSummary} from './github.js'; interface ArtifactResult { pr_number: number; @@ -266,10 +266,13 @@ async function analyzeAndComment(): Promise { scanForReplacements(messages, baseDependencies, currentDependencies); } - const commentBody = + const report = messages.length === 0 - ? `${COMMENT_TAG}\n## e18e dependency analysis\n\nNo dependency warnings found.\n` - : `${COMMENT_TAG}\n${messages.join('\n\n')}`; + ? '## e18e dependency analysis\n\nNo dependency warnings found.\n' + : messages.join('\n\n'); + const commentBody = `${COMMENT_TAG}\n${report}`; + + await writeJobSummary(report); if (mode === 'artifact') { const tmpDir = join(baseWorkspace, '.e18e-tmp'); diff --git a/test/github_test.ts b/test/github_test.ts new file mode 100644 index 0000000..6385246 --- /dev/null +++ b/test/github_test.ts @@ -0,0 +1,39 @@ +import {describe, it, expect, beforeEach, afterEach} from 'vitest'; +import * as fs from 'node:fs/promises'; +import * as os from 'node:os'; +import * as path from 'node:path'; +import * as process from 'process'; +import {writeJobSummary} from '../src/github.js'; +import {coreLogs, clearCoreLogs} from './util.js'; + +describe('writeJobSummary', () => { + let tmpDir: string; + + beforeEach(async () => { + tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'job-summary-')); + }); + + afterEach(async () => { + delete process.env['GITHUB_STEP_SUMMARY']; + clearCoreLogs(); + await fs.rm(tmpDir, {recursive: true, force: true}); + }); + + it('should write the report to the job summary', async () => { + const summaryPath = path.join(tmpDir, 'summary.md'); + await fs.writeFile(summaryPath, ''); + process.env['GITHUB_STEP_SUMMARY'] = summaryPath; + + await writeJobSummary('## e18e dependency analysis\n\nsome report'); + + const content = await fs.readFile(summaryPath, 'utf-8'); + expect(content).toContain('## e18e dependency analysis\n\nsome report'); + }); + + it('should not throw if the job summary cannot be written', async () => { + process.env['GITHUB_STEP_SUMMARY'] = path.join(tmpDir, 'missing.md'); + + await expect(writeJobSummary('report')).resolves.toBeUndefined(); + expect(coreLogs.info.join('\n')).toContain('Failed to write job summary'); + }); +});