Don't offer to create skeleton pack again

When running Create Query in the codespaces-codeql repo, it successfully
creates codeql-custom-queries-xxx as a subfolder of the first workspace
folder, and then adds a database.  After the database gets added, we get
prompted with this message:

```
We've noticed you don't have a CodeQL pack available to analyze this
database. Can we set up a query pack for you?
```

which would try to create another QL pack.

Since we're no longer pushing QL packs as top level folders in the
workspace when we use the new "Create Query" flow, we also need to adapt
the original flow to take into account subfolders.

Just as a reminder, the original flow is:
- Be in the codespace template
- Download a database from GitHub
- The extension will offer to create a QL pack for the database

The new flow:
- Run the "Create Query" command
- Choose a language
- Create a QL pack
- Download a database for it

In the new flow the last step of downloading a database would trigger
the extension to offer to create a QL pack.

Let's fix this by detecting subfolders as well and exiting early.
This commit is contained in:
Elena Tanasoiu
2023-04-14 09:50:15 +00:00
parent b6eaf93dba
commit 4a8ba1377d
3 changed files with 27 additions and 6 deletions

View File

@@ -30,6 +30,7 @@ import { isCodespacesTemplate } from "./config";
import { QlPackGenerator } from "./qlpack-generator";
import { QueryLanguage } from "./common/query-language";
import { App } from "./common/app";
import { existsSync } from "fs";
/**
* databases.ts
@@ -663,8 +664,13 @@ export class DatabaseManager extends DisposableObject {
return;
}
const firstWorkspaceFolder = getFirstWorkspaceFolder();
const folderName = `codeql-custom-queries-${databaseItem.language}`;
if (isFolderAlreadyInWorkspace(folderName)) {
if (
existsSync(join(firstWorkspaceFolder, folderName)) ||
isFolderAlreadyInWorkspace(folderName)
) {
return;
}
@@ -677,8 +683,6 @@ export class DatabaseManager extends DisposableObject {
}
try {
const firstWorkspaceFolder = getFirstWorkspaceFolder();
const qlPackGenerator = new QlPackGenerator(
folderName,
databaseItem.language as QueryLanguage,

View File

@@ -14,6 +14,7 @@ import { QlPackGenerator } from "./qlpack-generator";
import { DatabaseItem, DatabaseManager } from "./local-databases";
import { ProgressCallback, UserCancellationException } from "./progress";
import { askForGitHubRepo, downloadGitHubDatabase } from "./databaseFetcher";
import { existsSync } from "fs";
type QueryLanguagesToDatabaseMap = Record<string, string>;
@@ -56,9 +57,9 @@ export class SkeletonQueryWizard {
this.qlPackStoragePath = getFirstWorkspaceFolder();
const skeletonPackAlreadyExists = isFolderAlreadyInWorkspace(
this.folderName,
);
const skeletonPackAlreadyExists =
existsSync(join(this.qlPackStoragePath, this.folderName)) ||
isFolderAlreadyInWorkspace(this.folderName);
if (skeletonPackAlreadyExists) {
// just create a new example query file in skeleton QL pack

View File

@@ -687,6 +687,22 @@ describe("local databases", () => {
);
});
});
describe("when the QL pack already exists", () => {
beforeEach(() => {
fs.mkdirSync(join(dir.name, `codeql-custom-queries-${language}`));
});
it("should exit early", async () => {
showBinaryChoiceDialogSpy = jest
.spyOn(helpers, "showBinaryChoiceDialog")
.mockResolvedValue(false);
await (databaseManager as any).createSkeletonPacks(mockDbItem);
expect(generateSpy).not.toBeCalled();
});
});
});
describe("openDatabase", () => {