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 12, 2024
1 parent b468474 commit ab6c4fe
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
13 changes: 9 additions & 4 deletions scan/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 Expand Up @@ -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
\`\`\`
Expand Down
25 changes: 13 additions & 12 deletions scan/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
}
});
Expand Down Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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];
Expand All @@ -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;
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 ab6c4fe

Please sign in to comment.