Skip to content

Commit 398f16c

Browse files
committed
Change fetch queries to use shared methods.
1 parent ea14767 commit 398f16c

2 files changed

Lines changed: 44 additions & 64 deletions

File tree

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

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ import { runFlowModelQueries } from "./flow-model-queries";
2626
import { promptImportGithubDatabase } from "../databases/database-fetcher";
2727
import { App } from "../common/app";
2828
import { showResolvableLocation } from "../databases/local-databases/locations";
29-
import { decodeBqrsToExternalApiUsages } from "./bqrs";
3029
import { redactableError } from "../common/errors";
31-
import {
32-
readQueryResults,
33-
runExternalApiQueries,
34-
} from "./external-api-usage-queries";
30+
import { runExternalApiQueries } from "./external-api-usage-queries";
3531
import { ExternalApiUsage, Usage } from "./external-api-usage";
3632
import { ModeledMethod } from "./modeled-method";
3733
import { ExtensionPack } from "./shared/extension-pack";
@@ -319,28 +315,7 @@ export class DataExtensionsEditorView extends AbstractWebview<
319315
if (!queryResult) {
320316
return;
321317
}
322-
323-
progress({
324-
message: "Decoding results",
325-
step: 1100,
326-
maxStep: 1500,
327-
});
328-
329-
const bqrsChunk = await readQueryResults({
330-
cliServer: this.cliServer,
331-
bqrsPath: queryResult.outputDir.bqrsPath,
332-
});
333-
if (!bqrsChunk) {
334-
return;
335-
}
336-
337-
progress({
338-
message: "Finalizing results",
339-
step: 1450,
340-
maxStep: 1500,
341-
});
342-
343-
this.externalApiUsages = decodeBqrsToExternalApiUsages(bqrsChunk);
318+
this.externalApiUsages = queryResult;
344319

345320
await this.postMessage({
346321
t: "setExternalApiUsages",

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

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
import { CoreCompletedQuery, QueryRunner } from "../query-server";
1+
import { QueryRunner } from "../query-server";
22
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
33
import { extLogger } from "../common/logging/vscode";
4-
import { showAndLogExceptionWithTelemetry, TeeLogger } from "../common/logging";
4+
import { showAndLogExceptionWithTelemetry } from "../common/logging";
55
import { CancellationToken } from "vscode";
66
import { CodeQLCliServer } from "../codeql-cli/cli";
77
import { DatabaseItem } from "../databases/local-databases";
88
import { ProgressCallback } from "../common/vscode/progress";
9-
import { QueryResultType } from "../query-server/new-messages";
109
import { redactableError } from "../common/errors";
1110
import { telemetryListener } from "../common/vscode/telemetry";
1211
import { join } from "path";
1312
import { Mode } from "./shared/mode";
1413
import { writeFile } from "fs-extra";
1514
import { QueryLanguage } from "../common/query-language";
1615
import { fetchExternalApiQueries } from "./queries";
16+
import { ExternalApiUsage } from "./external-api-usage";
17+
import { runQuery } from "../local-queries/run-query";
18+
import { decodeBqrsToExternalApiUsages } from "./bqrs";
1719

1820
type RunQueryOptions = {
19-
cliServer: Pick<CodeQLCliServer, "resolveQlpacks">;
20-
queryRunner: Pick<QueryRunner, "createQueryRun" | "logger">;
21-
databaseItem: Pick<DatabaseItem, "contents" | "databaseUri" | "language">;
21+
cliServer: CodeQLCliServer;
22+
queryRunner: QueryRunner;
23+
databaseItem: DatabaseItem;
2224
queryStorageDir: string;
2325
queryDir: string;
2426

@@ -67,7 +69,7 @@ export async function runExternalApiQueries(
6769
progress,
6870
token,
6971
}: RunQueryOptions,
70-
): Promise<CoreCompletedQuery | undefined> {
72+
): Promise<ExternalApiUsage[] | undefined> {
7173
// The below code is temporary to allow for rapid prototyping of the queries. Once the queries are stabilized, we will
7274
// move these queries into the `github/codeql` repository and use them like any other contextual (e.g. AST) queries.
7375
// This is intentionally not pretty code, as it will be removed soon.
@@ -79,44 +81,47 @@ export async function runExternalApiQueries(
7981
await cliServer.resolveQlpacks(additionalPacks, true),
8082
);
8183

82-
const queryFile = join(
83-
queryDir,
84-
`FetchExternalApis${mode.charAt(0).toUpperCase() + mode.slice(1)}Mode.ql`,
85-
);
84+
const queryPath = join(queryDir, queryNameFromMode(mode));
8685

87-
const queryRun = queryRunner.createQueryRun(
88-
databaseItem.databaseUri.fsPath,
89-
{
90-
queryPath: queryFile,
91-
quickEvalPosition: undefined,
92-
quickEvalCountOnly: false,
93-
},
94-
false,
95-
getOnDiskWorkspaceFolders(),
96-
extensionPacks,
86+
// Run the actual query
87+
const completedQuery = await runQuery({
88+
cliServer,
89+
queryRunner,
90+
databaseItem,
91+
queryPath,
9792
queryStorageDir,
98-
undefined,
99-
undefined,
100-
);
101-
102-
const completedQuery = await queryRun.evaluate(
93+
additionalPacks,
94+
extensionPacks,
10395
progress,
10496
token,
105-
new TeeLogger(queryRunner.logger, queryRun.outputDir.logPath),
106-
);
97+
});
10798

108-
if (completedQuery.resultType !== QueryResultType.SUCCESS) {
109-
void showAndLogExceptionWithTelemetry(
110-
extLogger,
111-
telemetryListener,
112-
redactableError`External API usage query failed: ${
113-
completedQuery.message ?? "No message"
114-
}`,
115-
);
99+
if (!completedQuery) {
100+
return;
101+
}
102+
103+
// Read the results and covert to internal representation
104+
progress({
105+
message: "Decoding results",
106+
step: 1100,
107+
maxStep: 1500,
108+
});
109+
110+
const bqrsChunk = await readQueryResults({
111+
cliServer,
112+
bqrsPath: completedQuery.outputDir.bqrsPath,
113+
});
114+
if (!bqrsChunk) {
116115
return;
117116
}
118117

119-
return completedQuery;
118+
progress({
119+
message: "Finalizing results",
120+
step: 1450,
121+
maxStep: 1500,
122+
});
123+
124+
return decodeBqrsToExternalApiUsages(bqrsChunk);
120125
}
121126

122127
type GetResultsOptions = {

0 commit comments

Comments
 (0)