Skip to content

Commit fec7760

Browse files
authored
Make getActiveBranchesForPackage helper (#651)
1 parent 6b3d2b2 commit fec7760

File tree

4 files changed

+59
-51
lines changed

4 files changed

+59
-51
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { debugFn } from '@socketsecurity/registry/lib/debug'
2+
3+
import {
4+
getSocketBranchFullNameComponent,
5+
getSocketBranchPurlTypeComponent,
6+
} from './git.mts'
7+
import { getPurlObject } from '../../utils/purl.mts'
8+
9+
import type { CiEnv } from './fix-env-helpers.mts'
10+
import type { SocketBranchParseResult } from './git.mts'
11+
import type { PrMatch } from './open-pr.mts'
12+
13+
export function getActiveBranchesForPackage(
14+
ciEnv: CiEnv | null | undefined,
15+
partialPurl: string,
16+
openPrs: PrMatch[],
17+
): SocketBranchParseResult[] {
18+
if (!ciEnv) {
19+
return []
20+
}
21+
22+
const partialPurlObj = getPurlObject(partialPurl)
23+
const activeBranches: SocketBranchParseResult[] = []
24+
const branchFullName = getSocketBranchFullNameComponent(partialPurlObj)
25+
const branchPurlType = getSocketBranchPurlTypeComponent(partialPurlObj)
26+
27+
for (const pr of openPrs) {
28+
const parsedBranch = ciEnv.branchParser(pr.headRefName)
29+
if (
30+
branchPurlType === parsedBranch?.type &&
31+
branchFullName === parsedBranch?.fullName
32+
) {
33+
activeBranches.push(parsedBranch)
34+
}
35+
}
36+
37+
if (activeBranches.length) {
38+
debugFn(`found: ${activeBranches.length} active branches\n`, activeBranches)
39+
} else if (openPrs.length) {
40+
debugFn('miss: 0 active branches found')
41+
}
42+
43+
return activeBranches
44+
}

src/commands/fix/fix-env-helpers.mts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export function getCiEnv(): CiEnv | null {
3838
: null
3939
}
4040

41-
export async function getOpenPrsForEnvironment(env: CiEnv): Promise<PrMatch[]> {
41+
export async function getOpenPrsForEnvironment(
42+
env: CiEnv | null | undefined,
43+
): Promise<PrMatch[]> {
4244
return env
4345
? await getOpenSocketPrs(env.repoInfo.owner, env.repoInfo.repo, {
4446
author: env.gitUser,

src/commands/fix/npm-fix.mts

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ import {
1414
} from '@socketsecurity/registry/lib/packages'
1515
import { naturalCompare } from '@socketsecurity/registry/lib/sorts'
1616

17+
import { getActiveBranchesForPackage } from './fix-branch-helpers.mts'
1718
import { getCiEnv, getOpenPrsForEnvironment } from './fix-env-helpers.mts'
1819
import {
19-
getSocketBranchFullNameComponent,
2020
getSocketBranchName,
21-
getSocketBranchPurlTypeComponent,
2221
getSocketBranchWorkspaceComponent,
2322
getSocketCommitMessage,
2423
gitCreateAndPushBranch,
@@ -55,7 +54,6 @@ import { applyRange } from '../../utils/semver.mts'
5554
import { getCveInfoFromAlertsMap } from '../../utils/socket-package-alert.mts'
5655
import { idToPurl } from '../../utils/spec.mts'
5756

58-
import type { SocketBranchParseResult } from './git.mts'
5957
import type {
6058
ArboristInstance,
6159
NodeClass,
@@ -199,28 +197,11 @@ export async function npmFix(
199197
continue infoEntriesLoop
200198
}
201199

202-
const activeBranches: SocketBranchParseResult[] = []
203-
if (ciEnv) {
204-
const branchFullName = getSocketBranchFullNameComponent(partialPurlObj)
205-
const branchPurlType = getSocketBranchPurlTypeComponent(partialPurlObj)
206-
for (const pr of openPrs) {
207-
const parsedBranch = ciEnv.branchParser!(pr.headRefName)
208-
if (
209-
branchPurlType === parsedBranch?.type &&
210-
branchFullName === parsedBranch?.fullName
211-
) {
212-
activeBranches.push(parsedBranch)
213-
}
214-
}
215-
if (activeBranches.length) {
216-
debugFn(
217-
`found: ${activeBranches.length} active branches\n`,
218-
activeBranches,
219-
)
220-
} else if (openPrs.length) {
221-
debugFn('miss: 0 active branches found')
222-
}
223-
}
200+
const activeBranches = getActiveBranchesForPackage(
201+
ciEnv,
202+
infoEntry[0],
203+
openPrs,
204+
)
224205

225206
logger.log(`Processing vulns for ${name}:`)
226207
logger.indent()

src/commands/fix/pnpm-fix.mts

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ import {
1515
} from '@socketsecurity/registry/lib/packages'
1616
import { naturalCompare } from '@socketsecurity/registry/lib/sorts'
1717

18+
import { getActiveBranchesForPackage } from './fix-branch-helpers.mts'
1819
import { getCiEnv, getOpenPrsForEnvironment } from './fix-env-helpers.mts'
1920
import {
20-
getSocketBranchFullNameComponent,
2121
getSocketBranchName,
22-
getSocketBranchPurlTypeComponent,
2322
getSocketBranchWorkspaceComponent,
2423
getSocketCommitMessage,
2524
gitCreateAndPushBranch,
@@ -64,7 +63,6 @@ import { applyRange } from '../../utils/semver.mts'
6463
import { getCveInfoFromAlertsMap } from '../../utils/socket-package-alert.mts'
6564
import { idToPurl } from '../../utils/spec.mts'
6665

67-
import type { SocketBranchParseResult } from './git.mts'
6866
import type { NodeClass } from '../../shadow/npm/arborist/types.mts'
6967
import type { CResult, StringKeyValueObject } from '../../types.mts'
7068
import type { EnvDetails } from '../../utils/package-environment.mts'
@@ -274,28 +272,11 @@ export async function pnpmFix(
274272
continue infoEntriesLoop
275273
}
276274

277-
const activeBranches: SocketBranchParseResult[] = []
278-
if (ciEnv) {
279-
const branchFullName = getSocketBranchFullNameComponent(partialPurlObj)
280-
const branchPurlType = getSocketBranchPurlTypeComponent(partialPurlObj)
281-
for (const pr of openPrs) {
282-
const parsedBranch = ciEnv.branchParser!(pr.headRefName)
283-
if (
284-
branchPurlType === parsedBranch?.type &&
285-
branchFullName === parsedBranch?.fullName
286-
) {
287-
activeBranches.push(parsedBranch)
288-
}
289-
}
290-
if (activeBranches.length) {
291-
debugFn(
292-
`found: ${activeBranches.length} active branches\n`,
293-
activeBranches,
294-
)
295-
} else if (openPrs.length) {
296-
debugFn('miss: 0 active branches found')
297-
}
298-
}
275+
const activeBranches = getActiveBranchesForPackage(
276+
ciEnv,
277+
infoEntry[0],
278+
openPrs,
279+
)
299280

300281
logger.log(`Processing vulns for ${name}:`)
301282
logger.indent()

0 commit comments

Comments
 (0)