forked from reown-com/appkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdangerfile.ts
More file actions
682 lines (564 loc) · 23.1 KB
/
dangerfile.ts
File metadata and controls
682 lines (564 loc) · 23.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/* eslint-disable no-await-in-loop */
import { danger, fail, message, warn } from 'danger'
// -- Constants ---------------------------------------------------------------
const TYPE_COMMENT = `// -- Types --------------------------------------------- //`
const STATE_COMMENT = `// -- State --------------------------------------------- //`
const CONTROLLER_COMMENT = `// -- Controller ---------------------------------------- //`
const RENDER_COMMENT = `// -- Render -------------------------------------------- //`
const STATE_PROPERTIES_COMMENT = `// -- State & Properties -------------------------------- //`
const PRIVATE_COMMENT = `// -- Private ------------------------------------------- //`
const RELATIVE_IMPORT_SAME_DIR = `'./`
const RELATIVE_IMPORT_PARENT_DIR = `'../`
const RELATIVE_IMPORT_EXTENSION = `.js'`
const PRIVATE_FUNCTION_REGEX = /private\s+(?:\w+)\s*\(\s*\)/gu
// -- Data --------------------------------------------------------------------
const { modified_files, created_files, deleted_files, diffForFile } = danger.git
const updated_files = [...modified_files, ...created_files].filter(f => !f.includes('dangerfile'))
const all_files = [...updated_files, ...created_files, ...deleted_files].filter(
f => !f.includes('dangerfile')
)
// -- Dependency Checks -------------------------------------------------------
async function checkPackageJsons() {
const packageJsons = all_files.filter(f => f.includes('package.json'))
const pnpmLock = updated_files.find(f => f.includes('pnpm-lock.yaml'))
const yarnLock = updated_files.find(f => f.includes('yarn.lock'))
const npmLock = updated_files.find(f => f.includes('package-lock.json'))
if (packageJsons.length && !pnpmLock) {
warn('Changes were made to one or more package.json(s), but not to pnpm-lock.yaml')
}
if (yarnLock || npmLock) {
fail('Non pnpm lockfile(s) detected (yarn / npm), please use pnpm')
}
for (const f of packageJsons) {
const diff = await diffForFile(f)
if (diff?.added.includes('^') || diff?.added.includes('~')) {
fail(`Loose dependency versions in ${f}, please use strict versioning`)
}
}
}
checkPackageJsons()
// -- Ui Package Checks -------------------------------------------------------
async function checkUiPackage() {
const created_ui_components = created_files.filter(f => f.includes('ui/src/components'))
const created_ui_composites = created_files.filter(f => f.includes('ui/src/composites'))
const deleted_ui_composites = deleted_files.filter(f => f.includes('ui/src/composites'))
const created_ui_layout = created_files.filter(f => f.includes('ui/src/layout'))
const deleted_ui_layout = deleted_files.filter(f => f.includes('ui/src/layout'))
const created_ui_files = [
...created_ui_components,
...created_ui_composites,
...created_ui_layout
]
const created_ui_index_files = created_ui_files.filter(f => f.includes('index.ts'))
const created_ui_style_files = created_ui_files.filter(f => f.includes('styles.ts'))
for (const f of created_ui_index_files) {
const diff = await diffForFile(f)
if (!diff?.added.includes('[resetStyles')) {
fail(`${f} does not apply resetStyles`)
}
if (diff?.added.includes('@state(')) {
fail(`${f} is using @state decorator, which is not allowed in ui package`)
}
if (diff?.added.includes('import @reown/appkit-controllers')) {
fail(`${f} is importing @reown/appkit-controllers, which is not allowed in ui package`)
}
if (!diff?.added.includes(RENDER_COMMENT) && diff?.added.includes('render()')) {
fail(`${f} is missing \`${RENDER_COMMENT}\` comment`)
}
if (diff?.added.includes('@property(') && !diff.added.includes(STATE_PROPERTIES_COMMENT)) {
fail(`${f} is missing \`${STATE_PROPERTIES_COMMENT}\` comment`)
}
const privateFunctionsAdded = diff?.added.match(PRIVATE_FUNCTION_REGEX)?.length
if (privateFunctionsAdded && !diff?.added.includes(PRIVATE_COMMENT)) {
message(
`${f} is missing \`${PRIVATE_COMMENT}\` comment, but seems to have private members. Check if this is correct`
)
}
if (!diff?.added.includes(`@customElement('wui-`)) {
fail(`${f} is a ui element, but does not define wui- prefix`)
}
if (diff?.added.includes('@reown/appkit-ui/')) {
fail(`${f} should use relative imports instead of direct package access`)
}
}
for (const f of created_ui_style_files) {
const diff = await diffForFile(f)
if (diff?.added.includes(':host') && !diff.added.includes('display: ')) {
fail(`${f} uses :host container, but does not set display style on it`)
}
}
const created_ui_components_stories = created_files.filter(f =>
f.includes('gallery/stories/components')
)
const created_ui_composites_stories = created_files.filter(f =>
f.includes('gallery/stories/composites')
)
const created_ui_layout_stories = created_files.filter(f => f.includes('gallery/stories/layout'))
const ui_index = modified_files.find(f => f.includes('ui/index.ts'))
const ui_index_diff = ui_index ? await diffForFile(ui_index) : undefined
const types_util_index = modified_files.find(f => f.includes('ui/src/utils/JSXTypeUtil.ts'))
const types_util_diff = types_util_index ? await diffForFile(types_util_index) : undefined
const created_ui_components_index_ts = created_ui_components.filter(f => f.endsWith('index.ts'))
const created_ui_composites_index_ts = created_ui_composites.filter(f => f.endsWith('index.ts'))
const deleted_ui_composites_index_ts = deleted_ui_composites.filter(f => f.endsWith('index.ts'))
const is_new_composites_added =
created_ui_composites_index_ts.length > deleted_ui_composites_index_ts.length
const created_ui_layout_index_ts = created_ui_layout.filter(f => f.endsWith('index.ts'))
const deleted_ui_layout_index_ts = deleted_ui_layout.filter(f => f.endsWith('index.ts'))
const is_new_layout_added = created_ui_layout_index_ts.length > deleted_ui_layout_index_ts.length
if (created_ui_components_index_ts.length && !ui_index_diff?.added.includes('src/components')) {
fail('New components were added, but not exported in ui/index.ts')
}
if (is_new_composites_added && !types_util_diff) {
fail('New composites were added, but JSXTypeUtil.ts is not modified')
}
if (created_ui_layout_index_ts.length && !ui_index_diff?.added.includes('src/layout')) {
fail('New layout components were added, but not exported in ui/index.ts')
}
if (created_ui_components_index_ts.length && !types_util_diff?.added.includes('../components')) {
fail(
`New components were added, but not exported in ui/utils/JSXTypeUtil.ts: ${created_ui_components.join(
', '
)}`
)
}
if (is_new_composites_added && !types_util_diff?.added.includes('../composites')) {
fail('New composites were added, but not exported in ui/utils/JSXTypeUtil.ts')
}
if (is_new_layout_added && !types_util_diff?.added.includes('../layout')) {
fail('New layout components were added, but not exported in ui/utils/JSXTypeUtil.ts')
}
if (created_ui_components.length && !created_ui_components_stories.length) {
fail('New components were added, but no stories were created')
}
if (is_new_composites_added && !created_ui_composites_stories.length) {
fail('New composites were added, but no stories were created')
}
if (created_ui_layout.length && !created_ui_layout_stories.length) {
fail('New layout components were added, but no stories were created')
}
}
checkUiPackage()
// -- Core Package Checks -----------------------------------------------------
async function checkCorePackage() {
const created_core_controllers = created_files.filter(f => f.includes('core/src/controllers'))
const created_core_controllers_tests = created_files.filter(f =>
f.includes('core/tests/controllers')
)
const modified_core_controllers = modified_files.filter(f => f.includes('core/src/controllers'))
const modified_core_controllers_tests = modified_files.filter(f =>
f.includes('core/tests/controllers')
)
for (const f of created_core_controllers) {
const diff = await diffForFile(f)
if (diff?.added.includes('import @reown/appkit-ui')) {
fail(`${f} is importing @reown/appkit-ui, which is not allowed in core package`)
}
if (!diff?.added.includes(TYPE_COMMENT)) {
fail(`${f} is missing \`${TYPE_COMMENT}\` comment`)
}
if (!diff?.added.includes(STATE_COMMENT)) {
fail(`${f} is missing \`${STATE_COMMENT}\` comment`)
}
if (!diff?.added.includes(CONTROLLER_COMMENT)) {
fail(`${f} is missing \`${CONTROLLER_COMMENT}\` comment`)
}
if (diff?.added.includes('this.state')) {
fail(`${f} is using this.state, use just state`)
}
if (diff?.added.includes('@reown/appkit-controllers/')) {
fail(`${f} should use relative imports instead of direct package access`)
}
if (diff?.added.includes("'valtio'")) {
fail(`${f} is importing valtio, but should use valtio/vanilla`)
}
}
if (created_core_controllers.length && !created_core_controllers_tests.length) {
fail('New controllers were added, but no tests were created')
}
if (modified_core_controllers.length && !modified_core_controllers_tests) {
message(`
The following controllers were modified, but not tests were changed:
${modified_core_controllers.join('\n')}
`)
}
}
checkCorePackage()
// -- Scaffold Html Package Checks --------------------------------------------
async function checkScaffoldHtmlPackage() {
const created_modal = created_files.filter(f => f.includes('scaffold/src/modal'))
const created_pages = created_files.filter(f => f.includes('scaffold/src/pages'))
const created_partials = created_files.filter(f => f.includes('scaffold/src/partials'))
const created_scaffold_files = [...created_modal, ...created_pages, ...created_partials]
const created_scaffold_index_files = created_scaffold_files.filter(f => f.includes('index.ts'))
for (const f of created_scaffold_index_files) {
const diff = await diffForFile(f)
if (!diff?.added.includes(RENDER_COMMENT) && diff?.added.includes('render()')) {
fail(`${f} is missing \`${RENDER_COMMENT}\` comment`)
}
if (
(diff?.added.includes('@state(') || diff?.added.includes('@property(')) &&
!diff.added.includes(STATE_PROPERTIES_COMMENT)
) {
fail(`${f} is missing \`${STATE_PROPERTIES_COMMENT}\` comment`)
}
const privateFunctionsAdded = diff?.added.match(PRIVATE_FUNCTION_REGEX)?.length
if (privateFunctionsAdded && !diff?.added.includes(PRIVATE_COMMENT)) {
message(
`${f} is missing \`${PRIVATE_COMMENT}\` comment, but seems to have private members. Check if this is correct`
)
}
if (!diff?.added.includes(`@customElement('w3m-`)) {
fail(`${f} is a scaffold element, but does not define w3m- prefix`)
}
if (diff?.added.includes('.subscribe') && !diff.added.includes('this.unsubscribe.forEach')) {
fail(`${f} is subscribing to controller states without unsubscribe logic`)
}
if (
diff?.added.includes('@reown/appkit-controllers/') ||
diff?.added.includes('@reown/appkit-ui/') ||
diff?.added.includes('@reown/scaffold/')
) {
fail(`${f} should use relative imports instead of direct package access`)
}
}
}
checkScaffoldHtmlPackage()
// -- Client(s) Package Checks ----------------------------------------------------
// -- Helper functions
function isRelativeImport(addition: string | undefined) {
const sameDir = addition?.includes(RELATIVE_IMPORT_SAME_DIR)
const parentDir = addition?.includes(RELATIVE_IMPORT_PARENT_DIR)
return sameDir || parentDir
}
function containsRelativeImportWithoutJSExtension(addition: string | undefined) {
const hasImportStatement = addition?.includes('import')
const lacksJSExtension = !addition?.includes(RELATIVE_IMPORT_EXTENSION)
const hasRelativePath = isRelativeImport(addition)
return hasImportStatement && lacksJSExtension && hasRelativePath
}
async function checkClientPackages() {
const client_files = modified_files.filter(f =>
/\/packages\/(?:wagmi|solana|ethers|ethers5)\//u.test(f)
)
for (const f of client_files) {
const diff = await diffForFile(f)
if (diff?.added.includes("from '@reown/appkit-controllers")) {
fail(`${f} is not allowed to import from @reown/appkit-controllers`)
}
if (diff?.added.includes("from '@reown/appkit-ui")) {
fail(`${f} is not allowed to import from @reown/appkit-ui`)
}
if (containsRelativeImportWithoutJSExtension(diff?.added)) {
fail(`${f} contains relative imports without .js extension`)
}
}
}
checkClientPackages()
// -- Check wallet ------------------------------------------------------------
async function checkWallet() {
const wallet_files = modified_files.filter(f => f.includes('/wallet/'))
for (const f of wallet_files) {
const diff = await diffForFile(f)
if (f.includes('W3mFrameConstants') && diff?.added.includes('SECURE_SITE_SDK')) {
warn('Secure site URL has been changed')
}
}
}
checkWallet()
// -- Check wallet schema breaking changes -------------------------------------
async function checkWalletSchema() {
const wallet_schema_files = modified_files.filter(
f =>
f.includes('wallet/') && (f.includes('W3mFrameSchema.ts') || f.includes('W3mFrameTypes.ts'))
)
for (const f of wallet_schema_files) {
const diff = await diffForFile(f)
if (!diff) {
// eslint-disable-next-line no-continue
continue
}
const addedLines = diff.added.split('\n')
const removedLines = diff.removed.split('\n')
const fieldPattern = /^[+-]?\s*(?<fieldName>[a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*z\./u
// Check for new required fields
for (const line of addedLines) {
const fieldMatch = line.match(fieldPattern)
if (!fieldMatch) {
// eslint-disable-next-line no-continue
continue
}
const fieldName = fieldMatch.groups?.['fieldName']
const isOptional = line.includes('.optional()') || line.includes('z.optional(')
if (isOptional) {
// eslint-disable-next-line no-continue
continue
}
if (
fieldName &&
!line.trim().startsWith('//') &&
!line.includes('*') &&
!line.includes('export')
) {
fail(
`Breaking change in ${f}: New required field '${fieldName}' added. Make it optional with .optional() for backwards compatibility.`
)
}
}
// Check for removed required fields
for (const removedLine of removedLines) {
const fieldMatch = removedLine.match(fieldPattern)
const isOptional = removedLine.includes('.optional()') || removedLine.includes('z.optional(')
if (!fieldMatch || isOptional) {
// eslint-disable-next-line no-continue
continue
}
const fieldName = fieldMatch.groups?.['fieldName']
if (!fieldName) {
// eslint-disable-next-line no-continue
continue
}
const isMovedOrMadeOptional = addedLines.some(addedLine => {
const addedFieldMatch = addedLine.match(fieldPattern)
if (!addedFieldMatch) {
return false
}
const addedFieldName = addedFieldMatch.groups?.['fieldName']
if (addedFieldName !== fieldName) {
return false
}
const addedIsOptional =
addedLine.includes('.optional()') || addedLine.includes('z.optional(')
const normalizedRemoved = removedLine.replace(/^[+-]\s*/u, '').trim()
const normalizedAdded = addedLine.replace(/^[+-]\s*/u, '').trim()
const isSameDefinition = normalizedAdded === normalizedRemoved
return addedIsOptional || isSameDefinition
})
if (
!isMovedOrMadeOptional &&
!removedLine.trim().startsWith('//') &&
!removedLine.includes('*') &&
!removedLine.includes('export')
) {
fail(
`Breaking change in ${f}: Required field '${fieldName}' was removed. This breaks backwards compatibility.`
)
}
}
// Check for fields changed from optional to required
for (const removedLine of removedLines) {
const wasOptional = removedLine.includes('.optional()') || removedLine.includes('z.optional(')
if (!wasOptional) {
// eslint-disable-next-line no-continue
continue
}
const fieldMatch = removedLine.match(fieldPattern)
if (!fieldMatch) {
// eslint-disable-next-line no-continue
continue
}
const fieldName = fieldMatch.groups?.['fieldName']
if (!fieldName) {
// eslint-disable-next-line no-continue
continue
}
const madeRequired = addedLines.some(addedLine => {
const addedFieldMatch = addedLine.match(fieldPattern)
const addedIsOptional =
addedLine.includes('.optional()') || addedLine.includes('z.optional(')
return (
addedFieldMatch && addedFieldMatch.groups?.['fieldName'] === fieldName && !addedIsOptional
)
})
if (madeRequired) {
fail(
`Breaking change in ${f}: Field '${fieldName}' changed from optional to required. This breaks backwards compatibility.`
)
}
}
// Warn about type changes that might be breaking
if (diff.added.includes('z.enum(') || diff.removed.includes('z.enum(')) {
warn(
`Enum changes detected in ${f}. Please verify that enum modifications maintain backwards compatibility.`
)
}
// Check interface changes in types file
if (f.includes('W3mFrameTypes.ts')) {
const removedInterfaceProps = removedLines.filter(
line =>
line.includes(':') &&
!line.trim().startsWith('//') &&
!line.includes('export') &&
!line.includes('interface') &&
!line.includes('.optional()') &&
!line.includes('z.optional(')
)
if (removedInterfaceProps.length > 0) {
warn(
`Type interface changes detected in ${f}. Please verify that removed properties maintain backwards compatibility.`
)
}
}
}
}
checkWalletSchema()
// -- Check laboratory ------------------------------------------------------------
async function checkLaboratory() {
const lab_files = modified_files.filter(f => f.includes('/laboratory/'))
for (const f of lab_files) {
const diff = await diffForFile(f)
if (f.includes('project') && (diff?.removed.includes('spec') || diff?.added.includes('spec'))) {
warn('Testing spec changed')
}
}
// Check that no .only is present in tests
const test_files = lab_files.filter(f => f.includes('.spec.ts'))
for (const f of test_files) {
const fileContent = await danger.github.utils.fileContents(f)
if (fileContent.includes('.only')) {
fail(`${f} contains .only, please remove it`)
}
}
}
checkLaboratory()
// -- Check left over development constants ---------------------------------------
async function checkDevelopmentConstants() {
for (const f of updated_files) {
if (f.includes('README.md') || f.includes('.yml')) {
// eslint-disable-next-line no-continue
continue
}
const diff = await diffForFile(f)
const fileContent = await danger.github.utils.fileContents(f)
if (diff?.added.includes('localhost:') && !fileContent.includes('// Allow localhost')) {
warn(`${f} uses localhost: which is likely a mistake`)
}
}
}
checkDevelopmentConstants()
// -- Check changesets ------------------------------------------------------------
async function checkChangesetFiles() {
const changesetFiles = updated_files
.filter(f => f.startsWith('.changeset/'))
.filter(f => f.endsWith('.md') && !f.startsWith('README.md'))
const ignoredChangesetFiles = [
'@apps/gallery-new',
'@reown/appkit-ui-new',
'@reown/appkit-scaffold-ui-new',
'@apps/laboratory-new',
'@reown/appkit-new'
]
for (const f of changesetFiles) {
const fileContent = await danger.github.utils.fileContents(f)
if (fileContent.includes('@examples/')) {
fail(`Changeset file ${f} cannot include @examples/* packages as part of the changeset`)
}
if (ignoredChangesetFiles.some(ignored => fileContent.includes(ignored))) {
fail(
`Changeset file ${f} cannot include ${ignoredChangesetFiles
.map(changesetFile => changesetFile)
.join(', ')} packages as part of the changeset`
)
}
}
}
checkChangesetFiles()
// -- Check Workflows ------------------------------------------------------------
function checkWorkflows() {
const updatedWorkflows = updated_files.filter(f => f.includes('.github/workflows/'))
const deletedWorkflows = deleted_files.filter(f => f.includes('.github/workflows/'))
for (const f of deletedWorkflows) {
fail(`Workflow file(s) ${f} has been deleted`)
}
for (const f of updatedWorkflows) {
warn(`Workflow file ${f} has been modified`)
}
}
checkWorkflows()
const secretPatterns = [
{
pattern: /\b(?:sk_|pk_|ak_|api_|key_|tok_)[a-zA-Z0-9]{20,}\b/gu,
description: 'API key with common prefix'
},
{
pattern: /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/giu,
description: 'UUID'
},
{
pattern: /\beyJ[a-zA-Z0-9+/]+=*\.[a-zA-Z0-9+/]+=*\.[a-zA-Z0-9+/\-_]+=*/gu,
description: 'JWT token'
},
{
pattern: /-----BEGIN\s+(?:RSA\s+)?PRIVATE\s+KEY-----/giu,
description: 'Private key'
},
{
pattern: /\bAKIA[0-9A-Z]{16}\b/gu,
description: 'AWS access key'
},
{
pattern: /\bghp_[a-zA-Z0-9]{36}\b/gu,
description: 'GitHub Personal Access Token'
}
]
function shouldIgnoreMatch(match: string, content: string): boolean {
if (/https?:\/\//iu.test(match)) {
return true
}
// Skip pure alphabetic strings
if (/^[A-Za-z]{32,}$/u.test(match)) {
return true
}
// Skip file paths and extensions
if (/\.(?<file_extension>js|ts|json|md|txt|css|scss|html|xml|yml|yaml)$/u.test(match)) {
return true
}
const idx = content.indexOf(match)
if (idx !== -1) {
const ctx = content.substring(Math.max(0, idx - 40), idx + match.length + 40)
if (/process\.env\s*(?:\.|\[)/u.test(ctx)) {
return true
}
// Skip import/require statements
if (/(?<imports>import|require|from)\s+['"`]/iu.test(ctx)) {
return true
}
}
return false
}
function findLineNumber(lines: string[], snippet: string): number | null {
for (const [i, line] of lines.entries()) {
if (line.includes(snippet)) {
return i + 1
}
}
return null
}
async function scanFileForSecrets(filepath: string) {
const fileContent = await danger.github.utils.fileContents(filepath)
if (!fileContent) {
return
}
const lines = fileContent.split('\n')
for (const { pattern, description } of secretPatterns) {
const matches = fileContent.match(pattern) ?? []
for (const match of matches) {
if (shouldIgnoreMatch(match, fileContent)) {
// eslint-disable-next-line no-continue
continue
}
const lineNumber = findLineNumber(lines, match) ?? 'unknown'
const preview = `${match.slice(0, 20)}...`
warn(
`🔑 Potential ${description} detected in ${filepath} (line ${lineNumber}): \`${preview}\``
)
}
}
}
async function checkForKeys() {
const candidateFiles = [...updated_files, ...created_files].filter(
f => !f.endsWith('pnpm-lock.yaml')
)
await Promise.all(candidateFiles.map(scanFileForSecrets))
}
checkForKeys()