|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +import { stdin as inputText, stdout as output } from 'node:process' |
| 4 | +import * as readline from 'node:readline/promises' |
| 5 | + |
| 6 | +import chalk from 'chalk' |
| 7 | +import meow from 'meow' |
| 8 | +import open from 'open' |
| 9 | +import ora from 'ora' |
| 10 | +import { ErrorWithCause } from 'pony-cause' |
| 11 | + |
| 12 | +import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' |
| 13 | +import { prepareFlags } from '../../utils/flags.js' |
| 14 | +import { printFlagList } from '../../utils/formatting.js' |
| 15 | +import { createDebugLogger } from '../../utils/misc.js' |
| 16 | +import { getPackageFilesFullScans } from '../../utils/path-resolve.js' |
| 17 | +import { getDefaultKey, setupSdk } from '../../utils/sdk.js' |
| 18 | + |
| 19 | +/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */ |
| 20 | +export const create = { |
| 21 | + description: 'Create a scan', |
| 22 | + async run (argv, importMeta, { parentName }) { |
| 23 | + const name = parentName + ' create' |
| 24 | + |
| 25 | + const input = await setupCommand(name, create.description, argv, importMeta) |
| 26 | + if (input) { |
| 27 | + const spinnerText = 'Creating a scan... \n' |
| 28 | + const spinner = ora(spinnerText).start() |
| 29 | + |
| 30 | + await createFullScan(input, spinner) |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +const createFullScanFlags = prepareFlags({ |
| 36 | + repo: { |
| 37 | + type: 'string', |
| 38 | + shortFlag: 'r', |
| 39 | + default: '', |
| 40 | + description: 'Repository name', |
| 41 | + }, |
| 42 | + branch: { |
| 43 | + type: 'string', |
| 44 | + shortFlag: 'b', |
| 45 | + default: '', |
| 46 | + description: 'Branch name', |
| 47 | + }, |
| 48 | + commitMessage: { |
| 49 | + type: 'string', |
| 50 | + shortFlag: 'm', |
| 51 | + default: '', |
| 52 | + description: 'Commit message', |
| 53 | + }, |
| 54 | + commitHash: { |
| 55 | + type: 'string', |
| 56 | + shortFlag: 'ch', |
| 57 | + default: '', |
| 58 | + description: 'Commit hash', |
| 59 | + }, |
| 60 | + pullRequest: { |
| 61 | + type: 'number', |
| 62 | + shortFlag: 'pr', |
| 63 | + description: 'Commit hash', |
| 64 | + }, |
| 65 | + committers: { |
| 66 | + type: 'string', |
| 67 | + shortFlag: 'c', |
| 68 | + default: '', |
| 69 | + description: 'Committers', |
| 70 | + }, |
| 71 | + defaultBranch: { |
| 72 | + type: 'boolean', |
| 73 | + shortFlag: 'db', |
| 74 | + default: false, |
| 75 | + description: 'Make default branch', |
| 76 | + }, |
| 77 | + pendingHead: { |
| 78 | + type: 'boolean', |
| 79 | + shortFlag: 'ph', |
| 80 | + default: false, |
| 81 | + description: 'Set as pending head', |
| 82 | + }, |
| 83 | + tmp: { |
| 84 | + type: 'boolean', |
| 85 | + shortFlag: 't', |
| 86 | + default: false, |
| 87 | + description: 'Set the visibility (true/false) of the scan in your dashboard', |
| 88 | + } |
| 89 | +}) |
| 90 | + |
| 91 | +// Internal functions |
| 92 | + |
| 93 | +/** |
| 94 | + * @typedef CommandContext |
| 95 | + * @property {string} orgSlug |
| 96 | + * @property {string} repoName |
| 97 | + * @property {string} branchName |
| 98 | + * @property {string} committers |
| 99 | + * @property {string} commitMessage |
| 100 | + * @property {string} commitHash |
| 101 | + * @property {number | undefined} pullRequest |
| 102 | + * @property {boolean} defaultBranch |
| 103 | + * @property {boolean} pendingHead |
| 104 | + * @property {boolean} tmp |
| 105 | + * @property {string[]} packagePaths |
| 106 | + */ |
| 107 | + |
| 108 | +/** |
| 109 | + * @param {string} name |
| 110 | + * @param {string} description |
| 111 | + * @param {readonly string[]} argv |
| 112 | + * @param {ImportMeta} importMeta |
| 113 | + * @returns {Promise<void|CommandContext>} |
| 114 | + */ |
| 115 | +async function setupCommand (name, description, argv, importMeta) { |
| 116 | + const flags = { |
| 117 | + ...createFullScanFlags |
| 118 | + } |
| 119 | + |
| 120 | + const cli = meow(` |
| 121 | + Usage |
| 122 | + $ ${name} [...options] |
| 123 | +
|
| 124 | + Options |
| 125 | + ${printFlagList(flags, 6)} |
| 126 | +
|
| 127 | + Examples |
| 128 | + $ ${name} --org=FakeOrg --repo=test-repo --branch=main ./package.json |
| 129 | + `, { |
| 130 | + argv, |
| 131 | + description, |
| 132 | + importMeta, |
| 133 | + flags |
| 134 | + }) |
| 135 | + |
| 136 | + const { |
| 137 | + repo: repoName, |
| 138 | + branch: branchName, |
| 139 | + commitMessage, |
| 140 | + defaultBranch, |
| 141 | + pendingHead, |
| 142 | + tmp, |
| 143 | + committers, |
| 144 | + commitHash, |
| 145 | + pullRequest |
| 146 | + } = cli.flags |
| 147 | + |
| 148 | + if (!cli.input[0]) { |
| 149 | + cli.showHelp() |
| 150 | + return |
| 151 | + } |
| 152 | + |
| 153 | + const [orgSlug = ''] = cli.input |
| 154 | + |
| 155 | + const cwd = process.cwd() |
| 156 | + const socketSdk = await setupSdk() |
| 157 | + const supportedFiles = await socketSdk.getReportSupportedFiles() |
| 158 | + .then(res => { |
| 159 | + if (!res.success) handleUnsuccessfulApiResponse('getReportSupportedFiles', res, ora()) |
| 160 | + return res.data |
| 161 | + }).catch( |
| 162 | + /** @type {(cause: Error) => never} */ |
| 163 | + (cause) => { |
| 164 | + throw new ErrorWithCause('Failed getting supported files for report', { cause }) |
| 165 | + }) |
| 166 | + const debugLog = createDebugLogger(false) |
| 167 | + const packagePaths = await getPackageFilesFullScans(cwd, cli.input, supportedFiles, debugLog) |
| 168 | + |
| 169 | + if (!repoName || !branchName || !packagePaths.length) { |
| 170 | + console.error(`${chalk.bgRed('Input error')}: Please provide the required fields: \n |
| 171 | +- Repository name using --repo, \n |
| 172 | +- Branch name using --branch \n |
| 173 | +- At least one file path (e.g. ./package.json) .\n`) |
| 174 | + cli.showHelp() |
| 175 | + return |
| 176 | + } |
| 177 | + |
| 178 | + return { |
| 179 | + orgSlug, |
| 180 | + repoName, |
| 181 | + branchName, |
| 182 | + commitMessage, |
| 183 | + defaultBranch, |
| 184 | + pendingHead, |
| 185 | + tmp, |
| 186 | + packagePaths, |
| 187 | + commitHash, |
| 188 | + committers, |
| 189 | + pullRequest |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +/** |
| 194 | + * @typedef FullScanData |
| 195 | + * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'CreateOrgFullScan'>["data"]} data |
| 196 | + */ |
| 197 | + |
| 198 | +/** |
| 199 | + * @param {CommandContext} input |
| 200 | + * @param {import('ora').Ora} spinner |
| 201 | + * @returns {Promise<void|FullScanData>} |
| 202 | + */ |
| 203 | +async function createFullScan (input, spinner) { |
| 204 | + const socketSdk = await setupSdk(getDefaultKey()) |
| 205 | + const { |
| 206 | + orgSlug, |
| 207 | + repoName, |
| 208 | + branchName, |
| 209 | + commitMessage, |
| 210 | + defaultBranch, |
| 211 | + pendingHead, |
| 212 | + tmp, |
| 213 | + packagePaths |
| 214 | + } = input |
| 215 | + |
| 216 | + const result = await handleApiCall(socketSdk.createOrgFullScan(orgSlug, { |
| 217 | + repo: repoName, |
| 218 | + branch: branchName, |
| 219 | + commit_message: commitMessage, |
| 220 | + make_default_branch: defaultBranch, |
| 221 | + set_as_pending_head: pendingHead, |
| 222 | + tmp |
| 223 | + }, packagePaths), 'Creating scan') |
| 224 | + |
| 225 | + if (!result.success) { |
| 226 | + return handleUnsuccessfulApiResponse('CreateOrgFullScan', result, spinner) |
| 227 | + } |
| 228 | + spinner.stop() |
| 229 | + |
| 230 | + console.log('\n✅ Scan created successfully \n') |
| 231 | + const link = chalk.hex('#00FFFF').underline(`${result.data.html_report_url}`) |
| 232 | + console.log(`Available at: ${link} \n`) |
| 233 | + |
| 234 | + const rl = readline.createInterface({ input: inputText, output }) |
| 235 | + |
| 236 | + const answer = await rl.question('Would you like to open it in your browser? (y/n) ') |
| 237 | + |
| 238 | + answer.toLowerCase() === 'y' && open(`${result.data.html_report_url}`) |
| 239 | + |
| 240 | + rl.close() |
| 241 | + |
| 242 | + return { |
| 243 | + data: result.data |
| 244 | + } |
| 245 | +} |
0 commit comments