@@ -2,22 +2,58 @@ import { DisposableObject } from "../common/disposable-object";
22import { App } from "../common/app" ;
33import { findGitHubRepositoryForWorkspace } from "./github-repository-finder" ;
44import { redactableError } from "../common/errors" ;
5- import { asError } from "../common/helpers-pure" ;
5+ import { asError , getErrorMessage } from "../common/helpers-pure" ;
6+ import {
7+ CodeqlDatabase ,
8+ findGitHubDatabasesForRepository ,
9+ promptAndDownloadGitHubDatabase ,
10+ } from "./github-database-prompt" ;
11+ import {
12+ GitHubDatabaseConfig ,
13+ GitHubDatabaseConfigListener ,
14+ isCanary ,
15+ } from "../config" ;
16+ import { AppOctokit } from "../common/octokit" ;
17+ import { DatabaseManager } from "./local-databases" ;
18+ import { CodeQLCliServer } from "../codeql-cli/cli" ;
619
720export class GithubDatabaseModule extends DisposableObject {
8- private constructor ( private readonly app : App ) {
21+ private readonly config : GitHubDatabaseConfig ;
22+
23+ private constructor (
24+ private readonly app : App ,
25+ private readonly databaseManager : DatabaseManager ,
26+ private readonly databaseStoragePath : string ,
27+ private readonly cliServer : CodeQLCliServer ,
28+ ) {
929 super ( ) ;
30+
31+ this . config = this . push ( new GitHubDatabaseConfigListener ( ) ) ;
1032 }
1133
12- public static async initialize ( app : App ) : Promise < GithubDatabaseModule > {
13- const githubDatabaseModule = new GithubDatabaseModule ( app ) ;
34+ public static async initialize (
35+ app : App ,
36+ databaseManager : DatabaseManager ,
37+ databaseStoragePath : string ,
38+ cliServer : CodeQLCliServer ,
39+ ) : Promise < GithubDatabaseModule > {
40+ const githubDatabaseModule = new GithubDatabaseModule (
41+ app ,
42+ databaseManager ,
43+ databaseStoragePath ,
44+ cliServer ,
45+ ) ;
1446 app . subscriptions . push ( githubDatabaseModule ) ;
1547
1648 await githubDatabaseModule . initialize ( ) ;
1749 return githubDatabaseModule ;
1850 }
1951
2052 private async initialize ( ) : Promise < void > {
53+ if ( ! this . config . enable ) {
54+ return ;
55+ }
56+
2157 // Start the check and downloading the database asynchronously. We don't want to block on this
2258 // in extension activation since this makes network requests and waits for user input.
2359 void this . promptGitHubRepositoryDownload ( ) . catch ( ( e : unknown ) => {
@@ -31,6 +67,10 @@ export class GithubDatabaseModule extends DisposableObject {
3167 }
3268
3369 private async promptGitHubRepositoryDownload ( ) : Promise < void > {
70+ if ( this . config . download === "never" ) {
71+ return ;
72+ }
73+
3474 const githubRepositoryResult = await findGitHubRepositoryForWorkspace ( ) ;
3575 if ( githubRepositoryResult . isFailure ) {
3676 void this . app . logger . log (
@@ -42,8 +82,58 @@ export class GithubDatabaseModule extends DisposableObject {
4282 }
4383
4484 const githubRepository = githubRepositoryResult . value ;
45- void this . app . logger . log (
46- `Found GitHub repository for workspace: '${ githubRepository . owner } /${ githubRepository . name } '` ,
85+
86+ const hasExistingDatabase = this . databaseManager . databaseItems . some (
87+ ( db ) =>
88+ db . origin ?. type === "github" &&
89+ db . origin . repository ===
90+ `${ githubRepository . owner } /${ githubRepository . name } ` ,
91+ ) ;
92+ if ( hasExistingDatabase ) {
93+ return ;
94+ }
95+
96+ const credentials = isCanary ( ) ? this . app . credentials : undefined ;
97+
98+ const octokit = credentials
99+ ? await credentials . getOctokit ( )
100+ : new AppOctokit ( ) ;
101+
102+ let databases : CodeqlDatabase [ ] ;
103+ try {
104+ databases = await findGitHubDatabasesForRepository (
105+ octokit ,
106+ githubRepository . owner ,
107+ githubRepository . name ,
108+ ) ;
109+ } catch ( e ) {
110+ this . app . telemetry ?. sendError (
111+ redactableError (
112+ asError ( e ) ,
113+ ) `Failed to prompt for GitHub database download` ,
114+ ) ;
115+
116+ void this . app . logger . log (
117+ `Failed to find GitHub databases for repository: ${ getErrorMessage ( e ) } ` ,
118+ ) ;
119+
120+ return ;
121+ }
122+
123+ if ( databases . length === 0 ) {
124+ return ;
125+ }
126+
127+ await promptAndDownloadGitHubDatabase (
128+ octokit ,
129+ githubRepository . owner ,
130+ githubRepository . name ,
131+ databases ,
132+ this . config ,
133+ this . databaseManager ,
134+ this . databaseStoragePath ,
135+ this . cliServer ,
136+ this . app . commands ,
47137 ) ;
48138 }
49139}
0 commit comments