From 7cad8f14e330ef648280a373aa24f0820c18a3cb Mon Sep 17 00:00:00 2001 From: jdalton Date: Wed, 25 Nov 2026 13:49:13 -0500 Subject: [PATCH] feat(python-cli-parity): upsert GitHub PR comments --- .gitignore | 2 + src/commands/ci/publish-pr-comments.mts | 97 ++++++++++++++++++ src/commands/ci/publish-pr-comments.test.mts | 102 +++++++++++++++++++ src/utils/github.mts | 38 +++++++ 4 files changed, 239 insertions(+) create mode 100644 src/commands/ci/publish-pr-comments.mts create mode 100644 src/commands/ci/publish-pr-comments.test.mts diff --git a/.gitignore b/.gitignore index 3d333045c..7657b4677 100644 --- a/.gitignore +++ b/.gitignore @@ -1163,6 +1163,8 @@ !/src/utils/github-errors.mts !/src/utils/github-errors.test.mts !/src/utils/github.mts +!/src/commands/ci/publish-pr-comments.mts +!/src/commands/ci/publish-pr-comments.test.mts !/src/utils/glob-content-dedup.test.mts !/src/utils/glob.mts !/src/utils/glob.test.mts diff --git a/src/commands/ci/publish-pr-comments.mts b/src/commands/ci/publish-pr-comments.mts new file mode 100644 index 000000000..66525e570 --- /dev/null +++ b/src/commands/ci/publish-pr-comments.mts @@ -0,0 +1,97 @@ +import { upsertPullRequestComment } from '../../utils/github.mts' +import type { ReportLeafNode, ScanReport } from '../scan/generate-report.mts' + +export const PR_OVERVIEW_COMMENT_MARKER = + '' +export const PR_SECURITY_COMMENT_MARKER = + '' + +export async function publishPullRequestComments({ + disableOverview, + disableSecurity, + overviewBody, + owner, + pullRequest, + repo, + securityBody, +}: { + disableOverview: boolean + disableSecurity: boolean + overviewBody: string + owner: string + pullRequest: number + repo: string + securityBody: string +}): Promise { + if (!disableOverview && overviewBody.trim()) { + await upsertPullRequestComment({ + body: `${PR_OVERVIEW_COMMENT_MARKER}\n${overviewBody}`, + marker: PR_OVERVIEW_COMMENT_MARKER, + owner, + pullRequest, + repo, + }) + } + + if (!disableSecurity && securityBody.trim()) { + await upsertPullRequestComment({ + body: `${PR_SECURITY_COMMENT_MARKER}\n${securityBody}`, + marker: PR_SECURITY_COMMENT_MARKER, + owner, + pullRequest, + repo, + }) + } +} + +export function renderPullRequestOverview(report: ScanReport): string { + const alertCount = collectPullRequestAlertRows(report).length + return [ + '# Socket dependency overview', + '', + `Scan ${report.scanId} found ${alertCount} policy alert${alertCount === 1 ? '' : 's'}.`, + `Health status: ${report.healthy ? 'pass' : 'fail'}.`, + ].join('\n') +} + +export function renderPullRequestSecurity(report: ScanReport): string { + const rows = collectPullRequestAlertRows(report) + return [ + '# Socket security findings', + '', + rows.length > 0 ? rows.join('\n') : 'No policy findings remain.', + ].join('\n') +} + +export function collectPullRequestAlertRows(report: ScanReport): string[] { + const rows: string[] = [] + const pending: Array<{ node: unknown; path: string[] }> = [ + { node: report.alerts, path: [] }, + ] + while (pending.length > 0) { + const { node, path } = pending.pop()! + if (node instanceof Map) { + for (const [name, child] of node) { + pending.push({ node: child, path: [...path, String(name)] }) + } + continue + } + if (isReportLeafNode(node)) { + rows.push( + `- ${path.join(' / ')}: ${node.type} (${node.policy})${node.url ? ` ${node.url}` : ''}`, + ) + } + } + return rows +} + +function isReportLeafNode(node: unknown): node is ReportLeafNode { + return ( + typeof node === 'object' && + node !== null && + 'type' in node && + typeof node.type === 'string' && + 'policy' in node && + typeof node.policy === 'string' + ) +} diff --git a/src/commands/ci/publish-pr-comments.test.mts b/src/commands/ci/publish-pr-comments.test.mts new file mode 100644 index 000000000..48a25fea8 --- /dev/null +++ b/src/commands/ci/publish-pr-comments.test.mts @@ -0,0 +1,102 @@ +import { describe, expect, it, vi } from 'vitest' + +import { + PR_OVERVIEW_COMMENT_MARKER, + PR_SECURITY_COMMENT_MARKER, + publishPullRequestComments, + renderPullRequestOverview, + renderPullRequestSecurity, +} from './publish-pr-comments.mts' +import { upsertPullRequestComment } from '../../utils/github.mts' + +import type { ScanReport } from '../scan/generate-report.mts' + +vi.mock('../../utils/github.mts', () => ({ + upsertPullRequestComment: vi.fn(), +})) + +const config = { + disableOverview: false, + disableSecurity: false, + overviewBody: 'Dependency summary', + owner: 'SocketDev', + pullRequest: 42, + repo: 'socket-cli', + securityBody: 'Security findings', +} + +const report = { + alerts: new Map([ + [ + 'npm', + new Map([ + [ + 'left-pad', + new Map([ + [ + '1.0.0', + { + type: 'CVE-2026-1234', + policy: 'error', + manifest: ['package-lock.json'], + url: 'https://socket.dev/alert', + }, + ], + ]), + ], + ]), + ], + ]), + healthy: false, + options: { fold: 'version', reportLevel: 'error' }, + orgSlug: 'test-org', + scanId: 'scan-123', +} as unknown as ScanReport + +describe('publishPullRequestComments', () => { + it('renders alert leaves and counts findings rather than ecosystems', () => { + expect(renderPullRequestSecurity(report)).toContain( + 'npm / left-pad / 1.0.0: CVE-2026-1234 (error)', + ) + expect(renderPullRequestOverview(report)).toContain('found 1 policy alert.') + }) + + it('publishes separate marked overview and security comments', async () => { + await publishPullRequestComments(config) + expect(upsertPullRequestComment).toHaveBeenNthCalledWith(1, { + body: `${PR_OVERVIEW_COMMENT_MARKER}\nDependency summary`, + marker: PR_OVERVIEW_COMMENT_MARKER, + owner: 'SocketDev', + pullRequest: 42, + repo: 'socket-cli', + }) + expect(upsertPullRequestComment).toHaveBeenNthCalledWith(2, { + body: `${PR_SECURITY_COMMENT_MARKER}\nSecurity findings`, + marker: PR_SECURITY_COMMENT_MARKER, + owner: 'SocketDev', + pullRequest: 42, + repo: 'socket-cli', + }) + }) + + it('suppresses each comment type independently', async () => { + await publishPullRequestComments({ + ...config, + disableOverview: true, + overviewBody: '', + }) + expect(upsertPullRequestComment).toHaveBeenCalledTimes(1) + expect(upsertPullRequestComment).toHaveBeenCalledWith( + expect.objectContaining({ marker: PR_SECURITY_COMMENT_MARKER }), + ) + }) + + it('skips empty output when no prior comment can be located', async () => { + await publishPullRequestComments({ + ...config, + overviewBody: '', + securityBody: '', + }) + expect(upsertPullRequestComment).not.toHaveBeenCalled() + }) +}) diff --git a/src/utils/github.mts b/src/utils/github.mts index 1ecc5b7ae..024899bbc 100644 --- a/src/utils/github.mts +++ b/src/utils/github.mts @@ -266,6 +266,44 @@ export async function prExistForBranch( return false } +export async function upsertPullRequestComment({ + body, + marker, + owner, + pullRequest, + repo, +}: { + body: string + marker: string + owner: string + pullRequest: number + repo: string +}): Promise { + const octokit = getOctokit() + const comments = await octokit.paginate(octokit.issues.listComments, { + issue_number: pullRequest, + owner, + per_page: 100, + repo, + }) + const existing = comments.find(comment => comment.body?.includes(marker)) + if (existing) { + await octokit.issues.updateComment({ + body, + comment_id: existing.id, + owner, + repo, + }) + return + } + await octokit.issues.createComment({ + body, + issue_number: pullRequest, + owner, + repo, + }) +} + export async function setGitRemoteGithubRepoUrl( owner: string, repo: string,