Skip to content

Commit 405292e

Browse files
committed
Introduce type for QlPackFile
1 parent 5ae67fe commit 405292e

File tree

6 files changed

+120
-36
lines changed

6 files changed

+120
-36
lines changed

extensions/ql-vscode/src/local-queries/query-resolver.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { redactableError } from "../common/errors";
1313
import { showAndLogExceptionWithTelemetry } from "../common/logging";
1414
import { extLogger } from "../common/logging/vscode";
1515
import { telemetryListener } from "../common/vscode/telemetry";
16+
import { SuiteInstruction } from "../packaging/suite-instruction";
1617

1718
export async function qlpackOfDatabase(
1819
cli: Pick<CodeQLCliServer, "resolveQlpacks">,
@@ -50,12 +51,12 @@ async function resolveQueriesFromPacks(
5051
postfix: ".qls",
5152
})
5253
).path;
53-
const suiteYaml = [];
54+
const suiteYaml: SuiteInstruction[] = [];
5455
for (const qlpack of qlpacks) {
5556
suiteYaml.push({
5657
from: qlpack,
5758
queries: ".",
58-
include: constraints,
59+
include: constraints as Record<string, string[]>,
5960
});
6061
}
6162
await writeFile(

extensions/ql-vscode/src/model-editor/extension-pack-metadata.schema.json

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,86 @@
55
"ExtensionPackMetadata": {
66
"type": "object",
77
"properties": {
8+
"extensionTargets": {
9+
"type": "object",
10+
"additionalProperties": {
11+
"type": "string"
12+
}
13+
},
14+
"dataExtensions": {
15+
"type": "array",
16+
"items": {
17+
"type": "string"
18+
}
19+
},
820
"name": {
921
"type": "string"
1022
},
1123
"version": {
1224
"type": "string"
1325
},
14-
"dataExtensions": {
15-
"anyOf": [
16-
{
26+
"dependencies": {
27+
"type": "object",
28+
"additionalProperties": {
29+
"type": "string"
30+
}
31+
},
32+
"dbscheme": {
33+
"type": "string"
34+
},
35+
"library": {
36+
"type": "boolean"
37+
},
38+
"defaultSuite": {
39+
"type": "array",
40+
"items": {
41+
"$ref": "#/definitions/SuiteInstruction"
42+
}
43+
},
44+
"defaultSuiteFile": {
45+
"type": "string"
46+
}
47+
},
48+
"required": ["dataExtensions", "extensionTargets", "name", "version"]
49+
},
50+
"SuiteInstruction": {
51+
"type": "object",
52+
"properties": {
53+
"qlpack": {
54+
"type": "string"
55+
},
56+
"query": {
57+
"type": "string"
58+
},
59+
"queries": {
60+
"type": "string"
61+
},
62+
"include": {
63+
"type": "object",
64+
"additionalProperties": {
65+
"type": "array",
66+
"items": {
1767
"type": "string"
18-
},
19-
{
20-
"type": "array",
21-
"items": {
22-
"type": "string"
23-
}
2468
}
25-
]
69+
}
2670
},
27-
"extensionTargets": {
71+
"exclude": {
2872
"type": "object",
2973
"additionalProperties": {
30-
"type": "string"
74+
"type": "array",
75+
"items": {
76+
"type": "string"
77+
}
3178
}
79+
},
80+
"description": {
81+
"type": "string"
82+
},
83+
"from": {
84+
"type": "string"
3285
}
3386
},
34-
"required": ["name", "version", "dataExtensions", "extensionTargets"]
87+
"description": "A single entry in a .qls file."
3588
}
3689
}
3790
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
export type ExtensionPackMetadata = {
2-
name: string;
3-
version: string;
4-
dataExtensions: string | string[];
1+
import { QlPackFile } from "../packaging/qlpack-file";
2+
3+
export type ExtensionPackMetadata = QlPackFile & {
4+
// Make both extensionTargets and dataExtensions required
55
extensionTargets: Record<string, string>;
6+
dataExtensions: string[];
67
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { SuiteInstruction } from "./suite-instruction";
2+
3+
/**
4+
* The qlpack pack file, either in qlpack.yml or in codeql-pack.yml.
5+
*/
6+
export interface QlPackFile {
7+
name: string;
8+
version: string;
9+
dependencies?: Record<string, string>;
10+
extensionTargets?: Record<string, string>;
11+
dbscheme?: string;
12+
library?: boolean;
13+
defaultSuite?: SuiteInstruction[];
14+
defaultSuiteFile?: string;
15+
dataExtensions?: string[];
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* A single entry in a .qls file.
3+
*/
4+
export interface SuiteInstruction {
5+
qlpack?: string;
6+
query?: string;
7+
queries?: string;
8+
include?: Record<string, string[]>;
9+
exclude?: Record<string, string[]>;
10+
description?: string;
11+
from?: string;
12+
}

extensions/ql-vscode/src/variant-analysis/run-remote-query.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,7 @@ import {
3737
import { QueryLanguage } from "../common/query-language";
3838
import { tryGetQueryMetadata } from "../codeql-cli/query-metadata";
3939
import { askForLanguage, findLanguage } from "../codeql-cli/query-language";
40-
41-
interface QlPack {
42-
name: string;
43-
version: string;
44-
library?: boolean;
45-
dependencies: { [key: string]: string };
46-
defaultSuite?: Array<Record<string, unknown>>;
47-
defaultSuiteFile?: string;
48-
}
40+
import { QlPackFile } from "../packaging/qlpack-file";
4941

5042
/**
5143
* Well-known names for the query pack used by the server.
@@ -395,7 +387,7 @@ async function fixPackFile(
395387
)} file in '${queryPackDir}'`,
396388
);
397389
}
398-
const qlpack = load(await readFile(packPath, "utf8")) as QlPack;
390+
const qlpack = load(await readFile(packPath, "utf8")) as QlPackFile;
399391

400392
updateDefaultSuite(qlpack, packRelativePath);
401393
removeWorkspaceRefs(qlpack);
@@ -416,7 +408,11 @@ async function injectExtensionPacks(
416408
)} file in '${queryPackDir}'`,
417409
);
418410
}
419-
const syntheticQueryPack = load(await readFile(qlpackFile, "utf8")) as QlPack;
411+
const syntheticQueryPack = load(
412+
await readFile(qlpackFile, "utf8"),
413+
) as QlPackFile;
414+
415+
const dependencies = syntheticQueryPack.dependencies ?? {};
420416

421417
const extensionPacks = await cliServer.resolveQlpacks(workspaceFolders, true);
422418
Object.entries(extensionPacks).forEach(([name, paths]) => {
@@ -433,13 +429,16 @@ async function injectExtensionPacks(
433429
// Add this extension pack as a dependency. It doesn't matter which
434430
// version we specify, since we are guaranteed that the extension pack
435431
// is resolved from source at the given path.
436-
syntheticQueryPack.dependencies[name] = "*";
432+
dependencies[name] = "*";
437433
});
434+
435+
syntheticQueryPack.dependencies = dependencies;
436+
438437
await writeFile(qlpackFile, dump(syntheticQueryPack));
439438
await cliServer.clearCache();
440439
}
441440

442-
function updateDefaultSuite(qlpack: QlPack, packRelativePath: string) {
441+
function updateDefaultSuite(qlpack: QlPackFile, packRelativePath: string) {
443442
delete qlpack.defaultSuiteFile;
444443
qlpack.defaultSuite = generateDefaultSuite(packRelativePath);
445444
}
@@ -541,10 +540,12 @@ async function getControllerRepoFromApi(
541540
}
542541
}
543542

544-
export function removeWorkspaceRefs(qlpack: {
545-
dependencies: Record<string, string>;
546-
}) {
547-
for (const [key, value] of Object.entries(qlpack.dependencies || {})) {
543+
export function removeWorkspaceRefs(qlpack: QlPackFile) {
544+
if (!qlpack.dependencies) {
545+
return;
546+
}
547+
548+
for (const [key, value] of Object.entries(qlpack.dependencies)) {
548549
if (value === "${workspace}") {
549550
qlpack.dependencies[key] = "*";
550551
}

0 commit comments

Comments
 (0)