Skip to content

Commit 7a8cf55

Browse files
authored
Merge pull request #1294 from github/aeisenberg/db-name-github
Display nicer names for github-downloaded databases
2 parents 7932de3 + 8f2d865 commit 7a8cf55

3 files changed

Lines changed: 32 additions & 12 deletions

File tree

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export async function promptImportInternetDatabase(
5151
{},
5252
databaseManager,
5353
storagePath,
54+
undefined,
5455
progress,
5556
token,
5657
cli
@@ -98,11 +99,13 @@ export async function promptImportGithubDatabase(
9899
throw new Error(`Invalid GitHub repository: ${githubRepo}`);
99100
}
100101

101-
const databaseUrl = await convertGithubNwoToDatabaseUrl(githubRepo, credentials, progress);
102-
if (!databaseUrl) {
102+
const result = await convertGithubNwoToDatabaseUrl(githubRepo, credentials, progress);
103+
if (!result) {
103104
return;
104105
}
105106

107+
const { databaseUrl, name, owner } = result;
108+
106109
const octokit = await credentials.getOctokit();
107110
/**
108111
* The 'token' property of the token object returned by `octokit.auth()`.
@@ -125,6 +128,7 @@ export async function promptImportGithubDatabase(
125128
{ 'Accept': 'application/zip', 'Authorization': `Bearer ${octokitToken}` },
126129
databaseManager,
127130
storagePath,
131+
`${owner}/${name}`,
128132
progress,
129133
token,
130134
cli
@@ -173,6 +177,7 @@ export async function promptImportLgtmDatabase(
173177
{},
174178
databaseManager,
175179
storagePath,
180+
undefined,
176181
progress,
177182
token,
178183
cli
@@ -220,6 +225,7 @@ export async function importArchiveDatabase(
220225
{},
221226
databaseManager,
222227
storagePath,
228+
undefined,
223229
progress,
224230
token,
225231
cli
@@ -247,6 +253,7 @@ export async function importArchiveDatabase(
247253
* @param requestHeaders Headers to send with the request
248254
* @param databaseManager the DatabaseManager
249255
* @param storagePath where to store the unzipped database.
256+
* @param nameOverride a name for the database that overrides the default
250257
* @param progress callback to send progress messages to
251258
* @param token cancellation token
252259
*/
@@ -255,6 +262,7 @@ async function databaseArchiveFetcher(
255262
requestHeaders: { [key: string]: string },
256263
databaseManager: DatabaseManager,
257264
storagePath: string,
265+
nameOverride: string | undefined,
258266
progress: ProgressCallback,
259267
token: CancellationToken,
260268
cli?: CodeQLCliServer,
@@ -296,7 +304,7 @@ async function databaseArchiveFetcher(
296304
});
297305
await ensureZippedSourceLocation(dbPath);
298306

299-
const item = await databaseManager.openDatabase(progress, token, Uri.file(dbPath));
307+
const item = await databaseManager.openDatabase(progress, token, Uri.file(dbPath), nameOverride);
300308
await databaseManager.setCurrentDatabaseItem(item);
301309
return item;
302310
} else {
@@ -409,7 +417,6 @@ async function fetchAndUnzip(
409417

410418
await readAndUnzip(Uri.file(archivePath).toString(true), unzipPath, cli, progress);
411419

412-
413420
// remove archivePath eagerly since these archives can be large.
414421
await fs.remove(archivePath);
415422
}
@@ -517,7 +524,11 @@ function convertGitHubUrlToNwo(githubUrl: string): string | undefined {
517524
export async function convertGithubNwoToDatabaseUrl(
518525
githubRepo: string,
519526
credentials: Credentials,
520-
progress: ProgressCallback): Promise<string | undefined> {
527+
progress: ProgressCallback): Promise<{
528+
databaseUrl: string,
529+
owner: string,
530+
name: string
531+
} | undefined> {
521532
try {
522533
const nwo = convertGitHubUrlToNwo(githubRepo) || githubRepo;
523534
const [owner, repo] = nwo.split('/');
@@ -532,7 +543,11 @@ export async function convertGithubNwoToDatabaseUrl(
532543
return;
533544
}
534545

535-
return `https://api.github.com/repos/${owner}/${repo}/code-scanning/codeql/databases/${language}`;
546+
return {
547+
databaseUrl: `https://api.github.com/repos/${owner}/${repo}/code-scanning/codeql/databases/${language}`,
548+
owner,
549+
name: repo
550+
};
536551

537552
} catch (e) {
538553
void logger.log(`Error: ${getErrorMessage(e)}`);

extensions/ql-vscode/src/databases.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export async function findSourceArchive(
148148
}
149149

150150
async function resolveDatabase(
151-
databasePath: string
151+
databasePath: string,
152152
): Promise<DatabaseContents> {
153153

154154
const name = path.basename(databasePath);
@@ -170,7 +170,9 @@ async function getDbSchemeFiles(dbDirectory: string): Promise<string[]> {
170170
return await glob('*.dbscheme', { cwd: dbDirectory });
171171
}
172172

173-
async function resolveDatabaseContents(uri: vscode.Uri): Promise<DatabaseContents> {
173+
async function resolveDatabaseContents(
174+
uri: vscode.Uri,
175+
): Promise<DatabaseContents> {
174176
if (uri.scheme !== 'file') {
175177
throw new Error(`Database URI scheme '${uri.scheme}' not supported; only 'file' URIs are supported.`);
176178
}
@@ -569,14 +571,15 @@ export class DatabaseManager extends DisposableObject {
569571
progress: ProgressCallback,
570572
token: vscode.CancellationToken,
571573
uri: vscode.Uri,
574+
displayName?: string
572575
): Promise<DatabaseItem> {
573576
const contents = await resolveDatabaseContents(uri);
574577
// Ignore the source archive for QLTest databases by default.
575578
const isQLTestDatabase = path.extname(uri.fsPath) === '.testproj';
576579
const fullOptions: FullDatabaseOptions = {
577580
ignoreSourceArchive: isQLTestDatabase,
578-
// displayName is only set if a user explicitly renames a database
579-
displayName: undefined,
581+
// If a displayName is not passed in, the basename of folder containing the database is used.
582+
displayName,
580583
dateAdded: Date.now(),
581584
language: await this.getPrimaryLanguage(uri.fsPath)
582585
};

extensions/ql-vscode/src/vscode-tests/no-workspace/databaseFetcher.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,17 @@ describe('databaseFetcher', function() {
9191
mockRequest.resolves(mockApiResponse);
9292
quickPickSpy.resolves('javascript');
9393
const githubRepo = 'github/codeql';
94-
const dbUrl = await mod.convertGithubNwoToDatabaseUrl(
94+
const { databaseUrl, name, owner } = await mod.convertGithubNwoToDatabaseUrl(
9595
githubRepo,
9696
credentials,
9797
progressSpy
9898
);
9999

100-
expect(dbUrl).to.equal(
100+
expect(databaseUrl).to.equal(
101101
'https://api.github.com/repos/github/codeql/code-scanning/codeql/databases/javascript'
102102
);
103+
expect(name).to.equal('codeql');
104+
expect(owner).to.equal('github');
103105
expect(quickPickSpy.firstCall.args[0]).to.deep.equal([
104106
'csharp',
105107
'javascript',

0 commit comments

Comments
 (0)