Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/cli.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import lookupRegistryAuthToken from 'registry-auth-token'
import lookupRegistryUrl from 'registry-url'
import updateNotifier from 'tiny-updater'

import { debugFn, debugLog } from '@socketsecurity/registry/lib/debug'
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
import { logger } from '@socketsecurity/registry/lib/logger'

import { cmdAnalytics } from './commands/analytics/cmd-analytics.mts'
Expand Down Expand Up @@ -186,8 +186,8 @@ void (async () => {
)
} catch (e) {
process.exitCode = 1
debugFn('Uncaught error (BAD!):')
debugFn(e)
debugFn('error', 'Uncaught error (BAD!):')
debugDir('inspect', { error: e })

// Try to parse the flags, find out if --json or --markdown is set.
let isJson = false
Expand Down Expand Up @@ -231,8 +231,7 @@ void (async () => {
logger.error('\n') // Any-spinner-newline
logger.fail(failMsgWithBadge(errorTitle, errorMessage))
if (errorBody) {
// Explicitly use debugLog here.
debugLog(errorBody)
debugDir('inspect', { errorBody })
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/commands/audit-log/output-audit-log.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRequire } from 'node:module'

import { debugFn } from '@socketsecurity/registry/lib/debug'
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
import { logger } from '@socketsecurity/registry/lib/logger'

import constants from '../../constants.mts'
Expand Down Expand Up @@ -182,7 +182,8 @@ ${table}
logger.fail(
'There was a problem converting the logs to Markdown, please try the `--json` flag',
)
debugFn('catch: unexpected\n', e)
debugFn('error', 'caught: unexpected error')
debugDir('inspect', { error: e })
return 'Failed to generate the markdown report'
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/ci/fetch-default-org-slug.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function getDefaultOrgSlug(): Promise<CResult<string>> {
const defaultOrgResult = getConfigValueOrUndef('defaultOrg')

if (defaultOrgResult) {
debugFn('use: default org', defaultOrgResult)
debugFn('notice', 'use: default org', defaultOrgResult)
return { ok: true, data: defaultOrgResult }
}

Expand Down Expand Up @@ -51,7 +51,7 @@ export async function getDefaultOrgSlug(): Promise<CResult<string>> {
}
}

debugFn('resolve: org', slug)
debugFn('notice', 'resolve: org', slug)

return {
ok: true,
Expand Down
31 changes: 15 additions & 16 deletions src/commands/fix/agent-fix.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import semver from 'semver'

import { getManifestData } from '@socketsecurity/registry'
import { arrayUnique } from '@socketsecurity/registry/lib/arrays'
import { debugFn, isDebug } from '@socketsecurity/registry/lib/debug'
import { debugDir, debugFn, isDebug } from '@socketsecurity/registry/lib/debug'
import { logger } from '@socketsecurity/registry/lib/logger'
import { runNpmScript } from '@socketsecurity/registry/lib/npm'
import {
Expand Down Expand Up @@ -134,15 +134,15 @@ export async function agentFix(
spinner?.stop()
logger.info('No fixable vulns found.')
if (alertsMap.size) {
debugFn('inspect:', { alertsMap })
debugDir('inspect', { alertsMap })
} else {
debugFn('inspect: { alertsMap: Map(0) {} }')
debugFn('inspect', '{ alertsMap: Map(0) {} }')
}
return { ok: true, data: { fixed: false } }
}

if (isDebug()) {
debugFn('found: cves for', Array.from(infoByPartialPurl.keys()))
if (isDebug('notice')) {
debugFn('notice', 'found: cves for', Array.from(infoByPartialPurl.keys()))
}

// Lazily access constants.packumentCache.
Expand Down Expand Up @@ -198,7 +198,7 @@ export async function agentFix(
spinner?.indent()

if (getManifestData(partialPurlObj.type, name)) {
debugFn(`found: Socket Optimize variant for ${name}`)
debugFn('notice', `found: Socket Optimize variant for ${name}`)
}
// eslint-disable-next-line no-await-in-loop
const packument = await fetchPackagePackument(name)
Expand Down Expand Up @@ -266,7 +266,7 @@ export async function agentFix(
)

if (!oldVersions.length) {
debugFn(`skip: ${name} not found\n`)
debugFn('notice', `skip: ${name} not found\n`)
// Skip to next package.
cleanupInfoEntriesLoop()
continue infoEntriesLoop
Expand All @@ -283,7 +283,7 @@ export async function agentFix(
let hasAnnouncedWorkspace = false
let workspaceLogCallCount = logger.logCallCount
if (isDebug()) {
debugFn(`check: workspace ${workspace}`)
debugFn('notice', `check: workspace ${workspace}`)
hasAnnouncedWorkspace = true
workspaceLogCallCount = logger.logCallCount
}
Expand All @@ -294,7 +294,7 @@ export async function agentFix(

const node = findPackageNode(actualTree, name, oldVersion)
if (!node) {
debugFn(`skip: ${oldId} not found`)
debugFn('notice', `skip: ${oldId} not found`)
continue oldVersionsLoop
}
infosLoop: for (const {
Expand All @@ -319,7 +319,7 @@ export async function agentFix(
continue infosLoop
}
if (semver.gte(oldVersion, newVersion)) {
debugFn(`skip: ${oldId} is >= ${newVersion}`)
debugFn('notice', `skip: ${oldId} is >= ${newVersion}`)
continue infosLoop
}
if (
Expand All @@ -328,7 +328,7 @@ export async function agentFix(
b.workspace === branchWorkspace && b.newVersion === newVersion,
)
) {
debugFn(`skip: open PR found for ${name}@${newVersion}`)
debugFn('notice', `skip: open PR found for ${name}@${newVersion}`)
if (++count >= limit) {
cleanupInfoEntriesLoop()
break infoEntriesLoop
Expand Down Expand Up @@ -366,7 +366,7 @@ export async function agentFix(
)
// eslint-disable-next-line no-await-in-loop
if (!(await editablePkgJson.save({ ignoreWhitespace: true }))) {
debugFn(`skip: ${workspace}/package.json unchanged`)
debugFn('notice', `skip: ${workspace}/package.json unchanged`)
// Reset things just in case.
if (ciEnv) {
// eslint-disable-next-line no-await-in-loop
Expand Down Expand Up @@ -461,12 +461,12 @@ export async function agentFix(
)
) {
skipPr = true
debugFn(`skip: branch "${branch}" exists`)
debugFn('notice', `skip: branch "${branch}" exists`)
}
// eslint-disable-next-line no-await-in-loop
else if (await gitRemoteBranchExists(branch, cwd)) {
skipPr = true
debugFn(`skip: remote branch "${branch}" exists`)
debugFn('notice', `skip: remote branch "${branch}" exists`)
} else if (
// eslint-disable-next-line no-await-in-loop
!(await gitCreateAndPushBranch(
Expand Down Expand Up @@ -613,8 +613,7 @@ export async function agentFix(
cause: `Update failed for ${oldId} in ${workspace}${error ? '; ' + error : ''}`,
}
}
debugFn('name:', name)
debugFn('increment: count', count + 1)
debugFn('notice', 'increment: count', count + 1)
if (++count >= limit) {
cleanupInfoEntriesLoop()
break infoEntriesLoop
Expand Down
5 changes: 3 additions & 2 deletions src/commands/fix/fix-branch-helpers.mts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ export function getActiveBranchesForPackage(
}
}

if (isDebug()) {
if (isDebug('notice')) {
const fullName = resolvePackageName(partialPurlObj)
if (activeBranches.length) {
debugFn(
'notice',
`found: ${activeBranches.length} active branches for ${fullName}\n`,
activeBranches,
)
} else if (openPrs.length) {
debugFn(`miss: 0 active branches found for ${fullName}`)
debugFn('notice', `miss: 0 active branches found for ${fullName}`)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/fix/fix-env-helpers.mts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function getEnvRepoInfo(
// Lazily access constants.ENV.GITHUB_REPOSITORY.
const { GITHUB_REPOSITORY } = constants.ENV
if (!GITHUB_REPOSITORY) {
debugFn('miss: GITHUB_REPOSITORY env var')
debugFn('notice', 'miss: GITHUB_REPOSITORY env var')
}
const ownerSlashRepo = GITHUB_REPOSITORY
const slashIndex = ownerSlashRepo.indexOf('/')
Expand Down
17 changes: 11 additions & 6 deletions src/commands/fix/git.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import semver from 'semver'

import { PackageURL } from '@socketregistry/packageurl-js'
import { debugFn } from '@socketsecurity/registry/lib/debug'
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
import { normalizePath } from '@socketsecurity/registry/lib/path'
import { escapeRegExp } from '@socketsecurity/registry/lib/regexps'
import { spawn } from '@socketsecurity/registry/lib/spawn'
Expand Down Expand Up @@ -246,7 +246,8 @@ export async function gitCreateAndPushBranch(
return true
} catch (e) {
debugFn(
`catch: git push --force --set-upstream origin ${branch} failed\n`,
'error',
`caught: git push --force --set-upstream origin ${branch} failed\n`,
e,
)
}
Expand Down Expand Up @@ -284,9 +285,11 @@ export async function gitRepoInfo(
return { owner, repo }
}
} catch {}
debugFn('git: unmatched git remote URL format', remoteUrl)
debugFn('error', 'git: unmatched git remote URL format')
debugDir('inspect', { remoteUrl })
} catch (e) {
debugFn('catch: git remote get-url origin failed\n', e)
debugFn('error', 'caught: git remote get-url origin failed')
debugDir('inspect', { error: e })
}
return null
}
Expand Down Expand Up @@ -315,7 +318,8 @@ export async function gitEnsureIdentity(
try {
await spawn('git', ['config', prop, value], stdioIgnoreOptions)
} catch (e) {
debugFn(`catch: git config ${prop} ${value} failed\n`, e)
debugFn('error', `caught: git config ${prop} ${value} failed`)
debugDir('inspect', { error: e })
}
}
}),
Expand Down Expand Up @@ -373,7 +377,8 @@ export async function gitUnstagedModifiedFiles(
data: rawRelPaths.map(relPath => normalizePath(relPath)),
}
} catch (e) {
debugFn('catch: git diff --name-only failed\n', e)
debugFn('error', 'caught: git diff --name-only failed')
debugDir('inspect', { error: e })

return {
ok: false,
Expand Down
8 changes: 5 additions & 3 deletions src/commands/fix/handle-fix.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debugFn } from '@socketsecurity/registry/lib/debug'
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
import { logger } from '@socketsecurity/registry/lib/logger'
import { pluralize } from '@socketsecurity/registry/lib/words'

Expand Down Expand Up @@ -59,7 +59,8 @@ export async function handleFix({
)
ghsasCount = ghsas.length
} else {
debugFn('coana fail:', {
debugFn('error', 'fail: Coana CLI')
debugDir('inspect', {
message: autoCResult.message,
cause: autoCResult.cause,
})
Expand Down Expand Up @@ -87,7 +88,8 @@ export async function handleFix({
spinner?.stop()

if (!applyFixesCResult.ok) {
debugFn('coana fail:', {
debugFn('error', 'fail: Coana CLI')
debugDir('inspect', {
message: applyFixesCResult.message,
cause: applyFixesCResult.cause,
})
Expand Down
5 changes: 3 additions & 2 deletions src/commands/fix/npm-fix.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debugFn, isDebug } from '@socketsecurity/registry/lib/debug'
import { debugDir, debugFn, isDebug } from '@socketsecurity/registry/lib/debug'

import { agentFix } from './agent-fix.mts'
import { getCiEnv, getOpenPrsForEnvironment } from './fix-env-helpers.mts'
Expand Down Expand Up @@ -77,7 +77,8 @@ export async function npmFix(
}
} catch (e) {
spinner?.stop()
debugFn('catch: PURL API\n', e)
debugFn('error', 'caught: PURL API')
debugDir('inspect', { error: e })
return {
ok: false,
message: 'API Error',
Expand Down
18 changes: 10 additions & 8 deletions src/commands/fix/open-pr.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Octokit } from '@octokit/rest'
import semver from 'semver'

import { PackageURL } from '@socketregistry/packageurl-js'
import { debugFn } from '@socketsecurity/registry/lib/debug'
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
import { readJson, writeJson } from '@socketsecurity/registry/lib/fs'
import { spawn } from '@socketsecurity/registry/lib/spawn'
import { isNonEmptyString } from '@socketsecurity/registry/lib/strings'
Expand All @@ -37,7 +37,7 @@ function getOctokit() {
// Lazily access constants.ENV.SOCKET_CLI_GITHUB_TOKEN.
const { SOCKET_CLI_GITHUB_TOKEN } = constants.ENV
if (!SOCKET_CLI_GITHUB_TOKEN) {
debugFn('miss: SOCKET_CLI_GITHUB_TOKEN env var')
debugFn('notice', 'miss: SOCKET_CLI_GITHUB_TOKEN env var')
}
_octokit = new Octokit({
auth: SOCKET_CLI_GITHUB_TOKEN,
Expand All @@ -52,7 +52,7 @@ export function getOctokitGraphql(): typeof OctokitGraphql {
// Lazily access constants.ENV.SOCKET_CLI_GITHUB_TOKEN.
const { SOCKET_CLI_GITHUB_TOKEN } = constants.ENV
if (!SOCKET_CLI_GITHUB_TOKEN) {
debugFn('miss: SOCKET_CLI_GITHUB_TOKEN env var')
debugFn('notice', 'miss: SOCKET_CLI_GITHUB_TOKEN env var')
}
_octokitGraphql = OctokitGraphql.defaults({
headers: {
Expand Down Expand Up @@ -171,14 +171,15 @@ export async function cleanupOpenPrs(
pull_number: prNum,
state: 'closed',
})
debugFn(`close: ${prRef} for ${prToVersion}`)
debugFn('notice', `close: ${prRef} for ${prToVersion}`)
// Remove entry from parent object.
context.parent.splice(context.index, 1)
// Mark cache to be saved.
cachesToSave.set(context.cacheKey, context.data)
return null
} catch (e) {
debugFn(
'error',
`fail: close ${prRef} for ${prToVersion}\n`,
(e as Error)?.message || 'unknown error',
)
Expand All @@ -194,7 +195,7 @@ export async function cleanupOpenPrs(
base: match.headRefName,
head: match.baseRefName,
})
debugFn('update: stale', prRef)
debugFn('notice', 'update: stale', prRef)
// Update entry entry.
if (context.apiType === 'graphql') {
context.entry.mergeStateStatus = 'CLEAN'
Expand All @@ -205,7 +206,7 @@ export async function cleanupOpenPrs(
cachesToSave.set(context.cacheKey, context.data)
} catch (e) {
const message = (e as Error)?.message || 'Unknown error'
debugFn(`fail: update ${prRef} - ${message}`)
debugFn('error', `fail: update ${prRef} - ${message}`)
}
}
return match
Expand Down Expand Up @@ -477,7 +478,7 @@ export async function openPr(
.join('\n')
message += `:\n${details}`
}
debugFn(message)
debugFn('error', message)
}
return null
}
Expand Down Expand Up @@ -512,6 +513,7 @@ export async function setGitRemoteGithubRepoUrl(
try {
await spawn('git', ['remote', 'set-url', 'origin', url], stdioIgnoreOptions)
} catch (e) {
debugFn('catch: unexpected\n', e)
debugFn('error', 'caught: unexpected error')
debugDir('inspect', { error: e })
}
}
Loading