Skip to content

Commit 4844f56

Browse files
Merge pull request #982 from github/robertbrignull/read-problems-result-set
Read problems result set if '#select' result set doesn't exist
2 parents 8e61667 + deda27b commit 4844f56

File tree

3 files changed

+76
-13
lines changed

3 files changed

+76
-13
lines changed

dist/query.js

+10-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/codeql.test.ts

+47
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
Sarif,
1717
getSarifOutputType,
1818
QueryMetadata,
19+
getBqrsResultCount,
1920
} from "./codeql";
2021

2122
const test = anyTest as TestFn<{ db: string; tmpDir: string }>;
@@ -260,3 +261,49 @@ test("getting the SARIF output type when the `@kind` metadata is compatible with
260261

261262
t.is(getSarifOutputType(queryMetadata, compatibleQueryKinds), "problem");
262263
});
264+
265+
test("uses result count from #select result set if it exists", (t) => {
266+
const bqrsInfo: BQRSInfo = {
267+
resultSets: [{ name: "#select", rows: 3 }],
268+
compatibleQueryKinds: [],
269+
};
270+
271+
t.is(getBqrsResultCount(bqrsInfo), 3);
272+
});
273+
274+
test("uses result count from problems result set if it exists", (t) => {
275+
const bqrsInfo: BQRSInfo = {
276+
resultSets: [{ name: "problems", rows: 4 }],
277+
compatibleQueryKinds: [],
278+
};
279+
280+
t.is(getBqrsResultCount(bqrsInfo), 4);
281+
});
282+
283+
test("uses result count from #select result set if both #select and problems result sets exist", (t) => {
284+
const bqrsInfo: BQRSInfo = {
285+
resultSets: [
286+
{ name: "#select", rows: 3 },
287+
{ name: "problems", rows: 4 },
288+
],
289+
compatibleQueryKinds: [],
290+
};
291+
292+
t.is(getBqrsResultCount(bqrsInfo), 3);
293+
});
294+
295+
test("throws error if neither #select or problems result sets exist", (t) => {
296+
const bqrsInfo: BQRSInfo = {
297+
resultSets: [
298+
{ name: "something", rows: 13 },
299+
{ name: "unknown", rows: 42 },
300+
],
301+
compatibleQueryKinds: [],
302+
};
303+
304+
const error = t.throws(() => getBqrsResultCount(bqrsInfo));
305+
t.deepEqual(
306+
error?.message,
307+
"BQRS does not contain any result sets matching known names. Expected one of #select or problems but found something, unknown",
308+
);
309+
});

src/codeql.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -449,17 +449,29 @@ export function getSarifResultCount(sarif: Sarif): number {
449449
return count;
450450
}
451451

452+
/**
453+
* Names of result sets that can be considered the "default" result set
454+
* and should be used when calculating number of results and when showing
455+
* results to users.
456+
* Will check result sets in this order and use the first one that exists.
457+
*/
458+
const KNOWN_RESULT_SET_NAMES: string[] = ["#select", "problems"];
459+
452460
/**
453461
* Gets the number of results in the given BQRS data.
454462
*/
455-
function getBqrsResultCount(bqrsInfo: BQRSInfo): number {
456-
const selectResultSet = bqrsInfo.resultSets.find(
457-
(resultSet) => resultSet.name === "#select",
458-
);
459-
if (!selectResultSet) {
460-
throw new Error("No result set named #select");
463+
export function getBqrsResultCount(bqrsInfo: BQRSInfo): number {
464+
for (const name of KNOWN_RESULT_SET_NAMES) {
465+
const resultSet = bqrsInfo.resultSets.find((r) => r.name === name);
466+
if (resultSet !== undefined) {
467+
return resultSet.rows;
468+
}
461469
}
462-
return selectResultSet.rows;
470+
471+
const resultSetNames = bqrsInfo.resultSets.map((r) => r.name);
472+
throw new Error(
473+
`BQRS does not contain any result sets matching known names. Expected one of ${KNOWN_RESULT_SET_NAMES.join(" or ")} but found ${resultSetNames.join(", ")}`,
474+
);
463475
}
464476

465477
interface DatabaseMetadata {

0 commit comments

Comments
 (0)