From 601e75c19fd2410f317e99ebb01be56dc2b91c9e Mon Sep 17 00:00:00 2001 From: jdalton Date: Tue, 10 Jun 2025 14:11:41 -0400 Subject: [PATCH] Make getActiveBranchesForPackage helper --- src/commands/fix/fix-branch-helpers.mts | 44 +++++++++++++++++++++++++ src/commands/fix/fix-env-helpers.mts | 4 ++- src/commands/fix/npm-fix.mts | 31 ++++------------- src/commands/fix/pnpm-fix.mts | 31 ++++------------- 4 files changed, 59 insertions(+), 51 deletions(-) create mode 100644 src/commands/fix/fix-branch-helpers.mts diff --git a/src/commands/fix/fix-branch-helpers.mts b/src/commands/fix/fix-branch-helpers.mts new file mode 100644 index 000000000..987f85072 --- /dev/null +++ b/src/commands/fix/fix-branch-helpers.mts @@ -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 +} diff --git a/src/commands/fix/fix-env-helpers.mts b/src/commands/fix/fix-env-helpers.mts index c658b3a9b..c3e344a38 100644 --- a/src/commands/fix/fix-env-helpers.mts +++ b/src/commands/fix/fix-env-helpers.mts @@ -38,7 +38,9 @@ export function getCiEnv(): CiEnv | null { : null } -export async function getOpenPrsForEnvironment(env: CiEnv): Promise { +export async function getOpenPrsForEnvironment( + env: CiEnv | null | undefined, +): Promise { return env ? await getOpenSocketPrs(env.repoInfo.owner, env.repoInfo.repo, { author: env.gitUser, diff --git a/src/commands/fix/npm-fix.mts b/src/commands/fix/npm-fix.mts index fd1f31a4e..8f1f89701 100644 --- a/src/commands/fix/npm-fix.mts +++ b/src/commands/fix/npm-fix.mts @@ -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, @@ -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, @@ -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() diff --git a/src/commands/fix/pnpm-fix.mts b/src/commands/fix/pnpm-fix.mts index 67f5c8e4e..9ec6a7acd 100644 --- a/src/commands/fix/pnpm-fix.mts +++ b/src/commands/fix/pnpm-fix.mts @@ -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, @@ -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' @@ -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()