Skip to content

Commit db2fecb

Browse files
authored
Have open prs count towards fix limit (#617)
1 parent 7ce6ae8 commit db2fecb

File tree

2 files changed

+60
-37
lines changed

2 files changed

+60
-37
lines changed

src/commands/fix/npm-fix.mts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
cleanupOpenPrs,
2727
enablePrAutoMerge,
2828
getGitHubEnvRepoInfo,
29+
getOpenSocketPrs,
2930
openPr,
3031
prExistForBranch,
3132
setGitRemoteGitHubRepoUrl,
@@ -51,6 +52,7 @@ import { applyRange } from '../../utils/semver.mts'
5152
import { getCveInfoFromAlertsMap } from '../../utils/socket-package-alert.mts'
5253
import { idToPurl } from '../../utils/spec.mts'
5354

55+
import type { GitHubRepoInfo } from './open-pr.mts'
5456
import type {
5557
ArboristInstance,
5658
NodeClass,
@@ -106,10 +108,30 @@ export async function npmFix(
106108
): Promise<CResult<{ fixed: boolean }>> {
107109
// Lazily access constants.spinner.
108110
const { spinner } = constants
111+
const { pkgPath: rootPath } = pkgEnvDetails
112+
113+
// Lazily access constants.ENV properties.
114+
const token = constants.ENV.SOCKET_CLI_GITHUB_TOKEN
115+
const isCi = !!(
116+
constants.ENV.CI &&
117+
constants.ENV.GITHUB_ACTIONS &&
118+
constants.ENV.GITHUB_REPOSITORY &&
119+
token
120+
)
109121

110122
spinner?.start()
111123

112-
const { pkgPath: rootPath } = pkgEnvDetails
124+
let count = 0
125+
let repoInfo: GitHubRepoInfo | null = null
126+
if (isCi) {
127+
repoInfo = getGitHubEnvRepoInfo()!
128+
count += (
129+
await getOpenSocketPrs(repoInfo.owner, repoInfo.repo, {
130+
// Lazily access constants.ENV.SOCKET_CLI_GIT_USER_NAME.
131+
author: constants.ENV.SOCKET_CLI_GIT_USER_NAME,
132+
})
133+
).length
134+
}
113135

114136
const arb = new Arborist({
115137
path: rootPath,
@@ -126,7 +148,7 @@ export async function npmFix(
126148
: await getAlertsMapFromArborist(arb, getAlertsMapOptions({ limit }))
127149
} catch (e) {
128150
spinner?.stop()
129-
debugFn('catch: API error\n', e)
151+
debugFn('catch: PURL API\n', e)
130152
return {
131153
ok: false,
132154
message: 'API Error',
@@ -141,14 +163,6 @@ export async function npmFix(
141163
return { ok: true, data: { fixed: false } }
142164
}
143165

144-
// Lazily access constants.ENV properties.
145-
const token = constants.ENV.SOCKET_CLI_GITHUB_TOKEN
146-
const isCi = !!(
147-
constants.ENV.CI &&
148-
constants.ENV.GITHUB_ACTIONS &&
149-
constants.ENV.GITHUB_REPOSITORY &&
150-
token
151-
)
152166
const baseBranch = isCi ? getBaseGitBranch() : ''
153167
const workspacePkgJsonPaths = await globWorkspace(
154168
pkgEnvDetails.agent,
@@ -177,8 +191,6 @@ export async function npmFix(
177191

178192
spinner?.stop()
179193

180-
let count = 0
181-
182194
infoEntriesLoop: for (
183195
let i = 0, { length } = sortedInfoEntries;
184196
i < length;
@@ -380,13 +392,12 @@ export async function npmFix(
380392
continue infosLoop
381393
}
382394

383-
const repoInfo = getGitHubEnvRepoInfo()!
384395
const branch = getSocketBranchName(oldPurl, newVersion, workspace)
385396

386397
let skipPr = false
387398
if (
388399
// eslint-disable-next-line no-await-in-loop
389-
await prExistForBranch(repoInfo.owner, repoInfo.repo, branch)
400+
await prExistForBranch(repoInfo!.owner, repoInfo!.repo, branch)
390401
) {
391402
skipPr = true
392403
debugFn(`skip: branch "${branch}" exists`)
@@ -427,20 +438,20 @@ export async function npmFix(
427438
// eslint-disable-next-line no-await-in-loop
428439
await Promise.allSettled([
429440
setGitRemoteGitHubRepoUrl(
430-
repoInfo.owner,
431-
repoInfo.repo,
441+
repoInfo!.owner,
442+
repoInfo!.repo,
432443
token,
433444
cwd,
434445
),
435-
cleanupOpenPrs(repoInfo.owner, repoInfo.repo, newVersion, {
446+
cleanupOpenPrs(repoInfo!.owner, repoInfo!.repo, newVersion, {
436447
purl: oldPurl,
437448
workspace,
438449
}),
439450
])
440451
// eslint-disable-next-line no-await-in-loop
441452
const prResponse = await openPr(
442-
repoInfo.owner,
443-
repoInfo.repo,
453+
repoInfo!.owner,
454+
repoInfo!.repo,
444455
branch,
445456
oldPurl,
446457
newVersion,

src/commands/fix/pnpm-fix.mts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
cleanupOpenPrs,
2828
enablePrAutoMerge,
2929
getGitHubEnvRepoInfo,
30+
getOpenSocketPrs,
3031
openPr,
3132
prExistForBranch,
3233
setGitRemoteGitHubRepoUrl,
@@ -60,6 +61,7 @@ import { applyRange } from '../../utils/semver.mts'
6061
import { getCveInfoFromAlertsMap } from '../../utils/socket-package-alert.mts'
6162
import { idToPurl } from '../../utils/spec.mts'
6263

64+
import type { GitHubRepoInfo } from './open-pr.mts'
6365
import type { NodeClass } from '../../shadow/npm/arborist/types.mts'
6466
import type { CResult, StringKeyValueObject } from '../../types.mts'
6567
import type { EnvDetails } from '../../utils/package-environment.mts'
@@ -138,8 +140,29 @@ export async function pnpmFix(
138140
const { spinner } = constants
139141
const { pkgPath: rootPath } = pkgEnvDetails
140142

143+
// Lazily access constants.ENV properties.
144+
const token = constants.ENV.SOCKET_CLI_GITHUB_TOKEN
145+
const isCi = !!(
146+
constants.ENV.CI &&
147+
constants.ENV.GITHUB_ACTIONS &&
148+
constants.ENV.GITHUB_REPOSITORY &&
149+
token
150+
)
151+
141152
spinner?.start()
142153

154+
let count = 0
155+
let repoInfo: GitHubRepoInfo | null = null
156+
if (isCi) {
157+
repoInfo = getGitHubEnvRepoInfo()!
158+
count += (
159+
await getOpenSocketPrs(repoInfo.owner, repoInfo.repo, {
160+
// Lazily access constants.ENV.SOCKET_CLI_GIT_USER_NAME.
161+
author: constants.ENV.SOCKET_CLI_GIT_USER_NAME,
162+
})
163+
).length
164+
}
165+
143166
let actualTree: NodeClass | undefined
144167
const lockfilePath = path.join(rootPath, 'pnpm-lock.yaml')
145168
let lockfileContent = await readPnpmLockfile(lockfilePath)
@@ -202,7 +225,7 @@ export async function pnpmFix(
202225
)
203226
} catch (e) {
204227
spinner?.stop()
205-
debugFn('fail: PURL API\n', e)
228+
debugFn('catch: PURL API\n', e)
206229
return {
207230
ok: false,
208231
message: 'API Error',
@@ -217,14 +240,6 @@ export async function pnpmFix(
217240
return { ok: true, data: { fixed: false } }
218241
}
219242

220-
// Lazily access constants.ENV properties.
221-
const token = constants.ENV.SOCKET_CLI_GITHUB_TOKEN
222-
const isCi = !!(
223-
constants.ENV.CI &&
224-
constants.ENV.GITHUB_ACTIONS &&
225-
constants.ENV.GITHUB_REPOSITORY &&
226-
token
227-
)
228243
const baseBranch = isCi ? getBaseGitBranch() : ''
229244
const workspacePkgJsonPaths = await globWorkspace(
230245
pkgEnvDetails.agent,
@@ -252,8 +267,6 @@ export async function pnpmFix(
252267

253268
spinner?.stop()
254269

255-
let count = 0
256-
257270
infoEntriesLoop: for (
258271
let i = 0, { length } = sortedInfoEntries;
259272
i < length;
@@ -545,12 +558,11 @@ export async function pnpmFix(
545558
continue infosLoop
546559
}
547560

548-
const repoInfo = getGitHubEnvRepoInfo()!
549561
const branch = getSocketBranchName(oldPurl, newVersion, workspace)
550562
let skipPr = false
551563
if (
552564
// eslint-disable-next-line no-await-in-loop
553-
await prExistForBranch(repoInfo.owner, repoInfo.repo, branch)
565+
await prExistForBranch(repoInfo!.owner, repoInfo!.repo, branch)
554566
) {
555567
skipPr = true
556568
debugFn(`skip: branch "${branch}" exists`)
@@ -599,20 +611,20 @@ export async function pnpmFix(
599611
// eslint-disable-next-line no-await-in-loop
600612
await Promise.allSettled([
601613
setGitRemoteGitHubRepoUrl(
602-
repoInfo.owner,
603-
repoInfo.repo,
614+
repoInfo!.owner,
615+
repoInfo!.repo,
604616
token,
605617
cwd,
606618
),
607-
cleanupOpenPrs(repoInfo.owner, repoInfo.repo, newVersion, {
619+
cleanupOpenPrs(repoInfo!.owner, repoInfo!.repo, newVersion, {
608620
purl: oldPurl,
609621
workspace,
610622
}),
611623
])
612624
// eslint-disable-next-line no-await-in-loop
613625
const prResponse = await openPr(
614-
repoInfo.owner,
615-
repoInfo.repo,
626+
repoInfo!.owner,
627+
repoInfo!.repo,
616628
branch,
617629
oldPurl,
618630
newVersion,

0 commit comments

Comments
 (0)