diff --git a/scan/__tests__/main.test.ts b/scan/__tests__/main.test.ts index bbc70fd9..fcf29ce1 100644 --- a/scan/__tests__/main.test.ts +++ b/scan/__tests__/main.test.ts @@ -61,13 +61,18 @@ test('qodana scan command args', () => { test('test sarif with problems to output annotations', () => { const output = annotationsDefaultFixture() - const result = parseSarif('__tests__/data/some.sarif.json') + const defaultProjectDir = '' + const result = parseSarif('__tests__/data/some.sarif.json', defaultProjectDir) expect(result.annotations).toEqual(output) }) test('test sarif with no problems to output annotations', () => { const output = outputEmptyFixture() - const result = parseSarif('__tests__/data/empty.sarif.json') + const defaultProjectDir = '' + const result = parseSarif( + '__tests__/data/empty.sarif.json', + defaultProjectDir + ) expect(result.annotations).toEqual(output) }) diff --git a/scan/src/annotations.ts b/scan/src/annotations.ts index 122b3acc..49a99794 100644 --- a/scan/src/annotations.ts +++ b/scan/src/annotations.ts @@ -121,11 +121,13 @@ export interface Annotation { * Converts a SARIF result to a GitHub Check Annotation. * @param result The SARIF log to convert. * @param rules The map of SARIF rule IDs to their descriptions. + * @param projectDir The path to the project. * @returns GitHub Check annotations are created for each result. */ function parseResult( result: Result, - rules: Map + rules: Map, + projectDir: string ): Annotation | null { if ( !result.locations || @@ -136,10 +138,14 @@ function parseResult( } const location = result.locations[0].physicalLocation const region = location.region + const pathPrefix = + projectDir === '' || projectDir.endsWith('/') + ? projectDir + : `${projectDir}/` return { message: result.message.markdown ?? result.message.text!, title: rules.get(result.ruleId!)?.shortDescription, - path: location.artifactLocation!.uri!, + path: pathPrefix + location.artifactLocation!.uri!, start_line: region?.startLine || 0, end_line: region?.endLine || region?.startLine || 1, start_column: @@ -189,9 +195,10 @@ function parseRules(tool: Tool): Map { /** * Converts a SARIF from the given path to a GitHub Check Output. * @param path The SARIF path to convert. + * @param projectDir The path to the project. * @returns GitHub Check Outputs with annotations are created for each result. */ -export function parseSarif(path: string): Output { +export function parseSarif(path: string, projectDir: string): Output { const sarif: Log = JSON.parse( fs.readFileSync(path, {encoding: 'utf8'}) ) as Log @@ -205,7 +212,7 @@ export function parseSarif(path: string): Output { )} found by ` annotations = run.results .filter(result => result.baselineState !== 'unchanged') - .map(result => parseResult(result, rules)) + .map(result => parseResult(result, rules, projectDir)) .filter((a): a is Annotation => a !== null && a !== undefined) } const name = run.tool.driver.fullName || 'Qodana' diff --git a/scan/src/output.ts b/scan/src/output.ts index c97dadcb..3d4ad56c 100644 --- a/scan/src/output.ts +++ b/scan/src/output.ts @@ -162,7 +162,10 @@ export async function publishOutput( return } try { - const problems = parseSarif(`${resultsDir}/${QODANA_SARIF_NAME}`) + const problems = parseSarif( + `${resultsDir}/${QODANA_SARIF_NAME}`, + projectDir + ) const reportUrl = getReportURL(resultsDir) const coverageInfo = getCoverageStats( getCoverageFromSarif(`${resultsDir}/${QODANA_SHORT_SARIF_NAME}`)