11import { CoreCompletedQuery , QueryRunner } from "../query-server" ;
2- import { dir } from "tmp-promise" ;
3- import { writeFile } from "fs-extra" ;
4- import { dump as dumpYaml } from "js-yaml" ;
52import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders" ;
63import { extLogger } from "../common/logging/vscode" ;
74import { showAndLogExceptionWithTelemetry , TeeLogger } from "../common/logging" ;
8- import { isQueryLanguage } from "../common/query-language" ;
95import { CancellationToken } from "vscode" ;
106import { CodeQLCliServer } from "../codeql-cli/cli" ;
117import { DatabaseItem } from "../databases/local-databases" ;
128import { ProgressCallback } from "../common/vscode/progress" ;
13- import { fetchExternalApiQueries } from "./queries" ;
149import { QueryResultType } from "../query-server/new-messages" ;
15- import { join } from "path" ;
1610import { redactableError } from "../common/errors" ;
1711import { telemetryListener } from "../common/vscode/telemetry" ;
12+ import { join } from "path" ;
13+ import { Mode } from "./shared/mode" ;
14+ import { writeFile } from "fs-extra" ;
1815import { Query } from "./queries/query" ;
16+ import { QueryLanguage } from "../common/query-language" ;
17+ import { dump } from "js-yaml" ;
1918
2019type RunQueryOptions = {
2120 cliServer : Pick < CodeQLCliServer , "resolveQlpacks" > ;
2221 queryRunner : Pick < QueryRunner , "createQueryRun" | "logger" > ;
2322 databaseItem : Pick < DatabaseItem , "contents" | "databaseUri" | "language" > ;
2423 queryStorageDir : string ;
24+ queryDir : string ;
2525
2626 progress : ProgressCallback ;
2727 token : CancellationToken ;
2828} ;
2929
30- export async function runQuery (
31- queryName : keyof Omit < Query , "dependencies" > ,
32- {
33- cliServer,
34- queryRunner,
35- databaseItem,
36- queryStorageDir,
37- progress,
38- token,
39- } : RunQueryOptions ,
40- ) : Promise < CoreCompletedQuery | undefined > {
41- // The below code is temporary to allow for rapid prototyping of the queries. Once the queries are stabilized, we will
42- // move these queries into the `github/codeql` repository and use them like any other contextual (e.g. AST) queries.
43- // This is intentionally not pretty code, as it will be removed soon.
44- // For a reference of what this should do in the future, see the previous implementation in
45- // https://github.com/github/vscode-codeql/blob/089d3566ef0bc67d9b7cc66e8fd6740b31c1c0b0/extensions/ql-vscode/src/data-extensions-editor/external-api-usage-query.ts#L33-L72
46-
47- if ( ! isQueryLanguage ( databaseItem . language ) ) {
48- void showAndLogExceptionWithTelemetry (
49- extLogger ,
50- telemetryListener ,
51- redactableError `Unsupported database language ${ databaseItem . language } ` ,
52- ) ;
53- return ;
54- }
55-
56- const query = fetchExternalApiQueries [ databaseItem . language ] ;
57- if ( ! query ) {
58- void showAndLogExceptionWithTelemetry (
59- extLogger ,
60- telemetryListener ,
61- redactableError `No external API usage query found for language ${ databaseItem . language } ` ,
30+ export async function setUpPack (
31+ queryDir : string ,
32+ query : Query ,
33+ language : QueryLanguage ,
34+ ) {
35+ Object . values ( Mode ) . map ( async ( mode ) => {
36+ const queryFile = join (
37+ queryDir ,
38+ `FetchExternalApis${ mode . charAt ( 0 ) . toUpperCase ( ) + mode . slice ( 1 ) } Mode.ql` ,
6239 ) ;
63- return ;
64- }
65-
66- const queryDir = ( await dir ( { unsafeCleanup : true } ) ) . path ;
67- const queryFile = join ( queryDir , "FetchExternalApis.ql" ) ;
68- await writeFile ( queryFile , query [ queryName ] , "utf8" ) ;
40+ await writeFile ( queryFile , query [ `${ mode } ModeQuery` ] , "utf8" ) ;
41+ } ) ;
6942
7043 if ( query . dependencies ) {
7144 for ( const [ filename , contents ] of Object . entries ( query . dependencies ) ) {
@@ -78,18 +51,42 @@ export async function runQuery(
7851 name : "codeql/external-api-usage" ,
7952 version : "0.0.0" ,
8053 dependencies : {
81- [ `codeql/${ databaseItem . language } -all` ] : "*" ,
54+ [ `codeql/${ language } -all` ] : "*" ,
8255 } ,
8356 } ;
8457
8558 const qlpackFile = join ( queryDir , "codeql-pack.yml" ) ;
86- await writeFile ( qlpackFile , dumpYaml ( syntheticQueryPack ) , "utf8" ) ;
59+ await writeFile ( qlpackFile , dump ( syntheticQueryPack ) , "utf8" ) ;
60+ }
61+
62+ export async function runQuery (
63+ mode : Mode ,
64+ {
65+ cliServer,
66+ queryRunner,
67+ databaseItem,
68+ queryStorageDir,
69+ queryDir,
70+ progress,
71+ token,
72+ } : RunQueryOptions ,
73+ ) : Promise < CoreCompletedQuery | undefined > {
74+ // The below code is temporary to allow for rapid prototyping of the queries. Once the queries are stabilized, we will
75+ // move these queries into the `github/codeql` repository and use them like any other contextual (e.g. AST) queries.
76+ // This is intentionally not pretty code, as it will be removed soon.
77+ // For a reference of what this should do in the future, see the previous implementation in
78+ // https://github.com/github/vscode-codeql/blob/089d3566ef0bc67d9b7cc66e8fd6740b31c1c0b0/extensions/ql-vscode/src/data-extensions-editor/external-api-usage-query.ts#L33-L72
8779
8880 const additionalPacks = getOnDiskWorkspaceFolders ( ) ;
8981 const extensionPacks = Object . keys (
9082 await cliServer . resolveQlpacks ( additionalPacks , true ) ,
9183 ) ;
9284
85+ const queryFile = join (
86+ queryDir ,
87+ `FetchExternalApis${ mode . charAt ( 0 ) . toUpperCase ( ) + mode . slice ( 1 ) } Mode.ql` ,
88+ ) ;
89+
9390 const queryRun = queryRunner . createQueryRun (
9491 databaseItem . databaseUri . fsPath ,
9592 {
0 commit comments