From c751b9e6230b20cb81f76807a4bc3f70cace82c8 Mon Sep 17 00:00:00 2001 From: Maksim <11676775+maksimgm@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:17:52 -0800 Subject: [PATCH] fix: skip the comment in artifact mode when there are no warnings --- build/main.js | 29 +++++++++--------- src/github.ts | 7 +++-- src/main.ts | 29 +++++++++--------- test/github_test.ts | 71 +++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 103 insertions(+), 33 deletions(-) diff --git a/build/main.js b/build/main.js index 9c15a20..46d554d 100644 --- a/build/main.js +++ b/build/main.js @@ -25217,7 +25217,7 @@ async function writeJobSummary(report) { info(`Failed to write job summary: ${err}`); } } -async function upsertComment(octokit, prNumber, body) { +async function upsertComment(octokit, prNumber, body, { createIfMissing = true } = {}) { const { owner, repo } = context2.repo; const existingCommentId = await findExistingComment(octokit, prNumber); if (existingCommentId) { @@ -25228,7 +25228,7 @@ async function upsertComment(octokit, prNumber, body) { body }); info(`Updated existing dependency diff comment #${existingCommentId}`); - } else { + } else if (createIfMissing) { await octokit.rest.issues.createComment({ owner, repo, @@ -25236,6 +25236,8 @@ async function upsertComment(octokit, prNumber, body) { body }); info("Created new dependency diff comment"); + } else { + info("No dependency warnings found. Skipping comment creation."); } } @@ -25254,7 +25256,9 @@ async function postCommentFromArtifact() { ); info(`Posting comment to PR #${result.pr_number}`); const octokit = getOctokit(token); - await upsertComment(octokit, result.pr_number, result.body); + await upsertComment(octokit, result.pr_number, result.body, { + createIfMissing: result.has_warnings !== false + }); } async function analyzeAndComment() { const baseWorkspace = process2.env.GITHUB_WORKSPACE || process2.cwd(); @@ -25449,23 +25453,20 @@ ${report}`; await fs4.mkdir(tmpDir, { recursive: true }); await fs4.writeFile( outputPath, - JSON.stringify({ pr_number: prNumber, body: commentBody }) + JSON.stringify({ + pr_number: prNumber, + body: commentBody, + has_warnings: messages.length > 0 + }) ); setOutput("artifact-path", outputPath); info(`Wrote artifact to ${outputPath}`); return; } const octokit = getOctokit(token); - if (messages.length === 0) { - const existingCommentId = await findExistingComment(octokit, prNumber); - if (existingCommentId) { - await upsertComment(octokit, prNumber, commentBody); - } else { - info("No dependency warnings found. Skipping comment creation."); - } - return; - } - await upsertComment(octokit, prNumber, commentBody); + await upsertComment(octokit, prNumber, commentBody, { + createIfMissing: messages.length > 0 + }); } async function run() { try { diff --git a/src/github.ts b/src/github.ts index 81635b3..2793990 100644 --- a/src/github.ts +++ b/src/github.ts @@ -45,7 +45,8 @@ export async function writeJobSummary(report: string): Promise { export async function upsertComment( octokit: Octokit, prNumber: number, - body: string + body: string, + {createIfMissing = true}: {createIfMissing?: boolean} = {} ): Promise { const {owner, repo} = github.context.repo; const existingCommentId = await findExistingComment(octokit, prNumber); @@ -58,7 +59,7 @@ export async function upsertComment( body }); core.info(`Updated existing dependency diff comment #${existingCommentId}`); - } else { + } else if (createIfMissing) { await octokit.rest.issues.createComment({ owner, repo, @@ -66,5 +67,7 @@ export async function upsertComment( body }); core.info('Created new dependency diff comment'); + } else { + core.info('No dependency warnings found. Skipping comment creation.'); } } diff --git a/src/main.ts b/src/main.ts index e86a546..2b08200 100644 --- a/src/main.ts +++ b/src/main.ts @@ -29,11 +29,13 @@ import {scanForInstallScripts} from './checks/install-scripts.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, writeJobSummary} from './github.js'; +import {upsertComment, writeJobSummary} from './github.js'; interface ArtifactResult { pr_number: number; body: string; + // Missing in artifacts written by older versions, which always commented. + has_warnings?: boolean; } async function postCommentFromArtifact(): Promise { @@ -55,7 +57,9 @@ async function postCommentFromArtifact(): Promise { core.info(`Posting comment to PR #${result.pr_number}`); const octokit = github.getOctokit(token); - await upsertComment(octokit, result.pr_number, result.body); + await upsertComment(octokit, result.pr_number, result.body, { + createIfMissing: result.has_warnings !== false + }); } async function analyzeAndComment(): Promise { @@ -287,7 +291,11 @@ async function analyzeAndComment(): Promise { await fs.mkdir(tmpDir, {recursive: true}); await fs.writeFile( outputPath, - JSON.stringify({pr_number: prNumber, body: commentBody}) + JSON.stringify({ + pr_number: prNumber, + body: commentBody, + has_warnings: messages.length > 0 + } satisfies ArtifactResult) ); core.setOutput('artifact-path', outputPath); @@ -296,18 +304,9 @@ async function analyzeAndComment(): Promise { } const octokit = github.getOctokit(token); - - if (messages.length === 0) { - const existingCommentId = await findExistingComment(octokit, prNumber); - if (existingCommentId) { - await upsertComment(octokit, prNumber, commentBody); - } else { - core.info('No dependency warnings found. Skipping comment creation.'); - } - return; - } - - await upsertComment(octokit, prNumber, commentBody); + await upsertComment(octokit, prNumber, commentBody, { + createIfMissing: messages.length > 0 + }); } async function run(): Promise { diff --git a/test/github_test.ts b/test/github_test.ts index 6385246..71930a9 100644 --- a/test/github_test.ts +++ b/test/github_test.ts @@ -1,9 +1,11 @@ -import {describe, it, expect, beforeEach, afterEach} from 'vitest'; +import {describe, it, expect, beforeEach, afterEach, vi} 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 * as github from '@actions/github'; +import {upsertComment, writeJobSummary} from '../src/github.js'; +import {COMMENT_TAG} from '../src/constants.js'; import {coreLogs, clearCoreLogs} from './util.js'; describe('writeJobSummary', () => { @@ -37,3 +39,68 @@ describe('writeJobSummary', () => { expect(coreLogs.info.join('\n')).toContain('Failed to write job summary'); }); }); + +describe('upsertComment', () => { + function createOctokit(comments: Array<{id: number; body: string}>) { + const createComment = vi.fn(); + const updateComment = vi.fn(); + const listComments = vi.fn(); + const octokit = { + paginate: { + iterator: async function* () { + yield { + data: comments.map((c) => ({ + ...c, + user: {login: 'github-actions[bot]'} + })) + }; + } + }, + rest: {issues: {createComment, updateComment, listComments}} + } as unknown as ReturnType; + return {octokit, createComment, updateComment}; + } + + beforeEach(() => { + process.env['GITHUB_REPOSITORY'] = 'e18e/test-repo'; + }); + + afterEach(() => { + delete process.env['GITHUB_REPOSITORY']; + clearCoreLogs(); + }); + + it('should create a comment if none exists', async () => { + const {octokit, createComment, updateComment} = createOctokit([]); + + await upsertComment(octokit, 1, 'body'); + + expect(createComment).toHaveBeenCalledOnce(); + expect(updateComment).not.toHaveBeenCalled(); + }); + + it('should update an existing comment', async () => { + const {octokit, createComment, updateComment} = createOctokit([ + {id: 42, body: `${COMMENT_TAG}\nold`} + ]); + + await upsertComment(octokit, 1, 'body', {createIfMissing: false}); + + expect(updateComment).toHaveBeenCalledWith( + expect.objectContaining({comment_id: 42, body: 'body'}) + ); + expect(createComment).not.toHaveBeenCalled(); + }); + + it('should not create a comment when createIfMissing is false', async () => { + const {octokit, createComment, updateComment} = createOctokit([ + {id: 7, body: 'someone else'} + ]); + + await upsertComment(octokit, 1, 'body', {createIfMissing: false}); + + expect(createComment).not.toHaveBeenCalled(); + expect(updateComment).not.toHaveBeenCalled(); + expect(coreLogs.info.join('\n')).toContain('Skipping comment creation'); + }); +});