Skip to content

Commit b981da5

Browse files
authored
include PR link and number in JSON output for socket fix. Also remove… (#1063)
* include PR link and number in JSON output for socket fix. Also removes the nested data layer * update json output to use an array
1 parent 0c6038f commit b981da5

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

src/commands/fix/coana-fix.mts

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async function discoverGhsaIds(
111111

112112
export async function coanaFix(
113113
fixConfig: FixConfig,
114-
): Promise<CResult<{ data?: unknown; fixed: boolean }>> {
114+
): Promise<CResult<{ fixedAll: boolean; ghsaDetails: unknown[] }>> {
115115
const {
116116
all,
117117
applyFixes,
@@ -237,7 +237,7 @@ export async function coanaFix(
237237
if (!silence) {
238238
spinner?.stop()
239239
}
240-
return { ok: true, data: { fixed: false } }
240+
return { ok: true, data: { fixedAll: false, ghsaDetails: [] } }
241241
}
242242

243243
// Create a temporary file for the output.
@@ -290,7 +290,10 @@ export async function coanaFix(
290290
}
291291

292292
// Read the temporary file to get the actual fixes result.
293-
const fixesResultJson = readJsonSync(tmpFile, { throws: false })
293+
const fixesResultJson = readJsonSync(tmpFile, { throws: false }) as
294+
| { fixes?: Record<string, unknown> }
295+
| null
296+
| undefined
294297

295298
// Copy to outputFile if provided.
296299
if (outputFile) {
@@ -301,7 +304,13 @@ export async function coanaFix(
301304
await fs.writeFile(outputFile, tmpContent, 'utf8')
302305
}
303306

304-
return { ok: true, data: { data: fixesResultJson, fixed: true } }
307+
return {
308+
ok: true,
309+
data: {
310+
fixedAll: true,
311+
ghsaDetails: fixesResultJson ? [fixesResultJson] : [],
312+
},
313+
}
305314
} finally {
306315
// Clean up the temporary file.
307316
try {
@@ -366,7 +375,7 @@ export async function coanaFix(
366375
if (!silence) {
367376
spinner?.stop()
368377
}
369-
return { ok: true, data: { fixed: false } }
378+
return { ok: true, data: { fixedAll: false, ghsaDetails: [] } }
370379
}
371380

372381
debugFn('notice', `fetch: ${ids.length} GHSA details for ${joinAnd(ids)}`)
@@ -378,12 +387,17 @@ export async function coanaFix(
378387

379388
let count = 0
380389
let overallFixed = false
390+
const ghsaFixResults: unknown[] = []
381391

382392
// Process each GHSA ID individually.
383393
ghsaLoop: for (let i = 0, { length } = ids; i < length; i += 1) {
384394
const ghsaId = ids[i]!
385395
debugFn('notice', `check: ${ghsaId}`)
386396

397+
// Create a temporary file for Coana output.
398+
const tmpDir = os.tmpdir()
399+
const tmpFile = path.join(tmpDir, `socket-fix-${ghsaId}-${Date.now()}.json`)
400+
387401
// Apply fix for single GHSA ID.
388402
// eslint-disable-next-line no-await-in-loop
389403
const fixCResult = await spawnCoanaDlx(
@@ -408,6 +422,8 @@ export async function coanaFix(
408422
...(showAffectedDirectDependencies
409423
? ['--show-affected-direct-dependencies']
410424
: []),
425+
'--output-file',
426+
tmpFile,
411427
...fixConfig.unknownFlags,
412428
],
413429
fixConfig.orgSlug,
@@ -425,6 +441,13 @@ export async function coanaFix(
425441
`Update failed for ${ghsaId}: ${getErrorCause(fixCResult)}`,
426442
)
427443
}
444+
// Clean up temp file on failure.
445+
try {
446+
// eslint-disable-next-line no-await-in-loop
447+
await fs.unlink(tmpFile)
448+
} catch {
449+
// Ignore cleanup errors.
450+
}
428451
continue ghsaLoop
429452
}
430453

@@ -439,6 +462,13 @@ export async function coanaFix(
439462

440463
if (!modifiedFiles.length) {
441464
debugFn('notice', `skip: no changes for ${ghsaId}`)
465+
// Clean up temp file before continuing.
466+
try {
467+
// eslint-disable-next-line no-await-in-loop
468+
await fs.unlink(tmpFile)
469+
} catch {
470+
// Ignore cleanup errors.
471+
}
442472
continue ghsaLoop
443473
}
444474

@@ -553,6 +583,16 @@ export async function coanaFix(
553583
const { data } = prResult.pr
554584
const prRef = `PR #${data.number}`
555585

586+
// Read the fix result JSON and merge with PR data.
587+
const fixResultJson = readJsonSync(tmpFile, { throws: false })
588+
if (fixResultJson && typeof fixResultJson === 'object') {
589+
ghsaFixResults.push({
590+
...(fixResultJson as object),
591+
pullRequestLink: data.html_url,
592+
pullRequestNumber: data.number,
593+
})
594+
}
595+
556596
if (!silence) {
557597
logger.success(`Opened ${prRef} for ${ghsaId}.`)
558598
}
@@ -646,6 +686,14 @@ export async function coanaFix(
646686
await gitResetAndClean(fixEnv.baseBranch, cwd)
647687
// eslint-disable-next-line no-await-in-loop
648688
await gitCheckoutBranch(fixEnv.baseBranch, cwd)
689+
} finally {
690+
// Clean up temp file.
691+
try {
692+
// eslint-disable-next-line no-await-in-loop
693+
await fs.unlink(tmpFile)
694+
} catch {
695+
// Ignore cleanup errors.
696+
}
649697
}
650698

651699
count += 1
@@ -664,6 +712,6 @@ export async function coanaFix(
664712

665713
return {
666714
ok: true,
667-
data: { fixed: overallFixed },
715+
data: { fixedAll: overallFixed, ghsaDetails: ghsaFixResults },
668716
}
669717
}

0 commit comments

Comments
 (0)