@@ -4,23 +4,23 @@ import { Uri, ProgressOptions, ProgressLocation, commands, window } from "vscode
44import * as fs from "fs-extra" ;
55import * as path from "path" ;
66import { DatabaseManager , DatabaseItem } from "./databases" ;
7- import { ProgressCallback , showAndLogErrorMessage , withProgress } from "./helpers" ;
7+ import { ProgressCallback , showAndLogErrorMessage , withProgress , showAndLogInformationMessage } from "./helpers" ;
88
99/**
1010 * Prompts a user to fetch a database from a remote location. Database is assumed to be an archive file.
1111 *
1212 * @param databasesManager the DatabaseManager
1313 * @param storagePath where to store the unzipped database.
1414 */
15- export default async function promptFetchDatabase ( databasesManager : DatabaseManager , storagePath : string ) : Promise < DatabaseItem | undefined > {
15+ export async function promptImportInternetDatabase ( databasesManager : DatabaseManager , storagePath : string ) : Promise < DatabaseItem | undefined > {
1616 let item : DatabaseItem | undefined = undefined ;
1717
1818 try {
1919 const databaseUrl = await window . showInputBox ( {
2020 prompt : 'Enter URL of zipfile of database to download'
2121 } ) ;
2222 if ( databaseUrl ) {
23- validateUrl ( databaseUrl ) ;
23+ validateHttpsUrl ( databaseUrl ) ;
2424
2525 const progressOptions : ProgressOptions = {
2626 location : ProgressLocation . Notification ,
@@ -30,13 +30,41 @@ export default async function promptFetchDatabase(databasesManager: DatabaseMana
3030 await withProgress ( progressOptions , async progress => ( item = await databaseArchiveFetcher ( databaseUrl , databasesManager , storagePath , progress ) ) ) ;
3131 commands . executeCommand ( 'codeQLDatabases.focus' ) ;
3232 }
33+ showAndLogInformationMessage ( 'Database downloaded and imported successfully.' ) ;
3334 } catch ( e ) {
3435 showAndLogErrorMessage ( e . message ) ;
3536 }
3637
3738 return item ;
3839}
3940
41+
42+ /**
43+ * Imports a database from a local archive.
44+ *
45+ * @param databaseUrl the file url of the archive to import
46+ * @param databasesManager the DatabaseManager
47+ * @param storagePath where to store the unzipped database.
48+ */
49+ export async function importArchiveDatabase ( databaseUrl : string , databasesManager : DatabaseManager , storagePath : string ) : Promise < DatabaseItem | undefined > {
50+ let item : DatabaseItem | undefined = undefined ;
51+ try {
52+ const progressOptions : ProgressOptions = {
53+ location : ProgressLocation . Notification ,
54+ title : 'Importing database from archive' ,
55+ cancellable : false ,
56+ } ;
57+ await withProgress ( progressOptions , async progress => ( item = await databaseArchiveFetcher ( databaseUrl , databasesManager , storagePath , progress ) ) ) ;
58+ commands . executeCommand ( 'codeQLDatabases.focus' ) ;
59+
60+ showAndLogInformationMessage ( 'Database unzipped and imported successfully.' ) ;
61+ } catch ( e ) {
62+ showAndLogErrorMessage ( e . message ) ;
63+ }
64+ return item ;
65+ }
66+
67+
4068/**
4169 * Fetches an archive database. The database might be on the internet
4270 * or in the local filesystem.
@@ -46,15 +74,15 @@ export default async function promptFetchDatabase(databasesManager: DatabaseMana
4674 * @param storagePath where to store the unzipped database.
4775 * @param progressCallback optional callback to send progress messages to
4876 */
49- export async function databaseArchiveFetcher (
77+ async function databaseArchiveFetcher (
5078 databaseUrl : string ,
5179 databasesManager : DatabaseManager ,
5280 storagePath : string ,
5381 progressCallback ?: ProgressCallback
5482) : Promise < DatabaseItem > {
5583 progressCallback ?.( {
5684 maxStep : 3 ,
57- message : 'Downloading database' ,
85+ message : 'Getting database' ,
5886 step : 1
5987 } ) ;
6088 if ( ! storagePath ) {
@@ -75,6 +103,7 @@ export async function databaseArchiveFetcher(
75103 step : 3
76104 } ) ;
77105
106+ // find the path to the database. The actual database might be in a sub-folder
78107 const dbPath = await findDirWithFile ( unzipPath , '.dbinfo' , 'codeql-database.yml' ) ;
79108 if ( dbPath ) {
80109 const item = await databasesManager . openDatabase ( Uri . parse ( dbPath ) ) ;
@@ -106,7 +135,7 @@ async function getStorageFolder(storagePath: string, urlStr: string) {
106135}
107136
108137
109- function validateUrl ( databaseUrl : string ) {
138+ function validateHttpsUrl ( databaseUrl : string ) {
110139 let uri ;
111140 try {
112141 uri = Uri . parse ( databaseUrl , true ) ;
0 commit comments