|
| 1 | +import { exec, execFile } from 'child_process'; |
| 2 | +import { promisify } from 'util'; |
| 3 | +import { detectRepoName } from '../../scm/index.js'; |
| 4 | +import { startScan } from '../../scans/startScan.js'; |
| 5 | + |
| 6 | +const execAsync = promisify(exec); |
| 7 | +const execFileAsync = promisify(execFile); |
| 8 | + |
| 9 | +// Mirrors splitGlobs in src/index.js (not exported) — preserves commas inside {} brace expansions |
| 10 | +function splitGlobs(input) { |
| 11 | + const parts = []; |
| 12 | + let current = ''; |
| 13 | + let depth = 0; |
| 14 | + for (const ch of String(input)) { |
| 15 | + if (ch === '{') depth++; |
| 16 | + else if (ch === '}') depth--; |
| 17 | + if (ch === ',' && depth === 0) { |
| 18 | + parts.push(current.trim()); |
| 19 | + current = ''; |
| 20 | + } else { |
| 21 | + current += ch; |
| 22 | + } |
| 23 | + } |
| 24 | + if (current.trim()) parts.push(current.trim()); |
| 25 | + return parts.filter(Boolean); |
| 26 | +} |
| 27 | + |
| 28 | +async function resolveCurrentBranch() { |
| 29 | + try { |
| 30 | + const { stdout } = await execAsync('git rev-parse --abbrev-ref HEAD'); |
| 31 | + return stdout.trim(); |
| 32 | + } catch { |
| 33 | + return null; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +async function resolveRemoteCommit(branch) { |
| 38 | + try { |
| 39 | + const { stdout } = await execFileAsync('git', ['ls-remote', 'origin', `refs/heads/${branch}`]); |
| 40 | + const sha = stdout.trim().split(/\s+/)[0]; |
| 41 | + if (!sha) { |
| 42 | + const err = new Error(`Branch "${branch}" not found on remote origin. Pass --commit <sha> explicitly.`); |
| 43 | + err.exitCode = 1; |
| 44 | + throw err; |
| 45 | + } |
| 46 | + return sha; |
| 47 | + } catch (err) { |
| 48 | + if (err.exitCode) throw err; |
| 49 | + const wrapped = new Error(`Could not resolve commit for branch "${branch}": ${err.message}. Pass --commit <sha> explicitly.`); |
| 50 | + wrapped.exitCode = 1; |
| 51 | + throw wrapped; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +export async function runStartScan({ repo, branch, commit, include, exclude } = {}) { |
| 56 | + const resolvedRepo = repo || detectRepoName(); |
| 57 | + if (!resolvedRepo) { |
| 58 | + const err = new Error('Could not detect repo name. Use --repo owner/repo'); |
| 59 | + err.exitCode = 1; |
| 60 | + throw err; |
| 61 | + } |
| 62 | + |
| 63 | + const resolvedBranch = branch || await resolveCurrentBranch(); |
| 64 | + if (!resolvedBranch) { |
| 65 | + const err = new Error('Could not detect current branch. Use --branch <name>'); |
| 66 | + err.exitCode = 1; |
| 67 | + throw err; |
| 68 | + } |
| 69 | + |
| 70 | + const commitId = commit || await resolveRemoteCommit(resolvedBranch); |
| 71 | + |
| 72 | + const includeFiles = include ? splitGlobs(include) : []; |
| 73 | + const excludeFiles = exclude ? splitGlobs(exclude) : []; |
| 74 | + |
| 75 | + const result = await startScan({ |
| 76 | + repo: resolvedRepo, |
| 77 | + branch: resolvedBranch, |
| 78 | + commitId, |
| 79 | + includeFiles: includeFiles.length ? includeFiles : undefined, |
| 80 | + excludeFiles: excludeFiles.length ? excludeFiles : undefined, |
| 81 | + }); |
| 82 | + |
| 83 | + if (!result.success) { |
| 84 | + const err = new Error(result.error || 'Failed to start scan'); |
| 85 | + err.exitCode = 1; |
| 86 | + throw err; |
| 87 | + } |
| 88 | + |
| 89 | + console.log(result.message || 'Analysis started'); |
| 90 | +} |
0 commit comments