From ab6c4fedf66ade7b421ffa9785763f844640e326 Mon Sep 17 00:00:00 2001 From: okue Date: Thu, 12 Dec 2024 15:49:53 +0900 Subject: [PATCH] :bug: Fix annotation path incorrectness when --project-dir is specified --- scan/__tests__/main.test.ts | 13 +++++++++---- scan/dist/index.js | 25 +++++++++++++------------ scan/src/annotations.ts | 15 +++++++++++---- scan/src/output.ts | 5 ++++- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/scan/__tests__/main.test.ts b/scan/__tests__/main.test.ts index ca73f9fc..20dea478 100644 --- a/scan/__tests__/main.test.ts +++ b/scan/__tests__/main.test.ts @@ -42,7 +42,7 @@ test('validate branch names', () => { 'refs/heads/main', 'refs/tags/v1.0.0', 'refs/pull/123/merge', - 'v2024.3.2' + 'v2024.2.6' ] for (let branchName of validBranchNames) { expect(validateBranchName(branchName)).toEqual(branchName) @@ -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) }) @@ -332,7 +337,7 @@ To get \`*.log\` files or any other Qodana artifacts, run the action with \`uplo so that the action will upload the files as the job artifacts: \`\`\`yaml - name: 'Qodana Scan' - uses: JetBrains/qodana-action@v2024.3.2 + uses: JetBrains/qodana-action@v2024.2.6 with: upload-result: true \`\`\` diff --git a/scan/dist/index.js b/scan/dist/index.js index f52f15d7..9dd37c09 100644 --- a/scan/dist/index.js +++ b/scan/dist/index.js @@ -24614,14 +24614,14 @@ var require_github = __commonJS({ var version, checksum; var init_cli = __esm({ "../common/cli.json"() { - version = "2024.3.2"; + version = "2024.2.6"; checksum = { - windows_x86_64: "d2260f5d2cfd18c0795adcaf7ff57f77d2e7f9d0ce888531c443923388262eec", - linux_arm64: "8086568b6c62fb7f109c33d8f04f68cf060db4cd260a46d5f581fb093474ecc7", - darwin_arm64: "03962c7c513b6a1ca67164bbcb65f8673a60cff6fe13f618598439b45d352084", - darwin_x86_64: "e70904e0c4fc1d56a8d5f1e2682ccbe168b41b973dac2c5fe42f9b31837dbd86", - windows_arm64: "940e9a84b03db0b5e67f93299879e804984b3893577a58b2424dbc5c2db39cbe", - linux_x86_64: "a7a47898971fe2c7db08d10afc40292736df33e9b33d9d84b8672f3ec246da04" + windows_x86_64: "a63c618048aaab42c7448e5307f53cc8046d744f27cc769074529daf4bb09a8f", + linux_arm64: "77189751f3c04205613dde5890bae7c47143cb68c1cf542524d8c5b59aeccf8b", + darwin_arm64: "f5e1e2a64f0ef03b58f95f78beff0891610d0ea9fd80575d366128b109d116c2", + darwin_x86_64: "6993e105f8fd83101d099015a97cf2bdb4fab53d6dd59ce4160c1b64a22ecd70", + windows_arm64: "ae59b153135db484d5481723ec922c427028637833003b3b1762ac2686f0a885", + linux_x86_64: "655d227ca5783df9b12319db00b07ae1e98598e7cd9bb6738d333c7e963d8779" }; } }); @@ -137124,7 +137124,7 @@ ${c.freshLines} lines analyzed, ${c.freshCoveredLines} lines covered`; return; } try { - const problems = (0, annotations_1.parseSarif)(`${resultsDir}/${qodana_12.QODANA_SARIF_NAME}`); + const problems = (0, annotations_1.parseSarif)(`${resultsDir}/${qodana_12.QODANA_SARIF_NAME}`, projectDir); const reportUrl = getReportURL(resultsDir); const coverageInfo = getCoverageStats((0, qodana_12.getCoverageFromSarif)(`${resultsDir}/${qodana_12.QODANA_SHORT_SARIF_NAME}`)); let licensesInfo = ""; @@ -137380,17 +137380,18 @@ var require_annotations = __commonJS({ }); } __name(publishAnnotations, "publishAnnotations"); - function parseResult(result, rules) { + function parseResult(result, rules, projectDir) { var _a, _b; if (!result.locations || result.locations.length === 0 || !result.locations[0].physicalLocation) { return null; } const location = result.locations[0].physicalLocation; const region = location.region; + const pathPrefix = projectDir === "" || projectDir.endsWith("/") ? projectDir : projectDir + "/"; return { message: (_a = result.message.markdown) !== null && _a !== void 0 ? _a : result.message.text, title: (_b = rules.get(result.ruleId)) === null || _b === void 0 ? void 0 : _b.shortDescription, - path: location.artifactLocation.uri, + path: pathPrefix + location.artifactLocation.uri, start_line: (region === null || region === void 0 ? void 0 : region.startLine) || 0, end_line: (region === null || region === void 0 ? void 0 : region.endLine) || (region === null || region === void 0 ? void 0 : region.startLine) || 1, start_column: (region === null || region === void 0 ? void 0 : region.startLine) === (region === null || region === void 0 ? void 0 : region.endColumn) ? region === null || region === void 0 ? void 0 : region.startColumn : void 0, @@ -137429,7 +137430,7 @@ var require_annotations = __commonJS({ return rules; } __name(parseRules, "parseRules"); - function parseSarif(path2) { + function parseSarif(path2, projectDir) { var _a; const sarif = JSON.parse(fs2.readFileSync(path2, { encoding: "utf8" })); const run = sarif.runs[0]; @@ -137438,7 +137439,7 @@ var require_annotations = __commonJS({ let annotations = []; if ((_a = run.results) === null || _a === void 0 ? void 0 : _a.length) { title = `${run.results.length} ${(0, output_12.getProblemPlural)(run.results.length)} found by `; - annotations = run.results.filter((result) => result.baselineState !== "unchanged").map((result) => parseResult(result, rules)).filter((a) => a !== null && a !== void 0); + annotations = run.results.filter((result) => result.baselineState !== "unchanged").map((result) => parseResult(result, rules, projectDir)).filter((a) => a !== null && a !== void 0); } const name = run.tool.driver.fullName || "Qodana"; title += name; 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}`)