Repository navigation
feat(python-cli-parity): compare pull request scans with base scans #1766
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
jdalton/surf-2240-ci-pr-context-stack
Choose a base branch
from
jdalton/surf-2230-pr-baselines
base: jdalton/surf-2240-ci-pr-context-stack
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.
+269
−12
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
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,57 @@ | ||
| import { fetchOrgFullScanList } from '../scan/fetch-list-scans.mts' | ||
|
|
||
| import type { CResult } from '../../types.mts' | ||
| import type { FullScanItem } from '@socketsecurity/sdk' | ||
|
|
||
| export type SelectBaselineScanConfig = { | ||
| branch: string | ||
| commitHash: string | ||
| orgSlug: string | ||
| repo: string | ||
| } | ||
|
|
||
| export function findBaselineScan( | ||
| scans: FullScanItem[], | ||
| commitHash: string, | ||
| ): FullScanItem | undefined { | ||
| return scans.find(scan => scan.commit_hash === commitHash) | ||
| } | ||
|
|
||
| export async function selectBaselineScan({ | ||
| branch, | ||
| commitHash, | ||
| orgSlug, | ||
| repo, | ||
| }: SelectBaselineScanConfig): Promise<CResult<FullScanItem>> { | ||
| if (!commitHash) { | ||
| return { | ||
| ok: false, | ||
| message: 'Cannot select PR scan baseline', | ||
| cause: 'The CI checkout does not provide a commit SHA.', | ||
| } | ||
| } | ||
|
|
||
| const scansCResult = await fetchOrgFullScanList({ | ||
| branch, | ||
| commitHash, | ||
| direction: 'desc', | ||
| from_time: '', | ||
| orgSlug, | ||
| page: 1, | ||
| perPage: 1, | ||
| repo, | ||
| sort: 'created_at', | ||
| }) | ||
| if (!scansCResult.ok) { | ||
| return scansCResult | ||
| } | ||
|
|
||
| const scan = findBaselineScan(scansCResult.data.results, commitHash) | ||
| return scan | ||
| ? { ok: true, data: scan } | ||
| : { | ||
| ok: false, | ||
| message: 'No matching PR scan baseline found', | ||
| cause: `No scan exists for commit ${commitHash} on ${repo}/${branch}. Run a scan for the base commit first.`, | ||
| } | ||
| } | ||
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,96 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { | ||
| findBaselineScan, | ||
| selectBaselineScan, | ||
| } from './select-baseline-scan.mts' | ||
| import { fetchOrgFullScanList } from '../scan/fetch-list-scans.mts' | ||
|
|
||
| vi.mock('../scan/fetch-list-scans.mts', () => ({ | ||
| fetchOrgFullScanList: vi.fn(), | ||
| })) | ||
|
|
||
| const scan = { | ||
| api_url: null, | ||
| created_at: '2026-01-01T00:00:00Z', | ||
| html_report_url: 'https://socket.dev/report', | ||
| id: 'scan-base', | ||
| integration_repo_url: null, | ||
| integration_type: null, | ||
| organization_id: 'org-id', | ||
| organization_slug: 'test-org', | ||
| repo: 'test-repo', | ||
| repository_id: 'repo-id', | ||
| repository_slug: 'test-repo', | ||
| updated_at: '2026-01-01T00:00:00Z', | ||
| commit_hash: 'base-sha', | ||
| branch: 'main', | ||
| } | ||
|
|
||
| describe('selectBaselineScan', () => { | ||
| beforeEach(() => { | ||
| vi.mocked(fetchOrgFullScanList).mockReset() | ||
| }) | ||
|
|
||
| it('selects the exact commit when the scan list includes nearby scans', () => { | ||
| expect( | ||
| findBaselineScan( | ||
| [{ ...scan, id: 'newer', commit_hash: 'newer-sha' }, scan], | ||
| 'base-sha', | ||
| ), | ||
| ).toEqual(scan) | ||
| }) | ||
|
|
||
| it('selects an exact commit scan from the matching repo and branch query', async () => { | ||
| vi.mocked(fetchOrgFullScanList).mockResolvedValue({ | ||
| ok: true, | ||
| data: { results: [scan] }, | ||
| }) | ||
| await expect( | ||
| selectBaselineScan({ | ||
| branch: 'main', | ||
| commitHash: 'base-sha', | ||
| orgSlug: 'test-org', | ||
| repo: 'test-repo', | ||
| }), | ||
| ).resolves.toEqual({ ok: true, data: scan }) | ||
| expect(fetchOrgFullScanList).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| branch: 'main', | ||
| commitHash: 'base-sha', | ||
| perPage: 1, | ||
| repo: 'test-repo', | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| it('fails without a commit SHA', async () => { | ||
| await expect( | ||
| selectBaselineScan({ | ||
| branch: 'main', | ||
| commitHash: '', | ||
| orgSlug: 'test-org', | ||
| repo: 'test-repo', | ||
| }), | ||
| ).resolves.toMatchObject({ ok: false }) | ||
| expect(fetchOrgFullScanList).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('fails closed when the exact commit has no scan', async () => { | ||
| vi.mocked(fetchOrgFullScanList).mockResolvedValue({ | ||
| ok: true, | ||
| data: { results: [scan] }, | ||
| }) | ||
| await expect( | ||
| selectBaselineScan({ | ||
| branch: 'main', | ||
| commitHash: 'unknown-sha', | ||
| orgSlug: 'test-org', | ||
| repo: 'test-repo', | ||
| }), | ||
| ).resolves.toMatchObject({ | ||
| ok: false, | ||
| message: 'No matching PR scan baseline found', | ||
| }) | ||
| }) | ||
| }) |
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
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,44 @@ | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { outputDiffScan } from './output-diff-scan.mts' | ||
|
|
||
| vi.mock('../../utils/logger.mts', () => ({ | ||
| logger: { error: vi.fn(), fail: vi.fn(), info: vi.fn(), log: vi.fn() }, | ||
| })) | ||
|
|
||
| const diff = { | ||
| after: { id: 'head' }, | ||
| artifacts: { | ||
| added: [ | ||
| { | ||
| type: 'npm', | ||
| name: 'new-package', | ||
| version: '1.0.0', | ||
| alerts: [{ action: 'error', type: 'test-alert', key: 'alert-1' }], | ||
| }, | ||
| ], | ||
| removed: [], | ||
| replaced: [], | ||
| unchanged: [], | ||
| updated: [], | ||
| }, | ||
| before: { id: 'base' }, | ||
| directDependenciesChanged: false, | ||
| diff_report_url: '', | ||
| } | ||
|
|
||
| describe('outputDiffScan', () => { | ||
| afterEach(() => { | ||
| process.exitCode = 0 | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('fails for new blocking findings in a PR diff', async () => { | ||
| await outputDiffScan({ ok: true, data: diff } as never, { | ||
| depth: 0, | ||
| file: '', | ||
| outputKind: 'json', | ||
| }) | ||
| expect(process.exitCode).toBe(1) | ||
| }) | ||
| }) |
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.