Repository navigation
feat(python-cli-parity): post and update GitHub scan comments #1769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
John-David Dalton (jdalton)
wants to merge
1
commit into
v1.x
Choose a base branch
from
jdalton/surf-2236-github-pr-comments
base: v1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+239
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') | ||
| } | ||
|
|
||
| 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') | ||
| } | ||
|
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' | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.