Skip to content

Commit f0cccb6

Browse files
committed
feat(ci): select exact PR scan baselines
Refresh the native scan diff snapshot and allow the Windows Python shim test more time. Assisted-by: Codex:gpt-6-luna
1 parent 28cebf4 commit f0cccb6

14 files changed

Lines changed: 322 additions & 65 deletions

‎.github/workflows/ci.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,13 @@ jobs:
234234
- name: Install dependencies • Bash
235235
if: matrix.shell == 'bash'
236236
shell: bash
237-
run: pnpm install --loglevel error
237+
run: pnpm install --loglevel error ${{ runner.os == 'Windows' && '--config.enable-global-virtual-store=false' || '' }}
238238

239239
- &test-pwsh-dependencies
240240
name: Install dependencies • PowerShell
241241
if: matrix.shell == 'pwsh'
242242
shell: pwsh
243-
run: pnpm install --loglevel error
243+
run: pnpm install --loglevel error --config.enable-global-virtual-store=false
244244

245245
- name: Run full unit and integration suite • Bash
246246
if: matrix.suite == 'regular' && matrix.shell == 'bash'

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,9 @@
367367
!/src/commands/ci/fetch-default-org-slug.test.mts
368368
!/src/commands/ci/handle-ci.mts
369369
!/src/commands/ci/handle-ci.test.mts
370-
!/src/commands/ci/detect-ci-pull-request-context.mts
371370
!/src/commands/ci/detect-ci-pull-request-context.test.mts
371+
!/src/commands/ci/select-baseline-scan.mts
372+
!/src/commands/ci/select-baseline-scan.test.mts
372373
!/src/commands/cli.test.mts
373374
!/src/commands/config/cmd-config-auto.mts
374375
!/src/commands/config/cmd-config-auto.test.mts

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
"test:integration": "node scripts/test/integration.mts",
100100
"test:integration:ci": "pnpm run test:integration --use-built-dist --bail=1",
101101
"test:prepare": "dotenvx -q run -f .env.test -- pnpm run test:prepare:build",
102-
"test:prepare:build": "pnpm run build && del-cli 'test/**/node_modules'",
102+
"test:prepare:build": "pnpm run build:dist:src && del-cli 'test/**/node_modules'",
103103
"test:sea": "node scripts/test/native.mts",
104104
"test:unit": "dotenvx -q run -f .env.test -- vitest run --config .config/vitest.config.mts",
105105
"test:unit:ci": "pnpm run test:unit --bail=1",

