Skip to content

Commit

Permalink
🐛 Fix annotation path incorrectness when --project-dir is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
okue committed Dec 20, 2024
1 parent c9aae26 commit 859a107
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
9 changes: 7 additions & 2 deletions scan/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down
15 changes: 11 additions & 4 deletions scan/src/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Rule>
rules: Map<string, Rule>,
projectDir: string
): Annotation | null {
if (
!result.locations ||
Expand All @@ -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:
Expand Down Expand Up @@ -189,9 +195,10 @@ function parseRules(tool: Tool): Map<string, Rule> {
/**
* 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
Expand All @@ -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'
Expand Down
5 changes: 4 additions & 1 deletion scan/src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down

0 comments on commit 859a107

Please sign in to comment.