@@ -4,23 +4,23 @@ import { Uri, ProgressOptions, ProgressLocation, commands, window } from "vscode
4
4
import * as fs from "fs-extra" ;
5
5
import * as path from "path" ;
6
6
import { DatabaseManager , DatabaseItem } from "./databases" ;
7
- import { ProgressCallback , showAndLogErrorMessage , withProgress } from "./helpers" ;
7
+ import { ProgressCallback , showAndLogErrorMessage , withProgress , showAndLogInformationMessage } from "./helpers" ;
8
8
9
9
/**
10
10
* Prompts a user to fetch a database from a remote location. Database is assumed to be an archive file.
11
11
*
12
12
* @param databasesManager the DatabaseManager
13
13
* @param storagePath where to store the unzipped database.
14
14
*/
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 > {
16
16
let item : DatabaseItem | undefined = undefined ;
17
17
18
18
try {
19
19
const databaseUrl = await window . showInputBox ( {
20
20
prompt : 'Enter URL of zipfile of database to download'
21
21
} ) ;
22
22
if ( databaseUrl ) {
23
- validateUrl ( databaseUrl ) ;
23
+ validateHttpsUrl ( databaseUrl ) ;
24
24
25
25
const progressOptions : ProgressOptions = {
26
26
location : ProgressLocation . Notification ,
@@ -30,13 +30,41 @@ export default async function promptFetchDatabase(databasesManager: DatabaseMana
30
30
await withProgress ( progressOptions , async progress => ( item = await databaseArchiveFetcher ( databaseUrl , databasesManager , storagePath , progress ) ) ) ;
31
31
commands . executeCommand ( 'codeQLDatabases.focus' ) ;
32
32
}
33
+ showAndLogInformationMessage ( 'Database downloaded and imported successfully.' ) ;
33
34
} catch ( e ) {
34
35
showAndLogErrorMessage ( e . message ) ;
35
36
}
36
37
37
38
return item ;
38
39
}
39
40
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
+
40
68
/**
41
69
* Fetches an archive database. The database might be on the internet
42
70
* or in the local filesystem.
@@ -46,15 +74,15 @@ export default async function promptFetchDatabase(databasesManager: DatabaseMana
46
74
* @param storagePath where to store the unzipped database.
47
75
* @param progressCallback optional callback to send progress messages to
48
76
*/
49
- export async function databaseArchiveFetcher (
77
+ async function databaseArchiveFetcher (
50
78
databaseUrl : string ,
51
79
databasesManager : DatabaseManager ,
52
80
storagePath : string ,
53
81
progressCallback ?: ProgressCallback
54
82
) : Promise < DatabaseItem > {
55
83
progressCallback ?.( {
56
84
maxStep : 3 ,
57
- message : 'Downloading database' ,
85
+ message : 'Getting database' ,
58
86
step : 1
59
87
} ) ;
60
88
if ( ! storagePath ) {
@@ -75,6 +103,7 @@ export async function databaseArchiveFetcher(
75
103
step : 3
76
104
} ) ;
77
105
106
+ // find the path to the database. The actual database might be in a sub-folder
78
107
const dbPath = await findDirWithFile ( unzipPath , '.dbinfo' , 'codeql-database.yml' ) ;
79
108
if ( dbPath ) {
80
109
const item = await databasesManager . openDatabase ( Uri . parse ( dbPath ) ) ;
@@ -106,7 +135,7 @@ async function getStorageFolder(storagePath: string, urlStr: string) {
106
135
}
107
136
108
137
109
- function validateUrl ( databaseUrl : string ) {
138
+ function validateHttpsUrl ( databaseUrl : string ) {
110
139
let uri ;
111
140
try {
112
141
uri = Uri . parse ( databaseUrl , true ) ;
0 commit comments