Skip to content

Commit 6b3d2b2

Browse files
authored
Abstract ciEnv (#650)
1 parent 206c76d commit 6b3d2b2

File tree

3 files changed

+105
-100
lines changed

3 files changed

+105
-100
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { createSocketBranchParser, getBaseGitBranch } from './git.mts'
2+
import { getGithubEnvRepoInfo, getOpenSocketPrs } from './open-pr.mts'
3+
import constants from '../../constants.mts'
4+
5+
import type { SocketBranchParser } from './git.mts'
6+
import type { GithubRepoInfo, PrMatch } from './open-pr.mts'
7+
8+
export interface CiEnv {
9+
gitEmail: string
10+
gitUser: string
11+
githubToken: string
12+
repoInfo: GithubRepoInfo
13+
baseBranch: string
14+
branchParser: SocketBranchParser
15+
}
16+
17+
export function getCiEnv(): CiEnv | null {
18+
const gitEmail = constants.ENV.SOCKET_CLI_GIT_USER_EMAIL
19+
const gitUser = constants.ENV.SOCKET_CLI_GIT_USER_NAME
20+
const githubToken = constants.ENV.SOCKET_CLI_GITHUB_TOKEN
21+
const isCi = !!(
22+
constants.ENV.CI &&
23+
constants.ENV.GITHUB_ACTIONS &&
24+
constants.ENV.GITHUB_REPOSITORY &&
25+
gitEmail &&
26+
gitUser &&
27+
githubToken
28+
)
29+
return isCi
30+
? {
31+
gitEmail,
32+
gitUser,
33+
githubToken,
34+
repoInfo: getGithubEnvRepoInfo()!,
35+
baseBranch: getBaseGitBranch(),
36+
branchParser: createSocketBranchParser(),
37+
}
38+
: null
39+
}
40+
41+
export async function getOpenPrsForEnvironment(env: CiEnv): Promise<PrMatch[]> {
42+
return env
43+
? await getOpenSocketPrs(env.repoInfo.owner, env.repoInfo.repo, {
44+
author: env.gitUser,
45+
})
46+
: []
47+
}

src/commands/fix/npm-fix.mts

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ import {
1414
} from '@socketsecurity/registry/lib/packages'
1515
import { naturalCompare } from '@socketsecurity/registry/lib/sorts'
1616

17+
import { getCiEnv, getOpenPrsForEnvironment } from './fix-env-helpers.mts'
1718
import {
18-
createSocketBranchParser,
19-
getBaseGitBranch,
2019
getSocketBranchFullNameComponent,
2120
getSocketBranchName,
2221
getSocketBranchPurlTypeComponent,
@@ -30,8 +29,6 @@ import {
3029
import {
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

Comments
 (0)