Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix annotation path incorrectness when --project-dir is specified #410

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading