@@ -2,6 +2,14 @@ import type { BranchProtectionResponse, UpdateBranchProtectionParams } from '@/t
22import { BRANCH_PROTECTION_OUTPUT_PROPERTIES } from '@/tools/github/types'
33import type { ToolConfig } from '@/tools/types'
44
5+ /**
6+ * Names the failure class without echoing the rejected text. `JSON.parse` quotes
7+ * the input it rejected back into its own message, and these fields can carry
8+ * values resolved from other blocks.
9+ */
10+ const BRANCH_PROTECTION_SHAPE_ERROR =
11+ 'Branch protection fields must be a JSON object, or left empty to disable the rule'
12+
513/**
614 * GitHub documents `required_status_checks`, `enforce_admins`,
715 * `required_pull_request_reviews` and `restrictions` as required body fields
@@ -16,15 +24,20 @@ function toNullableObject(value: unknown): Record<string, unknown> | null {
1624 if ( typeof value === 'string' ) {
1725 const trimmed = value . trim ( )
1826 if ( trimmed === '' || trimmed === 'null' ) return null
19- const parsed : unknown = JSON . parse ( trimmed )
27+ let parsed : unknown
28+ try {
29+ parsed = JSON . parse ( trimmed )
30+ } catch {
31+ throw new Error ( BRANCH_PROTECTION_SHAPE_ERROR )
32+ }
2033 if ( parsed === null ) return null
2134 if ( typeof parsed !== 'object' || Array . isArray ( parsed ) ) {
22- throw new Error ( 'Branch protection fields must be JSON objects' )
35+ throw new Error ( BRANCH_PROTECTION_SHAPE_ERROR )
2336 }
2437 return parsed as Record < string , unknown >
2538 }
2639 if ( typeof value !== 'object' || Array . isArray ( value ) ) {
27- throw new Error ( 'Branch protection fields must be JSON objects' )
40+ throw new Error ( BRANCH_PROTECTION_SHAPE_ERROR )
2841 }
2942 return value as Record < string , unknown >
3043}
0 commit comments