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
44 changes: 44 additions & 0 deletions src/commands/fix/fix-branch-helpers.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { debugFn } from '@socketsecurity/registry/lib/debug'

import {
getSocketBranchFullNameComponent,
getSocketBranchPurlTypeComponent,
} from './git.mts'
import { getPurlObject } from '../../utils/purl.mts'

import type { CiEnv } from './fix-env-helpers.mts'
import type { SocketBranchParseResult } from './git.mts'
import type { PrMatch } from './open-pr.mts'

export function getActiveBranchesForPackage(
ciEnv: CiEnv | null | undefined,
partialPurl: string,
openPrs: PrMatch[],
): SocketBranchParseResult[] {
if (!ciEnv) {
return []
}

const partialPurlObj = getPurlObject(partialPurl)
const activeBranches: SocketBranchParseResult[] = []
const branchFullName = getSocketBranchFullNameComponent(partialPurlObj)
const branchPurlType = getSocketBranchPurlTypeComponent(partialPurlObj)

for (const pr of openPrs) {
const parsedBranch = ciEnv.branchParser(pr.headRefName)
if (
branchPurlType === parsedBranch?.type &&
branchFullName === parsedBranch?.fullName
) {
activeBranches.push(parsedBranch)
}
}

if (activeBranches.length) {
debugFn(`found: ${activeBranches.length} active branches\n`, activeBranches)
} else if (openPrs.length) {
debugFn('miss: 0 active branches found')
}

return activeBranches
}
4 changes: 3 additions & 1 deletion src/commands/fix/fix-env-helpers.mts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export function getCiEnv(): CiEnv | null {
: null
}

export async function getOpenPrsForEnvironment(env: CiEnv): Promise<PrMatch[]> {
export async function getOpenPrsForEnvironment(
env: CiEnv | null | undefined,
): Promise<PrMatch[]> {
return env
? await getOpenSocketPrs(env.repoInfo.owner, env.repoInfo.repo, {
author: env.gitUser,
Expand Down
31 changes: 6 additions & 25 deletions src/commands/fix/npm-fix.mts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import {
} from '@socketsecurity/registry/lib/packages'
import { naturalCompare } from '@socketsecurity/registry/lib/sorts'

import { getActiveBranchesForPackage } from './fix-branch-helpers.mts'
import { getCiEnv, getOpenPrsForEnvironment } from './fix-env-helpers.mts'
import {
getSocketBranchFullNameComponent,
getSocketBranchName,
getSocketBranchPurlTypeComponent,
getSocketBranchWorkspaceComponent,
getSocketCommitMessage,
gitCreateAndPushBranch,
Expand Down Expand Up @@ -55,7 +54,6 @@ import { applyRange } from '../../utils/semver.mts'
import { getCveInfoFromAlertsMap } from '../../utils/socket-package-alert.mts'
import { idToPurl } from '../../utils/spec.mts'

import type { SocketBranchParseResult } from './git.mts'
import type {
ArboristInstance,
NodeClass,
Expand Down Expand Up @@ -199,28 +197,11 @@ export async function npmFix(
continue infoEntriesLoop
}

const activeBranches: SocketBranchParseResult[] = []
if (ciEnv) {
const branchFullName = getSocketBranchFullNameComponent(partialPurlObj)
const branchPurlType = getSocketBranchPurlTypeComponent(partialPurlObj)
for (const pr of openPrs) {
const parsedBranch = ciEnv.branchParser!(pr.headRefName)
if (
branchPurlType === parsedBranch?.type &&
branchFullName === parsedBranch?.fullName
) {
activeBranches.push(parsedBranch)
}
}
if (activeBranches.length) {
debugFn(
`found: ${activeBranches.length} active branches\n`,
activeBranches,
)
} else if (openPrs.length) {
debugFn('miss: 0 active branches found')
}
}
const activeBranches = getActiveBranchesForPackage(
ciEnv,
infoEntry[0],
openPrs,
)

logger.log(`Processing vulns for ${name}:`)
logger.indent()
Expand Down
31 changes: 6 additions & 25 deletions src/commands/fix/pnpm-fix.mts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ import {
} from '@socketsecurity/registry/lib/packages'
import { naturalCompare } from '@socketsecurity/registry/lib/sorts'

import { getActiveBranchesForPackage } from './fix-branch-helpers.mts'
import { getCiEnv, getOpenPrsForEnvironment } from './fix-env-helpers.mts'
import {
getSocketBranchFullNameComponent,
getSocketBranchName,
getSocketBranchPurlTypeComponent,
getSocketBranchWorkspaceComponent,
getSocketCommitMessage,
gitCreateAndPushBranch,
Expand Down Expand Up @@ -64,7 +63,6 @@ import { applyRange } from '../../utils/semver.mts'
import { getCveInfoFromAlertsMap } from '../../utils/socket-package-alert.mts'
import { idToPurl } from '../../utils/spec.mts'

import type { SocketBranchParseResult } from './git.mts'
import type { NodeClass } from '../../shadow/npm/arborist/types.mts'
import type { CResult, StringKeyValueObject } from '../../types.mts'
import type { EnvDetails } from '../../utils/package-environment.mts'
Expand Down Expand Up @@ -274,28 +272,11 @@ export async function pnpmFix(
continue infoEntriesLoop
}

const activeBranches: SocketBranchParseResult[] = []
if (ciEnv) {
const branchFullName = getSocketBranchFullNameComponent(partialPurlObj)
const branchPurlType = getSocketBranchPurlTypeComponent(partialPurlObj)
for (const pr of openPrs) {
const parsedBranch = ciEnv.branchParser!(pr.headRefName)
if (
branchPurlType === parsedBranch?.type &&
branchFullName === parsedBranch?.fullName
) {
activeBranches.push(parsedBranch)
}
}
if (activeBranches.length) {
debugFn(
`found: ${activeBranches.length} active branches\n`,
activeBranches,
)
} else if (openPrs.length) {
debugFn('miss: 0 active branches found')
}
}
const activeBranches = getActiveBranchesForPackage(
ciEnv,
infoEntry[0],
openPrs,
)

logger.log(`Processing vulns for ${name}:`)
logger.indent()
Expand Down