Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1a2e5d9
Extract functionality to download GitHub database
elenatanasoiu Mar 23, 2023
053a180
Export existing language prompt
elenatanasoiu Mar 23, 2023
2995b01
Provide a way to search for database items by name and language
elenatanasoiu Mar 29, 2023
e63f0fc
Be able to specify the name of the skeleton query file
elenatanasoiu Mar 29, 2023
ddd00d1
Introduce SkeletonWizard class
elenatanasoiu Mar 29, 2023
91c4c91
Introduce "Create Query" command
elenatanasoiu Mar 29, 2023
22d9487
Be able to specify language when downloading github database
elenatanasoiu Mar 29, 2023
b9decd8
Create QL pack in workspace instead of global storage
elenatanasoiu Mar 30, 2023
f212804
Make "Create Query" command work with vscode-codeql-starter
elenatanasoiu Mar 30, 2023
121ebc2
`skeleton-query.ts` -> `skeleton-query-wizard.ts`
elenatanasoiu Mar 30, 2023
c23b0bf
Replace `github/codeql` with top databases for querying
elenatanasoiu Mar 30, 2023
95ed076
Shorten beforeEach to decrease chances of timeout
elenatanasoiu Apr 3, 2023
061f347
Don't obfuscate assignment when we choose language
elenatanasoiu Apr 11, 2023
30011aa
Fail gracefully if we can't open the new query file
elenatanasoiu Apr 11, 2023
fe3e9a7
Get rid of unnecessary ternary
elenatanasoiu Apr 11, 2023
9c0deae
Make it clear we're checking the filename
elenatanasoiu Apr 11, 2023
f949eda
Fix error message
elenatanasoiu Apr 11, 2023
911c3af
Use a regular const instead of storing this on the class
elenatanasoiu Apr 11, 2023
97fb4ea
Use `Record`
elenatanasoiu Apr 11, 2023
b794427
Remove unnecessary casting to `DatabaseItem`
elenatanasoiu Apr 11, 2023
59909e2
Convert `folderName` property into getter method
elenatanasoiu Apr 11, 2023
16a8289
Shorten logic for deciding when to ask for language
elenatanasoiu Apr 11, 2023
62bebc0
Set storage path after the user selects language
elenatanasoiu Apr 11, 2023
9139426
Increase timeout for skeleton wizard tests
elenatanasoiu Apr 3, 2023
c7d9407
Rename `workoutNextFileName` -> `determineNextFileName`
elenatanasoiu Apr 11, 2023
f4a8de0
Don't count files that are not `example<number>.ql`
elenatanasoiu Apr 11, 2023
460da1e
Add `language` to list of params
elenatanasoiu Apr 11, 2023
e4406f4
Merge branch 'main' into elena/yer-a-wizard-query
elenatanasoiu Apr 12, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions extensions/ql-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@
"command": "codeQL.quickQuery",
"title": "CodeQL: Quick Query"
},
{
"command": "codeQL.createSkeletonQuery",
"title": "CodeQL: Create Query"
Comment thread
elenatanasoiu marked this conversation as resolved.
},
{
"command": "codeQL.openDocumentation",
"title": "CodeQL: Open Documentation"
Expand Down
1 change: 1 addition & 0 deletions extensions/ql-vscode/src/common/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export type LocalQueryCommands = {
"codeQL.quickEvalContextEditor": (uri: Uri) => Promise<void>;
"codeQL.codeLensQuickEval": (uri: Uri, range: Range) => Promise<void>;
"codeQL.quickQuery": () => Promise<void>;
"codeQL.createSkeletonQuery": () => Promise<void>;
Comment thread
elenatanasoiu marked this conversation as resolved.
};

export type ResultsViewCommands = {
Expand Down
75 changes: 61 additions & 14 deletions extensions/ql-vscode/src/databaseFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export async function promptImportInternetDatabase(
*
* @param databaseManager the DatabaseManager
* @param storagePath where to store the unzipped database.
* @param credentials the credentials to use to authenticate with GitHub
* @param progress the progress callback
* @param token the cancellation token
* @param cli the CodeQL CLI server
*/
export async function promptImportGithubDatabase(
commandManager: AppCommandManager,
Expand All @@ -103,6 +107,49 @@ export async function promptImportGithubDatabase(
return;
}

const databaseItem = await downloadGitHubDatabase(
githubRepo,
databaseManager,
storagePath,
credentials,
progress,
token,
cli,
);

if (databaseItem) {
await commandManager.execute("codeQLDatabases.focus");
void showAndLogInformationMessage(
"Database downloaded and imported successfully.",
);
return databaseItem;
}

return;
}

/**
* Downloads a database from GitHub
*
* @param githubRepo the GitHub repository to download the database from
* @param databaseManager the DatabaseManager
* @param storagePath where to store the unzipped database.
* @param credentials the credentials to use to authenticate with GitHub
* @param progress the progress callback
* @param token the cancellation token
* @param cli the CodeQL CLI server
Comment thread
shati-patel marked this conversation as resolved.
* @param language the language to download. If undefined, the user will be prompted to choose a language.
**/
export async function downloadGitHubDatabase(
githubRepo: string,
databaseManager: DatabaseManager,
storagePath: string,
credentials: Credentials | undefined,
progress: ProgressCallback,
token: CancellationToken,
cli?: CodeQLCliServer,
language?: string,
): Promise<DatabaseItem | undefined> {
const nwo = getNwoFromGitHubUrl(githubRepo) || githubRepo;
if (!isValidGitHubNwo(nwo)) {
throw new Error(`Invalid GitHub repository: ${githubRepo}`);
Expand All @@ -112,7 +159,12 @@ export async function promptImportGithubDatabase(
? await credentials.getOctokit()
: new Octokit.Octokit({ retry });

const result = await convertGithubNwoToDatabaseUrl(nwo, octokit, progress);
const result = await convertGithubNwoToDatabaseUrl(
nwo,
octokit,
progress,
language,
);
if (!result) {
return;
}
Expand All @@ -130,7 +182,7 @@ export async function promptImportGithubDatabase(
* We only need the actual token string.
*/
const octokitToken = ((await octokit.auth()) as { token: string })?.token;
const item = await databaseArchiveFetcher(
return await databaseArchiveFetcher(
databaseUrl,
{
Accept: "application/zip",
Expand All @@ -143,14 +195,6 @@ export async function promptImportGithubDatabase(
token,
cli,
);
if (item) {
await commandManager.execute("codeQLDatabases.focus");
void showAndLogInformationMessage(
"Database downloaded and imported successfully.",
);
return item;
}
return;
}

/**
Expand Down Expand Up @@ -450,6 +494,7 @@ export async function convertGithubNwoToDatabaseUrl(
nwo: string,
octokit: Octokit.Octokit,
progress: ProgressCallback,
language?: string,
): Promise<
| {
databaseUrl: string;
Expand All @@ -468,9 +513,11 @@ export async function convertGithubNwoToDatabaseUrl(

const languages = response.data.map((db: any) => db.language);

const language = await promptForLanguage(languages, progress);
if (!language) {
return;
if (!language || !languages.includes(language)) {
language = await promptForLanguage(languages, progress);
if (!language) {
return;
}
}

return {
Expand All @@ -484,7 +531,7 @@ export async function convertGithubNwoToDatabaseUrl(
}
}

async function promptForLanguage(
export async function promptForLanguage(
languages: string[],
progress: ProgressCallback,
): Promise<string | undefined> {
Expand Down
14 changes: 14 additions & 0 deletions extensions/ql-vscode/src/local-databases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,20 @@ export class DatabaseManager extends DisposableObject {
}
}

public async digForDatabaseItem(
language: string,
databaseNwo: string,
): Promise<DatabaseItem | undefined> {
const dbItems = this.databaseItems || [];
const dbs = dbItems.filter(
(db) => db.language === language && db.name === databaseNwo,
);
Comment thread
elenatanasoiu marked this conversation as resolved.
if (dbs.length === 0) {
return undefined;
}
return dbs[0];
}
Comment thread
koesie10 marked this conversation as resolved.

/**
* Returns the index of the workspace folder that corresponds to the source archive of `item`
* if there is one, and -1 otherwise.
Expand Down
24 changes: 23 additions & 1 deletion extensions/ql-vscode/src/local-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
window,
} from "vscode";
import { BaseLogger, extLogger, Logger, TeeLogger } from "./common";
import { MAX_QUERIES } from "./config";
import { isCanary, MAX_QUERIES } from "./config";
import { gatherQlFiles } from "./pure/files";
import { basename } from "path";
import {
Expand Down Expand Up @@ -51,6 +51,7 @@ import { App } from "./common/app";
import { DisposableObject } from "./pure/disposable-object";
import { QueryResultType } from "./pure/new-messages";
import { redactableError } from "./pure/errors";
import { SkeletonQueryWizard } from "./skeleton-query-wizard";

interface DatabaseQuickPickItem extends QuickPickItem {
databaseItem: DatabaseItem;
Expand Down Expand Up @@ -237,6 +238,7 @@ export class LocalQueries extends DisposableObject {
"codeQL.quickEvalContextEditor": this.quickEval.bind(this),
"codeQL.codeLensQuickEval": this.codeLensQuickEval.bind(this),
"codeQL.quickQuery": this.quickQuery.bind(this),
"codeQL.createSkeletonQuery": this.createSkeletonQuery.bind(this),
};
}

Expand Down Expand Up @@ -375,6 +377,26 @@ export class LocalQueries extends DisposableObject {
);
}

private async createSkeletonQuery(): Promise<void> {
await withProgress(
async (progress: ProgressCallback, token: CancellationToken) => {
const credentials = isCanary() ? this.app.credentials : undefined;
const skeletonQueryWizard = new SkeletonQueryWizard(
this.cliServer,
progress,
credentials,
extLogger,
this.databaseManager,
token,
);
await skeletonQueryWizard.execute();
},
{
title: "Create Query",
},
);
}

/**
* Creates a new `LocalQueryRun` object to track a query evaluation. This creates a timestamp
* file in the query's output directory, creates a `LocalQueryInfo` object, and registers that
Expand Down
4 changes: 2 additions & 2 deletions extensions/ql-vscode/src/qlpack-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export class QlPackGenerator {
await writeFile(qlPackFilePath, this.header + dump(qlPackYml), "utf8");
}

private async createExampleQlFile() {
const exampleQlFilePath = join(this.folderUri.fsPath, "example.ql");
public async createExampleQlFile(fileName = "example.ql") {
const exampleQlFilePath = join(this.folderUri.fsPath, fileName);

const exampleQl = `
/**
Expand Down
Loading