Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/commands/fix/fix-env-helpers.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { createSocketBranchParser, getBaseGitBranch } from './git.mts'
import { getGithubEnvRepoInfo, getOpenSocketPrs } from './open-pr.mts'
import constants from '../../constants.mts'

import type { SocketBranchParser } from './git.mts'
import type { GithubRepoInfo, PrMatch } from './open-pr.mts'

export interface CiEnv {
gitEmail: string
gitUser: string
githubToken: string
repoInfo: GithubRepoInfo
baseBranch: string
branchParser: SocketBranchParser
}

export function getCiEnv(): CiEnv | null {
const gitEmail = constants.ENV.SOCKET_CLI_GIT_USER_EMAIL
const gitUser = constants.ENV.SOCKET_CLI_GIT_USER_NAME
const githubToken = constants.ENV.SOCKET_CLI_GITHUB_TOKEN
const isCi = !!(
constants.ENV.CI &&
constants.ENV.GITHUB_ACTIONS &&
constants.ENV.GITHUB_REPOSITORY &&
gitEmail &&
gitUser &&
githubToken
)
return isCi
? {
gitEmail,
gitUser,
githubToken,
repoInfo: getGithubEnvRepoInfo()!,
baseBranch: getBaseGitBranch(),
branchParser: createSocketBranchParser(),
}
: null
}

export async function getOpenPrsForEnvironment(env: CiEnv): Promise<PrMatch[]> {
return env
? await getOpenSocketPrs(env.repoInfo.owner, env.repoInfo.repo, {
author: env.gitUser,
})
: []
}
77 changes: 28 additions & 49 deletions src/commands/fix/npm-fix.mts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import {
} from '@socketsecurity/registry/lib/packages'
import { naturalCompare } from '@socketsecurity/registry/lib/sorts'

