Skip to content

Commit a117e09

Browse files
committed
Use a single SARIF-compatible query instead of two separate queries
1 parent 5c81671 commit a117e09

File tree

10 files changed

+136
-158
lines changed

10 files changed

+136
-158
lines changed

extensions/ql-vscode/src/data-extensions-editor/auto-model-usages-query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export async function getAutoModelUsages({
3737

3838
const cancellationTokenSource = new CancellationTokenSource();
3939

40-
const queryResult = await runQuery("usagesQuery", {
40+
const queryResult = await runQuery({
4141
cliServer,
4242
queryRunner,
4343
queryStorageDir,

extensions/ql-vscode/src/data-extensions-editor/bqrs.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export function decodeBqrsToExternalApiUsages(
77
const methodsByApiName = new Map<string, ExternalApiUsage>();
88

99
chunk?.tuples.forEach((tuple) => {
10-
const signature = tuple[0] as string;
11-
const supported = tuple[1] as boolean;
12-
const usage = tuple[2] as Call;
10+
const usage = tuple[0] as Call;
11+
const signature = tuple[1] as string;
12+
const supported = (tuple[2] as string) === "true";
1313

1414
const [packageWithType, methodDeclaration] = signature.split("#");
1515

extensions/ql-vscode/src/data-extensions-editor/data-extensions-editor-view.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export class DataExtensionsEditorView extends AbstractWebview<
243243
const cancellationTokenSource = new CancellationTokenSource();
244244

245245
try {
246-
const queryResult = await runQuery("mainQuery", {
246+
const queryResult = await runQuery({
247247
cliServer: this.cliServer,
248248
queryRunner: this.queryRunner,
249249
databaseItem: this.databaseItem,

extensions/ql-vscode/src/data-extensions-editor/external-api-usage-query.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { QueryResultType } from "../pure/new-messages";
1616
import { join } from "path";
1717
import { redactableError } from "../pure/errors";
1818
import { QueryLanguage } from "../common/query-language";
19-
import { Query } from "./queries/query";
2019

2120
export type RunQueryOptions = {
2221
cliServer: Pick<CodeQLCliServer, "resolveQlpacks">;
@@ -28,17 +27,14 @@ export type RunQueryOptions = {
2827
token: CancellationToken;
2928
};
3029

31-
export async function runQuery(
32-
queryName: keyof Omit<Query, "dependencies">,
33-
{
34-
cliServer,
35-
queryRunner,
36-
databaseItem,
37-
queryStorageDir,
38-
progress,
39-
token,
40-
}: RunQueryOptions,
41-
): Promise<CoreCompletedQuery | undefined> {
30+
export async function runQuery({
31+
cliServer,
32+
queryRunner,
33+
databaseItem,
34+
queryStorageDir,
35+
progress,
36+
token,
37+
}: RunQueryOptions): Promise<CoreCompletedQuery | undefined> {
4238
// The below code is temporary to allow for rapid prototyping of the queries. Once the queries are stabilized, we will
4339
// move these queries into the `github/codeql` repository and use them like any other contextual (e.g. AST) queries.
4440
// This is intentionally not pretty code, as it will be removed soon.
@@ -55,7 +51,7 @@ export async function runQuery(
5551

5652
const queryDir = (await dir({ unsafeCleanup: true })).path;
5753
const queryFile = join(queryDir, "FetchExternalApis.ql");
58-
await writeFile(queryFile, query[queryName], "utf8");
54+
await writeFile(queryFile, query.mainQuery, "utf8");
5955

6056
if (query.dependencies) {
6157
for (const [filename, contents] of Object.entries(query.dependencies)) {

extensions/ql-vscode/src/data-extensions-editor/queries/csharp.ts

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,31 @@ import { Query } from "./query";
22

33
export const fetchExternalApisQuery: Query = {
44
mainQuery: `/**
5-
* @name Usage of APIs coming from external libraries
6-
* @description A list of 3rd party APIs used in the codebase.
7-
* @tags telemetry
8-
* @id cs/telemetry/fetch-external-apis
9-
*/
5+
* @name Usage of APIs coming from external libraries
6+
* @description A list of 3rd party APIs used in the codebase.
7+
* @tags telemetry
8+
* @kind problem
9+
* @id cs/telemetry/fetch-external-apis
10+
*/
1011
1112
import csharp
1213
import ExternalApi
1314
14-
private Call aUsage(ExternalApi api) {
15-
result.getTarget().getUnboundDeclaration() = api
16-
}
15+
private Call aUsage(ExternalApi api) { result.getTarget().getUnboundDeclaration() = api }
1716
1817
private boolean isSupported(ExternalApi api) {
19-
api.isSupported() and result = true
20-
or
21-
not api.isSupported() and
22-
result = false
18+
api.isSupported() and result = true
19+
or
20+
not api.isSupported() and
21+
result = false
2322
}
2423
2524
from ExternalApi api, string apiName, boolean supported, Call usage
2625
where
27-
apiName = api.getApiName() and
28-
supported = isSupported(api) and
29-
usage = aUsage(api)
30-
select apiName, supported, usage
31-
`,
32-
usagesQuery: `/**
33-
* @name Usage of APIs coming from external libraries
34-
* @description A list of 3rd party APIs used in the codebase.
35-
* @kind problem
36-
* @id cs/telemetry/fetch-external-api-usages
37-
*/
38-
39-
import csharp
40-
import ExternalApi
41-
42-
private Call aUsage(ExternalApi api) {
43-
result.getTarget().getUnboundDeclaration() = api
44-
}
45-
46-
from ExternalApi api, string apiName, Call usage
47-
where
48-
apiName = api.getApiName() and
49-
usage = aUsage(api)
50-
select usage, apiName
26+
apiName = api.getApiName() and
27+
supported = isSupported(api) and
28+
usage = aUsage(api)
29+
select usage, apiName, supported.toString(), "supported"
5130
`,
5231
dependencies: {
5332
"ExternalApi.qll": `/** Provides classes and predicates related to handling APIs from external libraries. */

extensions/ql-vscode/src/data-extensions-editor/queries/java.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const fetchExternalApisQuery: Query = {
55
* @name Usage of APIs coming from external libraries
66
* @description A list of 3rd party APIs used in the codebase. Excludes test and generated code.
77
* @tags telemetry
8+
* @kind problem
89
* @id java/telemetry/fetch-external-apis
910
*/
1011
@@ -27,28 +28,7 @@ where
2728
apiName = api.getApiName() and
2829
supported = isSupported(api) and
2930
usage = aUsage(api)
30-
select apiName, supported, usage
31-
`,
32-
usagesQuery: `/**
33-
* @name Usage of APIs coming from external libraries
34-
* @description A list of 3rd party APIs used in the codebase. Excludes test and generated code.
35-
* @kind problem
36-
* @id java/telemetry/fetch-external-api-usages
37-
*/
38-
39-
import java
40-
import ExternalApi
41-
42-
private Call aUsage(ExternalApi api) {
43-
result.getCallee().getSourceDeclaration() = api and
44-
not result.getFile() instanceof GeneratedFile
45-
}
46-
47-
from ExternalApi api, string apiName, Call usage
48-
where
49-
apiName = api.getApiName() and
50-
usage = aUsage(api)
51-
select usage, apiName
31+
select usage, apiName, supported.toString(), "supported"
5232
`,
5333
dependencies: {
5434
"ExternalApi.qll": `/** Provides classes and predicates related to handling APIs from external libraries. */

extensions/ql-vscode/src/data-extensions-editor/queries/query.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
export type Query = {
2+
/**
3+
* The main query.
4+
*
5+
* It should select all usages of external APIs, and return the following result pattern:
6+
* - usage: the usage of the external API. This is an entity.
7+
* - apiName: the name of the external API. This is a string.
8+
* - supported: whether the external API is supported by the extension. This should be a string representation of a boolean to satify the result pattern for a problem query.
9+
* - "supported": a string literal. This is required to make the query a valid problem query.
10+
*/
211
mainQuery: string;
3-
usagesQuery: string;
412
dependencies?: {
513
[filename: string]: string;
614
};

extensions/ql-vscode/src/pure/bqrs-cli-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export type BqrsKind =
115115
| "Entity";
116116

117117
interface BqrsColumn {
118-
name: string;
118+
name?: string;
119119
kind: BqrsKind;
120120
}
121121
export interface DecodedBqrsChunk {

0 commit comments

Comments
 (0)