From b2656d93405113fbcf488e56e2f1fafdd33f33be Mon Sep 17 00:00:00 2001 From: Yair Zohar Date: Mon, 10 Jan 2022 16:41:26 +0200 Subject: [PATCH] fix: IaC SARIF output --- src/lib/formatters/iac-output.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/lib/formatters/iac-output.ts b/src/lib/formatters/iac-output.ts index f4309fd20c..c29df9bca1 100644 --- a/src/lib/formatters/iac-output.ts +++ b/src/lib/formatters/iac-output.ts @@ -138,7 +138,7 @@ export function createSarifOutputForIac( const basePath = isLocalFolder(iacTestResponses[0].path) ? pathLib.resolve('.', iacTestResponses[0].path) : pathLib.resolve('.'); - const repoRoot = getRepoRoot(); + const repoRoot = getRepoRoot(basePath); const issues = iacTestResponses.reduce((collect: ResponseIssues, res) => { if (res.result) { // targetFile is the computed relative path of the scanned file @@ -294,13 +294,17 @@ export function mapIacTestResponseToSarifResults( }); } -function getRepoRoot() { - const cwd = process.cwd(); - const stdout = execSync('git rev-parse --show-toplevel', { - encoding: 'utf8', - cwd, - }); - return stdout.trim() + '/'; +function getRepoRoot(basePath: string) { + try { + const cwd = process.cwd(); + const stdout = execSync('git rev-parse --show-toplevel', { + encoding: 'utf8', + cwd, + }); + return stdout.trim() + '/'; + } catch { + return basePath; + } } function getPathRelativeToRepoRoot(