|
1 | 1 | import { joinAnd } from '@socketsecurity/registry/lib/arrays' |
2 | 2 | import { debugFn, isDebug } from '@socketsecurity/registry/lib/debug' |
| 3 | +import { logger } from '@socketsecurity/registry/lib/logger' |
3 | 4 |
|
4 | 5 | import { getSocketFixPrs } from './pull-request.mts' |
5 | 6 | import constants from '../../constants.mts' |
@@ -34,30 +35,91 @@ export interface FixEnv { |
34 | 35 | repoInfo: RepoInfo | undefined |
35 | 36 | } |
36 | 37 |
|
| 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 | + |
37 | 93 | export async function getFixEnv(): Promise<FixEnv> { |
38 | 94 | const baseBranch = await getBaseBranch() |
39 | 95 | const gitEmail = constants.ENV.SOCKET_CLI_GIT_USER_EMAIL |
40 | 96 | const gitUser = constants.ENV.SOCKET_CLI_GIT_USER_NAME |
41 | 97 | const githubToken = constants.ENV.SOCKET_CLI_GITHUB_TOKEN |
42 | 98 | const isCi = !!(constants.ENV.CI && gitEmail && gitUser && githubToken) |
43 | 99 |
|
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 && |
49 | 117 | // then log about it when in debug mode. |
50 | 118 | isDebug('notice') |
51 | 119 | ) { |
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 | | - ] |
58 | 120 | debugFn( |
59 | 121 | '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`, |
61 | 123 | ) |
62 | 124 | } |
63 | 125 |
|
|
0 commit comments