Skip to content
Open
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
29 changes: 15 additions & 14 deletions build/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -25228,14 +25228,16 @@ 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,
issue_number: prNumber,
body
});
info("Created new dependency diff comment");
} else {
info("No dependency warnings found. Skipping comment creation.");
}
}

Expand All @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export async function writeJobSummary(report: string): Promise<void> {
export async function upsertComment(
octokit: Octokit,
prNumber: number,
body: string
body: string,
{createIfMissing = true}: {createIfMissing?: boolean} = {}
): Promise<void> {
const {owner, repo} = github.context.repo;
const existingCommentId = await findExistingComment(octokit, prNumber);
Expand All @@ -58,13 +59,15 @@ export async function upsertComment(
body
});
core.info(`Updated existing dependency diff comment #${existingCommentId}`);
} else {
} else if (createIfMissing) {
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: prNumber,
body
});
core.info('Created new dependency diff comment');
} else {
core.info('No dependency warnings found. Skipping comment creation.');
}
}
29 changes: 14 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand All @@ -55,7 +57,9 @@ async function postCommentFromArtifact(): Promise<void> {
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<void> {
Expand Down Expand Up @@ -287,7 +291,11 @@ async function analyzeAndComment(): Promise<void> {
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);
Expand All @@ -296,18 +304,9 @@ async function analyzeAndComment(): Promise<void> {
}

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<void> {
Expand Down
71 changes: 69 additions & 2 deletions test/github_test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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<typeof github.getOctokit>;
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');
});
});