@@ -14,9 +14,8 @@ import {
1414} from '@socketsecurity/registry/lib/packages'
1515import { naturalCompare } from '@socketsecurity/registry/lib/sorts'
1616
17+ import { getCiEnv , getOpenPrsForEnvironment } from './fix-env-helpers.mts'
1718import {
18- createSocketBranchParser ,
19- getBaseGitBranch ,
2019 getSocketBranchFullNameComponent ,
2120 getSocketBranchName ,
2221 getSocketBranchPurlTypeComponent ,
@@ -30,8 +29,6 @@ import {
3029import {
3130 cleanupOpenPrs ,
3231 enablePrAutoMerge ,
33- getGithubEnvRepoInfo ,
34- getOpenSocketPrs ,
3532 openPr ,
3633 prExistForBranch ,
3734 setGitRemoteGithubRepoUrl ,
@@ -114,31 +111,10 @@ export async function npmFix(
114111 const { spinner } = constants
115112 const { pkgPath : rootPath } = pkgEnvDetails
116113
117- // Lazily access constants.ENV properties.
118- const gitEmail = constants . ENV . SOCKET_CLI_GIT_USER_EMAIL
119- const gitUser = constants . ENV . SOCKET_CLI_GIT_USER_NAME
120- const githubToken = constants . ENV . SOCKET_CLI_GITHUB_TOKEN
121-
122- const isCi = ! ! (
123- constants . ENV . CI &&
124- constants . ENV . GITHUB_ACTIONS &&
125- constants . ENV . GITHUB_REPOSITORY &&
126- gitEmail &&
127- gitUser &&
128- githubToken
129- )
130-
131- const repoInfo = isCi ? getGithubEnvRepoInfo ( ) ! : null
132-
133114 spinner ?. start ( )
134115
135- const openPrs =
136- // Check repoInfo to make TypeScript happy.
137- isCi && repoInfo
138- ? await getOpenSocketPrs ( repoInfo . owner , repoInfo . repo , {
139- author : gitUser ,
140- } )
141- : [ ]
116+ const ciEnv = getCiEnv ( )
117+ const openPrs = ciEnv ? await getOpenPrsForEnvironment ( ciEnv ) : [ ]
142118
143119 let count = 0
144120
@@ -180,8 +156,7 @@ export async function npmFix(
180156 return { ok : true , data : { fixed : false } }
181157 }
182158
183- const baseBranch = isCi ? getBaseGitBranch ( ) : ''
184- const branchParser = isCi ? createSocketBranchParser ( ) : null
159+ // baseBranch and branchParser are now from env
185160 const workspacePkgJsonPaths = await globWorkspace (
186161 pkgEnvDetails . agent ,
187162 rootPath ,
@@ -225,11 +200,11 @@ export async function npmFix(
225200 }
226201
227202 const activeBranches : SocketBranchParseResult [ ] = [ ]
228- if ( isCi ) {
203+ if ( ciEnv ) {
229204 const branchFullName = getSocketBranchFullNameComponent ( partialPurlObj )
230205 const branchPurlType = getSocketBranchPurlTypeComponent ( partialPurlObj )
231206 for ( const pr of openPrs ) {
232- const parsedBranch = branchParser ! ( pr . headRefName )
207+ const parsedBranch = ciEnv . branchParser ! ( pr . headRefName )
233208 if (
234209 branchPurlType === parsedBranch ?. type &&
235210 branchFullName === parsedBranch ?. fullName
@@ -280,7 +255,7 @@ export async function npmFix(
280255 const workspace = isWorkspaceRoot
281256 ? 'root'
282257 : path . relative ( rootPath , pkgPath )
283- const branchWorkspace = isCi
258+ const branchWorkspace = ciEnv
284259 ? getSocketBranchWorkspaceComponent ( workspace )
285260 : ''
286261
@@ -393,9 +368,9 @@ export async function npmFix(
393368 if ( ! ( await editablePkgJson . save ( { ignoreWhitespace : true } ) ) ) {
394369 debugFn ( `skip: ${ workspace } /package.json unchanged` )
395370 // Reset things just in case.
396- if ( isCi ) {
371+ if ( ciEnv ) {
397372 // eslint-disable-next-line no-await-in-loop
398- await gitResetAndClean ( baseBranch , cwd )
373+ await gitResetAndClean ( ciEnv . baseBranch , cwd )
399374 }
400375 continue infosLoop
401376 }
@@ -432,7 +407,7 @@ export async function npmFix(
432407 spinner ?. stop ( )
433408
434409 // Check repoInfo to make TypeScript happy.
435- if ( ! errored && isCi && repoInfo ) {
410+ if ( ! errored && ciEnv ?. repoInfo ) {
436411 try {
437412 // eslint-disable-next-line no-await-in-loop
438413 const result = await gitUnstagedModifiedFiles ( cwd )
@@ -464,7 +439,11 @@ export async function npmFix(
464439 let skipPr = false
465440 if (
466441 // eslint-disable-next-line no-await-in-loop
467- await prExistForBranch ( repoInfo . owner , repoInfo . repo , branch )
442+ await prExistForBranch (
443+ ciEnv . repoInfo . owner ,
444+ ciEnv . repoInfo . repo ,
445+ branch ,
446+ )
468447 ) {
469448 skipPr = true
470449 debugFn ( `skip: branch "${ branch } " exists` )
@@ -481,8 +460,8 @@ export async function npmFix(
481460 moddedFilepaths ,
482461 {
483462 cwd,
484- email : gitEmail ,
485- user : gitUser ,
463+ email : ciEnv . gitEmail ,
464+ user : ciEnv . gitUser ,
486465 } ,
487466 ) )
488467 ) {
@@ -493,7 +472,7 @@ export async function npmFix(
493472 }
494473 if ( skipPr ) {
495474 // eslint-disable-next-line no-await-in-loop
496- await gitResetAndClean ( baseBranch , cwd )
475+ await gitResetAndClean ( ciEnv . baseBranch , cwd )
497476 // eslint-disable-next-line no-await-in-loop
498477 const maybeActualTree = await install ( arb , { cwd } )
499478 if ( ! maybeActualTree ) {
@@ -507,26 +486,26 @@ export async function npmFix(
507486 // eslint-disable-next-line no-await-in-loop
508487 await Promise . allSettled ( [
509488 setGitRemoteGithubRepoUrl (
510- repoInfo . owner ,
511- repoInfo . repo ,
512- githubToken ,
489+ ciEnv . repoInfo . owner ,
490+ ciEnv . repoInfo . repo ,
491+ ciEnv . githubToken ! ,
513492 cwd ,
514493 ) ,
515- cleanupOpenPrs ( repoInfo . owner , repoInfo . repo , {
494+ cleanupOpenPrs ( ciEnv . repoInfo . owner , ciEnv . repoInfo . repo , {
516495 newVersion,
517496 purl : oldPurl ,
518497 workspace,
519498 } ) ,
520499 ] )
521500 // eslint-disable-next-line no-await-in-loop
522501 const prResponse = await openPr (
523- repoInfo . owner ,
524- repoInfo . repo ,
502+ ciEnv . repoInfo . owner ,
503+ ciEnv . repoInfo . repo ,
525504 branch ,
526505 oldPurl ,
527506 newVersion ,
528507 {
529- baseBranch,
508+ baseBranch : ciEnv . baseBranch ,
530509 cwd,
531510 workspace,
532511 } ,
@@ -560,10 +539,10 @@ export async function npmFix(
560539 }
561540 }
562541
563- if ( isCi ) {
542+ if ( ciEnv ) {
564543 spinner ?. start ( )
565544 // eslint-disable-next-line no-await-in-loop
566- await gitResetAndClean ( baseBranch , cwd )
545+ await gitResetAndClean ( ciEnv . baseBranch , cwd )
567546 // eslint-disable-next-line no-await-in-loop
568547 const maybeActualTree = await install ( arb , { cwd } )
569548 spinner ?. stop ( )
@@ -574,7 +553,7 @@ export async function npmFix(
574553 }
575554 }
576555 if ( errored ) {
577- if ( ! isCi ) {
556+ if ( ! ciEnv ) {
578557 spinner ?. start ( )
579558 editablePkgJson . update ( revertData )
580559 // eslint-disable-next-line no-await-in-loop
0 commit comments