Skip to content

Commit e1e0078

Browse files
committed
feat(python-cli-parity): support authorized PR ignore commands
1 parent b94be6f commit e1e0078

14 files changed

Lines changed: 986 additions & 37 deletions

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@
369369
!/src/commands/ci/fetch-default-org-slug.test.mts
370370
!/src/commands/ci/handle-ci.mts
371371
!/src/commands/ci/handle-ci.test.mts
372+
!/src/commands/ci/pr-ignore-commands.test.mts
372373
!/src/commands/ci/handle-ci-skip.test.mts
373374
!/src/commands/ci/detect-ci-pull-request-context.test.mts
374375
!/src/commands/ci/select-baseline-scan.mts

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,24 @@ const config: CliCommandConfig = {
4040
description:
4141
'Fail for existing blocking alerts as well as alerts introduced by this PR.',
4242
},
43+
disableIgnore: {
44+
type: 'boolean',
45+
default: false,
46+
description:
47+
'Ignore no PR comments and do not show @SocketSecurity ignore instructions.',
48+
},
4349
exitCodeOnApiError: {
4450
type: 'number',
4551
default: 3,
4652
description:
4753
'Exit code for Socket API errors (default: 3). Ignored with --disable-blocking.',
4854
},
55+
ignoreAuthorization: {
56+
type: 'string',
57+
default: 'enforce',
58+
description:
59+
'Authorization policy for PR ignore commands: enforce (default), strict, or off.',
60+
},
4961
jsonFormat: {
5062
type: 'string',
5163
description:
@@ -77,6 +89,8 @@ const config: CliCommandConfig = {
7789
$ ${command} --auto-manifest
7890
$ ${command} --disable-blocking
7991
$ ${command} --strict-blocking
92+
$ ${command} --disable-ignore
93+
$ ${command} --ignore-authorization strict
8094
$ ${command} --exit-code-on-api-error 5
8195
`,
8296
}
@@ -129,9 +143,20 @@ async function run(
129143
return
130144
}
131145

146+
const ignoreAuthorization = String(cli.flags['ignoreAuthorization'] ?? 'enforce')
147+
if (!['enforce', 'strict', 'off'].includes(ignoreAuthorization)) {
148+
logger.fail('--ignore-authorization must be enforce, strict, or off.')
149+
setCiExitCode('usage')
150+
return
151+
}
152+
132153
await handleCi(
133154
Boolean(cli.flags['autoManifest']),
134155
jsonFormat === 'python-v1',
135156
Boolean(cli.flags['strictBlocking']),
157+
{
158+
authorization: ignoreAuthorization as 'enforce' | 'strict' | 'off',
159+
disabled: Boolean(cli.flags['disableIgnore']),
160+
},
136161
)
137162
}

‎src/commands/ci/cmd-ci.test.mts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ describe('socket ci', async () => {
2525
Options
2626
--auto-manifest Auto generate manifest files where detected? See autoManifest flag in \`socket scan create\`
2727
--disable-blocking Return success for findings and infrastructure failures, but keep usage errors non-zero.
28+
--disable-ignore Ignore no PR comments and do not show @SocketSecurity ignore instructions.
2829
--exit-code-on-api-error Exit code for Socket API errors (default: 3). Ignored with --disable-blocking.
30+
--ignore-authorization Authorization policy for PR ignore commands: enforce (default), strict, or off.
2931
--json-format Select a versioned JSON payload. Supported: python-v1 (pull request comparisons only).
3032
--strict-blocking Fail for existing blocking alerts as well as alerts introduced by this PR.
3133
@@ -47,6 +49,8 @@ describe('socket ci', async () => {
4749
$ socket ci --auto-manifest
4850
$ socket ci --disable-blocking
4951
$ socket ci --strict-blocking
52+
$ socket ci --disable-ignore
53+
$ socket ci --ignore-authorization strict
5054
$ socket ci --exit-code-on-api-error 5"
5155
`,
5256
)

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { detectCiPullRequestContext } from './detect-ci-pull-request-context.mts'
22
import { getDefaultOrgSlug } from './fetch-default-org-slug.mts'
3+
import {
4+
markPrIgnoreCommandsProcessed,
5+
readPrIgnoreCommands,
6+
} from './pr-ignore-commands.mts'
37
import {
48
reportGitLabCommitStatusFailure,
59
withGitLabCommitStatus,
@@ -34,11 +38,16 @@ import {
3438
} from '../scan/python-diff-json.mts'
3539

3640
import type { PythonDiffJsonContext } from '../scan/python-diff-json.mts'
41+
import type { IgnoreAuthorization } from './pr-ignore-commands.mts'
3742

3843
export async function handleCi(
3944
autoManifest: boolean,
4045
pythonJsonFormat = false,
4146
strictBlocking = false,
47+
ignoreSettings: {
48+
authorization?: IgnoreAuthorization | undefined
49+
disabled?: boolean | undefined
50+
} = {},
4251
): Promise<void> {
4352
debugFn('notice', 'Starting CI scan')
4453
debugDir('inspect', { autoManifest, pythonJsonFormat })
@@ -183,6 +192,8 @@ export async function handleCi(
183192
pullRequestContext,
184193
pythonJsonContext,
185194
pythonJsonFormat,
195+
ignoreAuthorization: ignoreSettings.authorization ?? 'enforce',
196+
disableIgnore: ignoreSettings.disabled ?? false,
186197
repoName,
187198
})
188199
})
@@ -192,6 +203,8 @@ async function runCiScan({
192203
autoManifest,
193204
branchName,
194205
cwd,
206+
disableIgnore,
207+
ignoreAuthorization,
195208
onDiffOutcome,
196209
orgSlug,
197210
pullRequestContext,
@@ -213,6 +226,8 @@ async function runCiScan({
213226
pullRequestContext: Awaited<ReturnType<typeof detectCiPullRequestContext>>
214227
pythonJsonContext: PythonDiffJsonContext
215228
pythonJsonFormat: boolean
229+
ignoreAuthorization: IgnoreAuthorization
230+
disableIgnore: boolean
216231
repoName: string
217232
}): Promise<void> {
218233
const { number: pullRequest } = pullRequestContext
@@ -266,6 +281,13 @@ async function runCiScan({
266281
debugDir('inspect', { orgSlug, cwd, branchName, repoName })
267282

268283
const [owner, repo] = (constants.ENV.GITHUB_REPOSITORY || '').split('/')
284+
const ignoreCommands = baselineScanId
285+
? await readPrIgnoreCommands({
286+
authorization: ignoreAuthorization,
287+
context: pullRequestContext,
288+
disabled: disableIgnore,
289+
})
290+
: []
269291
await handleCreateNewScan({
270292
autoManifest,
271293
branchName,
@@ -286,6 +308,7 @@ async function runCiScan({
286308
pendingHead: true,
287309
pullRequest,
288310
pullRequestUrl: pullRequestContext.url,
311+
prIgnoreCommands: ignoreCommands,
289312
baselineScanId,
290313
onDiffReport: async (diffScanId, outcome) => {
291314
onDiffOutcome(outcome)
@@ -302,6 +325,10 @@ async function runCiScan({
302325
console.error(
303326
'Socket could not publish GitLab merge request notes: missing target project metadata. Check CI_MERGE_REQUEST_PROJECT_ID, CI_MERGE_REQUEST_PROJECT_URL, and CI_SERVER_URL.',
304327
)
328+
await markPrIgnoreCommandsProcessed({
329+
commands: ignoreCommands,
330+
context: pullRequestContext,
331+
})
305332
return
306333
}
307334
try {
@@ -314,6 +341,8 @@ async function runCiScan({
314341
process.env['SOCKET_CLI_DISABLE_SECURITY_COMMENT'],
315342
),
316343
orgSlug,
344+
ignoreCommands,
345+
disableIgnore,
317346
mergeRequestIid: pullRequest,
318347
projectId,
319348
projectUrl,
@@ -326,6 +355,10 @@ async function runCiScan({
326355
errorMessage(error),
327356
)
328357
}
358+
await markPrIgnoreCommandsProcessed({
359+
commands: ignoreCommands,
360+
context: pullRequestContext,
361+
})
329362
return
330363
}
331364
if (
@@ -334,6 +367,10 @@ async function runCiScan({
334367
!repo ||
335368
!constants.ENV.SOCKET_CLI_GITHUB_TOKEN
336369
) {
370+
await markPrIgnoreCommandsProcessed({
371+
commands: ignoreCommands,
372+
context: pullRequestContext,
373+
})
337374
return
338375
}
339376
try {
@@ -346,6 +383,8 @@ async function runCiScan({
346383
process.env['SOCKET_CLI_DISABLE_SECURITY_COMMENT'],
347384
),
348385
orgSlug,
386+
ignoreCommands,
387+
disableIgnore,
349388
owner,
350389
pullRequest,
351390
repo,
@@ -357,6 +396,10 @@ async function runCiScan({
357396
'Socket could not publish the scan comments. Check the Socket API scopes and GitHub pull request write permission.\n',
358397
)
359398
}
399+
await markPrIgnoreCommandsProcessed({
400+
commands: ignoreCommands,
401+
context: pullRequestContext,
402+
})
360403
},
361404
reach: {
362405
dynamicSbomInference: undefined,

0 commit comments

Comments
 (0)