Skip to content

Commit 546fb6b

Browse files
committed
Add better ux around missing env vars
1 parent 2cf3f1a commit 546fb6b

File tree

2 files changed

+109
-14
lines changed

2 files changed

+109
-14
lines changed

src/commands/fix/coana-fix.mts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
55
import { logger } from '@socketsecurity/registry/lib/logger'
66
import { pluralize } from '@socketsecurity/registry/lib/words'
77

8-
import { getFixEnv } from './env-helpers.mts'
8+
import { checkCiEnvVars, getCiEnvInstructions, getFixEnv } from './env-helpers.mts'
99
import { getSocketFixBranchName, getSocketFixCommitMessage } from './git.mts'
1010
import { getSocketFixPrs, openSocketFixPr } from './pull-request.mts'
1111
import { GQL_PR_STATE_OPEN, UNKNOWN_ERROR } from '../../constants.mts'
@@ -100,6 +100,26 @@ export async function coanaFix(
100100
const shouldOpenPrs = fixEnv.isCi && fixEnv.repoInfo
101101

102102
if (!shouldOpenPrs) {
103+
// Inform user about local mode when fixes will be applied.
104+
if (!onlyCompute && ghsas.length) {
105+
const envCheck = checkCiEnvVars()
106+
if (envCheck.present.length) {
107+
// Some CI vars are set but not all - show what's missing.
108+
if (envCheck.missing.length) {
109+
logger.info(
110+
'Running in local mode - fixes will be applied directly to your working directory.\n' +
111+
`Missing environment variables for PR creation: ${joinAnd(envCheck.missing)}`
112+
)
113+
}
114+
} else {
115+
// No CI vars are present - show general local mode message.
116+
logger.info(
117+
'Running in local mode - fixes will be applied directly to your working directory.\n' +
118+
getCiEnvInstructions()
119+
)
120+
}
121+
}
122+
103123
const ids = isAll ? ['all'] : ghsas.slice(0, limit)
104124
if (!ids.length) {
105125
spinner?.stop()
@@ -304,11 +324,24 @@ export async function coanaFix(
304324
}
305325

306326
// Set up git remote.
327+
if (!fixEnv.githubToken) {
328+
logger.error(
329+
'Cannot create pull request: SOCKET_CLI_GITHUB_TOKEN environment variable is not set.\n' +
330+
'Set SOCKET_CLI_GITHUB_TOKEN or GITHUB_TOKEN to enable PR creation.'
331+
)
332+
// eslint-disable-next-line no-await-in-loop
333+
await gitResetAndClean(fixEnv.baseBranch, cwd)
334+
// eslint-disable-next-line no-await-in-loop
335+
await gitCheckoutBranch(fixEnv.baseBranch, cwd)
336+
// eslint-disable-next-line no-await-in-loop
337+
await gitDeleteBranch(branch, cwd)
338+
continue ghsaLoop
339+
}
307340
// eslint-disable-next-line no-await-in-loop
308341
await setGitRemoteGithubRepoUrl(
309342
fixEnv.repoInfo.owner,
310343
fixEnv.repoInfo.repo,
311-
fixEnv.githubToken!,
344+
fixEnv.githubToken,
312345
cwd,
313346
)
314347

src/commands/fix/env-helpers.mts

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { joinAnd } from '@socketsecurity/registry/lib/arrays'
22
import { debugFn, isDebug } from '@socketsecurity/registry/lib/debug'
3+
import { logger } from '@socketsecurity/registry/lib/logger'
34

45
import { getSocketFixPrs } from './pull-request.mts'
56
import constants from '../../constants.mts'
@@ -34,30 +35,91 @@ export interface FixEnv {
3435
repoInfo: RepoInfo | undefined
3536
}
3637

38+
export interface MissingEnvVars {
39+
missing: string[]
40+
present: string[]
41+
}
42+
43+
/**
44+
* Get formatted instructions for setting CI environment variables.
45+
*/
46+
export function getCiEnvInstructions(): string {
47+
return (
48+
'To enable automatic pull request creation, run in CI with these environment variables:\n' +
49+
' - CI=1\n' +
50+
' - SOCKET_CLI_GITHUB_TOKEN=<your-github-token>\n' +
51+
' - SOCKET_CLI_GIT_USER_NAME=<git-username>\n' +
52+
' - SOCKET_CLI_GIT_USER_EMAIL=<git-email>'
53+
)
54+
}
55+
56+
/**
57+
* Check which required CI environment variables are missing.
58+
* Returns lists of missing and present variables.
59+
*/
60+
export function checkCiEnvVars(): MissingEnvVars {
61+
const { CI, SOCKET_CLI_GIT_USER_EMAIL, SOCKET_CLI_GIT_USER_NAME, SOCKET_CLI_GITHUB_TOKEN } = constants.ENV
62+
63+
const missing: string[] = []
64+
const present: string[] = []
65+
66+
if (CI) {
67+
present.push('CI')
68+
} else {
69+
missing.push('CI')
70+
}
71+
72+
if (SOCKET_CLI_GIT_USER_EMAIL) {
73+
present.push('SOCKET_CLI_GIT_USER_EMAIL')
74+
} else {
75+
missing.push('SOCKET_CLI_GIT_USER_EMAIL')
76+
}
77+
78+
if (SOCKET_CLI_GIT_USER_NAME) {
79+
present.push('SOCKET_CLI_GIT_USER_NAME')
80+
} else {
81+
missing.push('SOCKET_CLI_GIT_USER_NAME')
82+
}
83+
84+
if (SOCKET_CLI_GITHUB_TOKEN) {
85+
present.push('SOCKET_CLI_GITHUB_TOKEN')
86+
} else {
87+
missing.push('SOCKET_CLI_GITHUB_TOKEN (or GITHUB_TOKEN)')
88+
}
89+
90+
return { missing, present }
91+
}
92+
3793
export async function getFixEnv(): Promise<FixEnv> {
3894
const baseBranch = await getBaseBranch()
3995
const gitEmail = constants.ENV.SOCKET_CLI_GIT_USER_EMAIL
4096
const gitUser = constants.ENV.SOCKET_CLI_GIT_USER_NAME
4197
const githubToken = constants.ENV.SOCKET_CLI_GITHUB_TOKEN
4298
const isCi = !!(constants.ENV.CI && gitEmail && gitUser && githubToken)
4399

44-
if (
45-
// If isCi is false,
46-
!isCi &&
47-
// but some CI checks are passing,
48-
(constants.ENV.CI || gitEmail || gitUser || githubToken) &&
100+
const envCheck = checkCiEnvVars()
101+
102+
// Provide clear feedback about missing environment variables.
103+
if (constants.ENV.CI && envCheck.missing.length > 1) {
104+
// CI is set but other required vars are missing.
105+
const missingExceptCi = envCheck.missing.filter(v => v !== 'CI')
106+
if (missingExceptCi.length) {
107+
logger.warn(
108+
`CI mode detected, but pull request creation is disabled due to missing environment variables:\n` +
109+
` Missing: ${joinAnd(missingExceptCi)}\n` +
110+
` Set these variables to enable automatic pull request creation.`
111+
)
112+
}
113+
} else if (
114+
// If not in CI but some CI-related env vars are set.
115+
!constants.ENV.CI &&
116+
envCheck.present.length &&
49117
// then log about it when in debug mode.
50118
isDebug('notice')
51119
) {
52-
const envVars = [
53-
...(constants.ENV.CI ? [] : ['process.env.CI']),
54-
...(gitEmail ? [] : ['process.env.SOCKET_CLI_GIT_USER_EMAIL']),
55-
...(gitUser ? [] : ['process.env.SOCKET_CLI_GIT_USER_NAME']),
56-
...(githubToken ? [] : ['process.env.GITHUB_TOKEN']),
57-
]
58120
debugFn(
59121
'notice',
60-
`miss: fixEnv.isCi is false, expected ${joinAnd(envVars)} to be set`,
122+
`miss: fixEnv.isCi is false, expected ${joinAnd(envCheck.missing)} to be set`,
61123
)
62124
}
63125

0 commit comments

Comments
 (0)