import { getCiEnv, getOpenPrsForEnvironment } from './fix-env-helpers.mts'
import {
createSocketBranchParser,
getBaseGitBranch,
getSocketBranchFullNameComponent,
getSocketBranchName,
getSocketBranchPurlTypeComponent,
Expand All @@ -30,8 +29,6 @@ import {
import {
cleanupOpenPrs,
enablePrAutoMerge,
getGithubEnvRepoInfo,
getOpenSocketPrs,
openPr,
prExistForBranch,
setGitRemoteGithubRepoUrl,
Expand Down Expand Up @@ -114,31 +111,10 @@ export async function npmFix(
const { spinner } = constants
const { pkgPath: rootPath } = pkgEnvDetails

// Lazily access constants.ENV properties.
const gitEmail = constants.ENV.SOCKET_CLI_GIT_USER_EMAIL
const gitUser = constants.ENV.SOCKET_CLI_GIT_USER_NAME
const githubToken = constants.ENV.SOCKET_CLI_GITHUB_TOKEN

const isCi = !!(
constants.ENV.CI &&
constants.ENV.GITHUB_ACTIONS &&
constants.ENV.GITHUB_REPOSITORY &&
gitEmail &&
gitUser &&
githubToken
)

const repoInfo = isCi ? getGithubEnvRepoInfo()! : null

spinner?.start()

const openPrs =
// Check repoInfo to make TypeScript happy.
isCi && repoInfo
? await getOpenSocketPrs(repoInfo.owner, repoInfo.repo, {
author: gitUser,
})
: []
const ciEnv = getCiEnv()
const openPrs = ciEnv ? await getOpenPrsForEnvironment(ciEnv) : []

let count = 0

Expand Down Expand Up @@ -180,8 +156,7 @@ export async function npmFix(
return { ok: true, data: { fixed: false } }
}

const baseBranch = isCi ? getBaseGitBranch() : ''
const branchParser = isCi ? createSocketBranchParser() : null
// baseBranch and branchParser are now from env
const workspacePkgJsonPaths = await globWorkspace(
pkgEnvDetails.agent,
rootPath,
Expand Down Expand Up @@ -225,11 +200,11 @@ export async function npmFix(
}

const activeBranches: SocketBranchParseResult[] = []
if (isCi) {
if (ciEnv) {
const branchFullName = getSocketBranchFullNameComponent(partialPurlObj)
const branchPurlType = getSocketBranchPurlTypeComponent(partialPurlObj)
for (const pr of openPrs) {
const parsedBranch = branchParser!(pr.headRefName)
const parsedBranch = ciEnv.branchParser!(pr.headRefName)
if (
branchPurlType === parsedBranch?.type &&
branchFullName === parsedBranch?.fullName
Expand Down Expand Up @@ -280,7 +255,7 @@ export async function npmFix(
const workspace = isWorkspaceRoot
? 'root'
: path.relative(rootPath, pkgPath)
const branchWorkspace = isCi
const branchWorkspace = ciEnv
? getSocketBranchWorkspaceComponent(workspace)
: ''

Expand Down Expand Up @@ -393,9 +368,9 @@ export async function npmFix(
if (!(await editablePkgJson.save({ ignoreWhitespace: true }))) {
debugFn(`skip: ${workspace}/package.json unchanged`)
// Reset things just in case.
if (isCi) {
if (ciEnv) {
// eslint-disable-next-line no-await-in-loop
await gitResetAndClean(baseBranch, cwd)
await gitResetAndClean(ciEnv.baseBranch, cwd)
}
continue infosLoop
}
Expand Down Expand Up @@ -432,7 +407,7 @@ export async function npmFix(
spinner?.stop()

// Check repoInfo to make TypeScript happy.
if (!errored && isCi && repoInfo) {
if (!errored && ciEnv?.repoInfo) {
try {
// eslint-disable-next-line no-await-in-loop
const result = await gitUnstagedModifiedFiles(cwd)
Expand Down Expand Up @@ -464,7 +439,11 @@ export async function npmFix(
let skipPr = false
if (
// eslint-disable-next-line no-await-in-loop
await prExistForBranch(repoInfo.owner, repoInfo.repo, branch)
await prExistForBranch(
ciEnv.repoInfo.owner,
ciEnv.repoInfo.repo,
branch,
)
) {
skipPr = true
debugFn(`skip: branch "${branch}" exists`)
Expand All @@ -481,8 +460,8 @@ export async function npmFix(
moddedFilepaths,
{
cwd,
email: gitEmail,
user: gitUser,
email: ciEnv.gitEmail,
user: ciEnv.gitUser,
},
))
) {
Expand All @@ -493,7 +472,7 @@ export async function npmFix(
}
if (skipPr) {
// eslint-disable-next-line no-await-in-loop
await gitResetAndClean(baseBranch, cwd)
await gitResetAndClean(ciEnv.baseBranch, cwd)
// eslint-disable-next-line no-await-in-loop
const maybeActualTree = await install(arb, { cwd })
if (!maybeActualTree) {
Expand All @@ -507,26 +486,26 @@ export async function npmFix(
// eslint-disable-next-line no-await-in-loop
await Promise.allSettled([
setGitRemoteGithubRepoUrl(
repoInfo.owner,
repoInfo.repo,
githubToken,
ciEnv.repoInfo.owner,
ciEnv.repoInfo.repo,
ciEnv.githubToken!,
cwd,
),
cleanupOpenPrs(repoInfo.owner, repoInfo.repo, {
cleanupOpenPrs(ciEnv.repoInfo.owner, ciEnv.repoInfo.repo, {
newVersion,
purl: oldPurl,
workspace,
}),
])
// eslint-disable-next-line no-await-in-loop
const prResponse = await openPr(
repoInfo.owner,
repoInfo.repo,
ciEnv.repoInfo.owner,
ciEnv.repoInfo.repo,
branch,
oldPurl,
newVersion,
{
baseBranch,
baseBranch: ciEnv.baseBranch,
cwd,
workspace,
},
Expand Down Expand Up @@ -560,10 +539,10 @@ export async function npmFix(
}
}

if (isCi) {
if (ciEnv) {
spinner?.start()
// eslint-disable-next-line no-await-in-loop
await gitResetAndClean(baseBranch, cwd)
await gitResetAndClean(ciEnv.baseBranch, cwd)
// eslint-disable-next-line no-await-in-loop
const maybeActualTree = await install(arb, { cwd })
spinner?.stop()
Expand All @@ -574,7 +553,7 @@ export async function npmFix(
}
}
if (errored) {
if (!isCi) {
if (!ciEnv) {
spinner?.start()
editablePkgJson.update(revertData)
// eslint-disable-next-line no-await-in-loop
Expand Down
Loading