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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
97 changes: 97 additions & 0 deletions src/commands/ci/publish-pr-comments.mts
Original file line number Diff line number Diff line change
@@ -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 =
'<!-- socket-overview-comment-actions -->'
export const PR_SECURITY_COMMENT_MARKER =
'<!-- socket-security-comment-actions -->'

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<void> {
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')
}
Comment thread
jdalton marked this conversation as resolved.
Resolved

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')
}
Comment thread
jdalton marked this conversation as resolved.
Resolved

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'
)
}
102 changes: 102 additions & 0 deletions src/commands/ci/publish-pr-comments.test.mts
Original file line number Diff line number Diff line change
@@ -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()
})
})
38 changes: 38 additions & 0 deletions src/utils/github.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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,
Expand Down
Loading