Skip to content

Commit 5199171

Browse files
committed
Attempt fix
1 parent 8513a67 commit 5199171

3 files changed

Lines changed: 50 additions & 60 deletions

File tree

src/commands/fix/agent-fix.mts

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync } from 'node:fs'
1+
import { existsSync, promises as fs } from 'node:fs'
22
import path from 'node:path'
33

44
import semver from 'semver'
@@ -253,11 +253,7 @@ export async function agentFix(
253253
await getActualTree(cwd)
254254
: // eslint-disable-next-line no-await-in-loop
255255
await installer(pkgEnvDetails, { cwd, spinner })
256-
const maybeLockSrc = maybeActualTree
257-
? // eslint-disable-next-line no-await-in-loop
258-
await readLockfile(pkgEnvDetails.lockPath)
259-
: null
260-
if (maybeActualTree && maybeLockSrc) {
256+
if (maybeActualTree && existsSync(pkgEnvDetails.lockPath)) {
261257
actualTree = maybeActualTree
262258
}
263259
}
@@ -385,19 +381,19 @@ export async function agentFix(
385381
)
386382

387383
// eslint-disable-next-line no-await-in-loop
388-
const res = await editablePkgJson.save({ ignoreWhitespace: true })
384+
await editablePkgJson.save({ ignoreWhitespace: true })
389385

390386
// eslint-disable-next-line no-await-in-loop
391387
const unstagedCResult = await gitUnstagedModifiedFiles(cwd)
392-
const moddedFilepaths = res && unstagedCResult.ok
393-
? unstagedCResult.data.filter(filepath => {
394-
const basename = path.basename(filepath)
395-
return (
396-
basename === 'package.json' ||
397-
basename === pkgEnvDetails.lockName
398-
)
399-
})
400-
: []
388+
const moddedFilepaths = unstagedCResult.ok
389+
? unstagedCResult.data.filter(filepath => {
390+
const basename = path.basename(filepath)
391+
return (
392+
basename === 'package.json' ||
393+
basename === pkgEnvDetails.lockName
394+
)
395+
})
396+
: []
401397
if (!moddedFilepaths.length) {
402398
logger.warn(
403399
'Unexpected condition: Nothing to commit, skipping PR creation.',
@@ -410,6 +406,9 @@ export async function agentFix(
410406
continue infosLoop
411407
}
412408

409+
// eslint-disable-next-line no-await-in-loop
410+
const lockSrc = await readLockfile(pkgEnvDetails.lockPath)
411+
413412
if (!hasAnnouncedWorkspace) {
414413
hasAnnouncedWorkspace = true
415414
workspaceLogCallCount = logger.logCallCount
@@ -428,14 +427,7 @@ export async function agentFix(
428427
cwd,
429428
spinner,
430429
})
431-
// eslint-disable-next-line no-await-in-loop
432-
const unstagedCResult = await gitUnstagedModifiedFiles(cwd)
433-
console.log('after installer', unstagedCResult)
434-
const maybeLockSrc = maybeActualTree
435-
? // eslint-disable-next-line no-await-in-loop
436-
await readLockfile(pkgEnvDetails.lockPath)
437-
: null
438-
if (maybeActualTree && maybeLockSrc) {
430+
if (maybeActualTree && existsSync(pkgEnvDetails.lockPath)) {
439431
actualTree = maybeActualTree
440432
// eslint-disable-next-line no-await-in-loop
441433
await afterInstall(
@@ -446,9 +438,6 @@ export async function agentFix(
446438
vulnerableVersionRange,
447439
fixConfig,
448440
)
449-
// eslint-disable-next-line no-await-in-loop
450-
const unstagedCResult = await gitUnstagedModifiedFiles(cwd)
451-
console.log('after afterInstall', unstagedCResult)
452441
if (test) {
453442
spinner?.info(`Testing ${newId} in ${workspace}.`)
454443
// eslint-disable-next-line no-await-in-loop
@@ -468,17 +457,11 @@ export async function agentFix(
468457

469458
// Check repoInfo to make TypeScript happy.
470459
if (!errored && fixEnv.isCi && fixEnv.repoInfo) {
460+
// Rewrite files in case the install reverted them.
471461
// eslint-disable-next-line no-await-in-loop
472-
const unstagedCResult = await gitUnstagedModifiedFiles(cwd)
473-
const moddedFilepaths = unstagedCResult.ok
474-
? unstagedCResult.data.filter(filepath => {
475-
const basename = path.basename(filepath)
476-
return (
477-
basename === 'package.json' ||
478-
basename === pkgEnvDetails.lockName
479-
)
480-
})
481-
: []
462+
await editablePkgJson.save({ ignoreWhitespace: true })
463+
// eslint-disable-next-line no-await-in-loop
464+
await fs.writeFile(pkgEnvDetails.lockPath, lockSrc!, 'utf8')
482465
try {
483466
if (
484467
// eslint-disable-next-line no-await-in-loop
@@ -503,11 +486,7 @@ export async function agentFix(
503486
cwd,
504487
spinner,
505488
})
506-
const maybeLockSrc = maybeActualTree
507-
? // eslint-disable-next-line no-await-in-loop
508-
await readLockfile(pkgEnvDetails.lockPath)
509-
: null
510-
if (maybeActualTree && maybeLockSrc) {
489+
if (maybeActualTree && existsSync(pkgEnvDetails.lockPath)) {
511490
actualTree = maybeActualTree
512491
continue infosLoop
513492
}

src/commands/fix/git.mts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import semver from 'semver'
22

33
import { PackageURL } from '@socketregistry/packageurl-js'
4-
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
5-
import { logger } from '@socketsecurity/registry/lib/logger'
4+
import { debugDir, debugFn, isDebug } from '@socketsecurity/registry/lib/debug'
65
import { normalizePath } from '@socketsecurity/registry/lib/path'
76
import { escapeRegExp } from '@socketsecurity/registry/lib/regexps'
87
import { spawn } from '@socketsecurity/registry/lib/spawn'
@@ -217,7 +216,10 @@ export function getSocketPullRequestTitle(
217216
}
218217

219218
export async function gitCleanFdx(cwd = process.cwd()): Promise<void> {
220-
const stdioIgnoreOptions: SpawnOptions = { cwd, stdio: 'ignore' }
219+
const stdioIgnoreOptions: SpawnOptions = {
220+
cwd,
221+
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
222+
}
221223
// TODO: propagate CResult?
222224
await spawn('git', ['clean', '-fdx'], stdioIgnoreOptions)
223225
}
@@ -235,8 +237,10 @@ export async function gitCreateAndPushBranch(
235237
// Lazily access constants.ENV.SOCKET_CLI_GIT_USER_NAME.
236238
user = constants.ENV.SOCKET_CLI_GIT_USER_NAME,
237239
} = { __proto__: null, ...options } as GitCreateAndPushBranchOptions
238-
const stdioIgnoreOptions: SpawnOptions = { cwd, stdio: 'inherit' }
239-
logger.dir({ branch, user, email, cwd, filepaths, commitMsg })
240+
const stdioIgnoreOptions: SpawnOptions = {
241+
cwd,
242+
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
243+
}
240244
try {
241245
await gitEnsureIdentity(user, email, cwd)
242246
await spawn('git', ['checkout', '-b', branch], stdioIgnoreOptions)
@@ -249,12 +253,11 @@ export async function gitCreateAndPushBranch(
249253
)
250254
return true
251255
} catch (e) {
252-
logger.dir({ error: e })
253-
// debugFn(
254-
// 'error',
255-
// `caught: git push --force --set-upstream origin ${branch} failed`,
256-
// )
257-
// debugDir('inspect', { error: e })
256+
debugFn(
257+
'error',
258+
`caught: git push --force --set-upstream origin ${branch} failed`,
259+
)
260+
debugDir('inspect', { error: e })
258261
}
259262
try {
260263
// Will throw with exit code 1 if branch does not exist.
@@ -304,7 +307,10 @@ export async function gitEnsureIdentity(
304307
email: string,
305308
cwd = process.cwd(),
306309
): Promise<void> {
307-
const stdioIgnoreOptions: SpawnOptions = { cwd, stdio: 'ignore' }
310+
const stdioIgnoreOptions: SpawnOptions = {
311+
cwd,
312+
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
313+
}
308314
const stdioPipeOptions: SpawnOptions = { cwd }
309315
const identEntries: Array<[string, string]> = [
310316
['user.email', name],
@@ -364,7 +370,10 @@ export async function gitResetHard(
364370
branch = 'HEAD',
365371
cwd = process.cwd(),
366372
): Promise<void> {
367-
const stdioIgnoreOptions: SpawnOptions = { cwd, stdio: 'ignore' }
373+
const stdioIgnoreOptions: SpawnOptions = {
374+
cwd,
375+
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
376+
}
368377
await spawn('git', ['reset', '--hard', branch], stdioIgnoreOptions)
369378
}
370379

@@ -376,11 +385,10 @@ export async function gitUnstagedModifiedFiles(
376385
const changedFilesDetails = (
377386
await spawn('git', ['diff', '--name-only'], stdioPipeOptions)
378387
).stdout
379-
const rawRelPaths = changedFilesDetails.split('\n') ?? []
380-
console.log({ rawRelPaths })
388+
const relPaths = changedFilesDetails.split('\n') ?? []
381389
return {
382390
ok: true,
383-
data: rawRelPaths.map(p => normalizePath(p)),
391+
data: relPaths.map(p => normalizePath(p)),
384392
}
385393
} catch (e) {
386394
debugFn('error', 'caught: git diff --name-only failed')

src/commands/fix/pull-request.mts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Octokit } from '@octokit/rest'
1010
import semver from 'semver'
1111

1212
import { PackageURL } from '@socketregistry/packageurl-js'
13-
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
13+
import { debugDir, debugFn, isDebug } from '@socketsecurity/registry/lib/debug'
1414
import { readJson, writeJson } from '@socketsecurity/registry/lib/fs'
1515
import { spawn } from '@socketsecurity/registry/lib/spawn'
1616
import { isNonEmptyString } from '@socketsecurity/registry/lib/strings'
@@ -534,7 +534,10 @@ export async function setGitRemoteGithubRepoUrl(
534534
token: string,
535535
cwd = process.cwd(),
536536
): Promise<void> {
537-
const stdioIgnoreOptions: SpawnOptions = { cwd, stdio: 'ignore' }
537+
const stdioIgnoreOptions: SpawnOptions = {
538+
cwd,
539+
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
540+
}
538541
const { host } = new URL(constants.ENV.GITHUB_SERVER_URL)
539542
const url = `https://x-access-token:${token}@${host}/${owner}/${repo}`
540543
try {

0 commit comments

Comments
 (0)