‎src/commands/ci/handle-ci.mts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { detectCiPullRequestContext } from './detect-ci-pull-request-context.mts'
22
import { getDefaultOrgSlug } from './fetch-default-org-slug.mts'
3+
import { selectBaselineScan } from './select-baseline-scan.mts'
34
import constants from '../../constants.mts'
45
import { setCiExitCode } from '../../utils/ci-exit-code.mts'
56
import { debugDir, debugFn } from '../../utils/debug.mts'
@@ -32,6 +33,23 @@ export async function handleCi(autoManifest: boolean): Promise<void> {
3233
const cwd = process.cwd()
3334
const branchName = (await gitBranch(cwd)) || (await detectDefaultBranch(cwd))
3435
const repoName = await getRepoName(cwd)
36+
let baselineScanId: string | undefined
37+
38+
if (pullRequest) {
39+
const baselineCResult = await selectBaselineScan({
40+
branch: pullRequestContext.baseBranch ?? '',
41+
commitHash: pullRequestContext.baseCommitHash ?? '',
42+
orgSlug,
43+
repo: repoName,
44+
})
45+
if (!baselineCResult.ok) {
46+
process.exitCode = baselineCResult.code ?? 1
47+
logger.log(serializeResultJson(baselineCResult))
48+
return
49+
}
50+
baselineScanId = baselineCResult.data.id
51+
debugFn('notice', `Selected PR baseline scan ${baselineScanId}`)
52+
}
3553

3654
debugFn(
3755
'notice',
@@ -53,6 +71,7 @@ export async function handleCi(autoManifest: boolean): Promise<void> {
5371
// When 'pendingHead' is true, it requires 'branchName' set and 'tmp' false.
5472
pendingHead: true,
5573
pullRequest,
74+
baselineScanId,
5675
reach: {
5776
dynamicSbomInference: undefined,
5877
excludePaths: [],
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { fetchOrgFullScanList } from '../scan/fetch-list-scans.mts'
2+
3+
import type { CResult } from '../../types.mts'
4+
import type { FullScanItem } from '@socketsecurity/sdk'
5+
6+
export type SelectBaselineScanConfig = {
7+
branch: string
8+
commitHash: string
9+
orgSlug: string
10+
repo: string
11+
}
12+
13+
export function findBaselineScan(
14+
scans: FullScanItem[],
15+
commitHash: string,
16+
): FullScanItem | undefined {
17+
return scans.find(scan => scan.commit_hash === commitHash)
18+
}
19+
20+
export async function selectBaselineScan({
21+
branch,
22+
commitHash,
23+
orgSlug,
24+
repo,
25+
}: SelectBaselineScanConfig): Promise<CResult<FullScanItem>> {
26+
if (!commitHash) {
27+
return {
28+
ok: false,
29+
message: 'Cannot select PR scan baseline',
30+
cause: 'The CI checkout does not provide a commit SHA.',
31+
}
32+
}
33+
34+
const scansCResult = await fetchOrgFullScanList({
35+
branch,
36+
commitHash,
37+
direction: 'desc',
38+
from_time: '',
39+
orgSlug,
40+
page: 1,
41+
perPage: 1,
42+
repo,
43+
sort: 'created_at',
44+
})
45+
if (!scansCResult.ok) {
46+
return scansCResult
47+
}
48+
49+
const scan = findBaselineScan(scansCResult.data.results, commitHash)
50+
return scan
51+
? { ok: true, data: scan }
52+
: {
53+
ok: false,
54+
message: 'No matching PR scan baseline found',
55+
cause: `No scan exists for commit ${commitHash} on ${repo}/${branch}. Run a scan for the base commit first.`,
56+
}
57+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
2+
3+
import {
4+
findBaselineScan,
5+
selectBaselineScan,
6+
} from './select-baseline-scan.mts'
7+
import { fetchOrgFullScanList } from '../scan/fetch-list-scans.mts'
8+
9+
vi.mock('../scan/fetch-list-scans.mts', () => ({
10+
fetchOrgFullScanList: vi.fn(),
11+
}))
12+
13+
const scan = {
14+
api_url: null,
15+
created_at: '2026-01-01T00:00:00Z',
16+
html_report_url: 'https://socket.dev/report',
17+
id: 'scan-base',
18+
integration_repo_url: null,
19+
integration_type: null,
20+
organization_id: 'org-id',
21+
organization_slug: 'test-org',
22+
repo: 'test-repo',
23+
repository_id: 'repo-id',
24+
repository_slug: 'test-repo',
25+
updated_at: '2026-01-01T00:00:00Z',
26+
commit_hash: 'base-sha',
27+
branch: 'main',
28+
}
29+
30+
describe('selectBaselineScan', () => {
31+
beforeEach(() => {
32+
vi.mocked(fetchOrgFullScanList).mockReset()
33+
})
34+
35+
it('selects the exact commit when the scan list includes nearby scans', () => {
36+
expect(
37+
findBaselineScan(
38+
[{ ...scan, id: 'newer', commit_hash: 'newer-sha' }, scan],
39+
'base-sha',
40+
),
41+
).toEqual(scan)
42+
})
43+
44+
it('selects an exact commit scan from the matching repo and branch query', async () => {
45+
vi.mocked(fetchOrgFullScanList).mockResolvedValue({
46+
ok: true,
47+
data: { results: [scan] },
48+
})
49+
await expect(
50+
selectBaselineScan({
51+
branch: 'main',
52+
commitHash: 'base-sha',
53+
orgSlug: 'test-org',
54+
repo: 'test-repo',
55+
}),
56+
).resolves.toEqual({ ok: true, data: scan })
57+
expect(fetchOrgFullScanList).toHaveBeenCalledWith(
58+
expect.objectContaining({
59+
branch: 'main',
60+
commitHash: 'base-sha',
61+
perPage: 1,
62+
repo: 'test-repo',
63+
}),
64+
)
65+
})
66+
67+
it('fails without a commit SHA', async () => {
68+
await expect(
69+
selectBaselineScan({
70+
branch: 'main',
71+
commitHash: '',
72+
orgSlug: 'test-org',
73+
repo: 'test-repo',
74+
}),
75+
).resolves.toMatchObject({ ok: false })
76+
expect(fetchOrgFullScanList).not.toHaveBeenCalled()
77+
})
78+
79+
it('fails closed when the exact commit has no scan', async () => {
80+
vi.mocked(fetchOrgFullScanList).mockResolvedValue({
81+
ok: true,
82+
data: { results: [scan] },
83+
})
84+
await expect(
85+
selectBaselineScan({
86+
branch: 'main',
87+
commitHash: 'unknown-sha',
88+
orgSlug: 'test-org',
89+
repo: 'test-repo',
90+
}),
91+
).resolves.toMatchObject({
92+
ok: false,
93+
message: 'No matching PR scan baseline found',
94+
})
95+
})
96+
})

‎src/commands/manifest/bazel/python-shim.test.mts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ describe('provisionPythonShim', () => {
8585
),
8686
).rejects.toThrow(/python/)
8787
},
88+
30_000,
8889
)
8990

9091
it.skipIf(!python || process.platform === 'win32')(

‎src/commands/scan/fetch-list-scans.mts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { FullScanListResult } from '@socketsecurity/sdk'
77

88
export type FetchOrgFullScanListConfig = {
99
branch: string
10+
commitHash?: string | undefined
1011
direction: string
1112
from_time: string
1213
orgSlug: string
@@ -35,14 +36,25 @@ export async function fetchOrgFullScanList(
3536
}
3637
const sockSdk = sockSdkCResult.data
3738

38-
const { branch, direction, from_time, orgSlug, page, perPage, repo, sort } = {
39+
const {
40+
branch,
41+
commitHash,
42+
direction,
43+
from_time,
44+
orgSlug,
45+
page,
46+
perPage,
47+
repo,
48+
sort,
49+
} = {
3950
__proto__: null,
4051
...config,
4152
} as FetchOrgFullScanListConfig
4253

4354
return await handleApiCall(
4455
sockSdk.listFullScans(orgSlug, {
4556
...(branch ? { branch } : {}),
57+
...(commitHash ? { commit_hash: commitHash } : {}),
4658
...(repo ? { repo } : {}),
4759
...(sort ? { sort: sort as 'name' | 'created_at' } : {}),
4860
...(direction ? { direction: direction as 'asc' | 'desc' } : {}),

‎src/commands/scan/handle-create-new-scan.mts‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { applyFullExcludePaths } from './exclude-paths.mts'
77
import { fetchCreateOrgFullScan } from './fetch-create-org-full-scan.mts'
88
import { fetchSupportedScanFileNames } from './fetch-supported-scan-file-names.mts'
99
import { finalizeTier1Scan } from './finalize-tier1-scan.mts'
10+
import { handlePrDiffScan } from './handle-diff-scan.mts'
1011
import { handleScanReport } from './handle-scan-report.mts'
1112
import { outputCreateNewScan } from './output-create-new-scan.mts'
1213
import { performReachabilityAnalysis } from './perform-reachability-analysis.mts'
@@ -91,6 +92,7 @@ export type GeneratedScanFiles = {
9192

9293
export type HandleCreateNewScanConfig = {
9394
autoManifest: boolean
95+
baselineScanId?: string | undefined
9496
branchName: string
9597
commitHash: string
9698
commitMessage: string
@@ -125,6 +127,7 @@ export type HandleCreateNewScanConfig = {
125127
async function createNewScan(
126128
{
127129
autoManifest,
130+
baselineScanId,
128131
branchName,
129132
commitHash,
130133
commitMessage,
@@ -475,16 +478,25 @@ async function createNewScan(
475478

476479
if (report && fullScanCResult.ok) {
477480
if (scanId) {
478-
await handleScanReport({
479-
filepath: '-',
480-
fold: constants.FOLD_SETTING_VERSION,
481-
includeLicensePolicy: true,
482-
orgSlug,
483-
outputKind,
484-
reportLevel,
485-
scanId,
486-
short: false,
487-
})
481+
if (baselineScanId) {
482+
await handlePrDiffScan({
483+
id1: baselineScanId,
484+
id2: scanId,
485+
orgSlug,
486+
outputKind,
487+
})
488+
} else {
489+
await handleScanReport({
490+
filepath: '-',
491+
fold: constants.FOLD_SETTING_VERSION,
492+
includeLicensePolicy: true,
493+
orgSlug,
494+
outputKind,
495+
reportLevel,
496+
scanId,
497+
short: false,
498+
})
499+
}
488500
} else {
489501
await outputCreateNewScan(
490502
{

‎src/commands/scan/handle-diff-scan.mts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,23 @@ export async function handleDiffScan({
3333
strictBlocking,
3434
})
3535
}
36+
37+
export async function handlePrDiffScan({
38+
id1,
39+
id2,
40+
orgSlug,
41+
outputKind,
42+
}: {
43+
id1: string
44+
id2: string
45+
orgSlug: string
46+
outputKind: OutputKind
47+
}): Promise<void> {
48+
const result = await fetchDiffScan({ id1, id2, orgSlug })
49+
await outputDiffScan(result, {
50+
depth: 2,
51+
file: '',
52+
outputKind,
53+
strictBlocking: false,
54+
})
55+
}

0 commit comments

Comments
 (0)