@@ -3,15 +3,26 @@ import { DisposableObject } from "../common/disposable-object";
33import { App } from "../common/app" ;
44import { findGitHubRepositoryForWorkspace } from "./github-repository-finder" ;
55import { redactableError } from "../common/errors" ;
6- import { asError , getErrorMessage } from "../common/helpers-pure" ;
6+ import { asError , assertNever , getErrorMessage } from "../common/helpers-pure" ;
77import {
88 askForGitHubDatabaseDownload ,
99 downloadDatabaseFromGitHub ,
1010} from "./github-database-download" ;
1111import { GitHubDatabaseConfig , GitHubDatabaseConfigListener } from "../config" ;
1212import { DatabaseManager } from "./local-databases" ;
1313import { CodeQLCliServer } from "../codeql-cli/cli" ;
14- import { listDatabases , ListDatabasesResult } from "./github-database-api" ;
14+ import {
15+ CodeqlDatabase ,
16+ listDatabases ,
17+ ListDatabasesResult ,
18+ } from "./github-database-api" ;
19+ import {
20+ askForGitHubDatabaseUpdate ,
21+ DatabaseUpdate ,
22+ downloadDatabaseUpdateFromGitHub ,
23+ isNewerDatabaseAvailable ,
24+ } from "./github-database-updates" ;
25+ import { Octokit } from "@octokit/rest" ;
1526
1627export class GithubDatabaseModule extends DisposableObject {
1728 private readonly config : GitHubDatabaseConfig ;
@@ -79,16 +90,6 @@ export class GithubDatabaseModule extends DisposableObject {
7990
8091 const githubRepository = githubRepositoryResult . value ;
8192
82- const hasExistingDatabase = this . databaseManager . databaseItems . some (
83- ( db ) =>
84- db . origin ?. type === "github" &&
85- db . origin . repository ===
86- `${ githubRepository . owner } /${ githubRepository . name } ` ,
87- ) ;
88- if ( hasExistingDatabase ) {
89- return ;
90- }
91-
9293 let result : ListDatabasesResult | undefined ;
9394 try {
9495 result = await listDatabases (
@@ -130,6 +131,45 @@ export class GithubDatabaseModule extends DisposableObject {
130131 return ;
131132 }
132133
134+ const updateStatus = isNewerDatabaseAvailable (
135+ databases ,
136+ githubRepository . owner ,
137+ githubRepository . name ,
138+ this . databaseManager ,
139+ ) ;
140+
141+ switch ( updateStatus . type ) {
142+ case "upToDate" :
143+ return ;
144+ case "updateAvailable" :
145+ await this . updateGitHubDatabase (
146+ octokit ,
147+ githubRepository . owner ,
148+ githubRepository . name ,
149+ updateStatus . databaseUpdates ,
150+ ) ;
151+ break ;
152+ case "noDatabase" :
153+ await this . downloadGitHubDatabase (
154+ octokit ,
155+ githubRepository . owner ,
156+ githubRepository . name ,
157+ databases ,
158+ promptedForCredentials ,
159+ ) ;
160+ break ;
161+ default :
162+ assertNever ( updateStatus ) ;
163+ }
164+ }
165+
166+ private async downloadGitHubDatabase (
167+ octokit : Octokit ,
168+ owner : string ,
169+ repo : string ,
170+ databases : CodeqlDatabase [ ] ,
171+ promptedForCredentials : boolean ,
172+ ) {
133173 // If the user already had an access token, first ask if they even want to download the DB.
134174 if ( ! promptedForCredentials ) {
135175 if ( ! ( await askForGitHubDatabaseDownload ( databases , this . config ) ) ) {
@@ -139,13 +179,39 @@ export class GithubDatabaseModule extends DisposableObject {
139179
140180 await downloadDatabaseFromGitHub (
141181 octokit ,
142- githubRepository . owner ,
143- githubRepository . name ,
182+ owner ,
183+ repo ,
144184 databases ,
145185 this . databaseManager ,
146186 this . databaseStoragePath ,
147187 this . cliServer ,
148188 this . app . commands ,
149189 ) ;
150190 }
191+
192+ private async updateGitHubDatabase (
193+ octokit : Octokit ,
194+ owner : string ,
195+ repo : string ,
196+ databaseUpdates : DatabaseUpdate [ ] ,
197+ ) : Promise < void > {
198+ if ( this . config . update === "never" ) {
199+ return ;
200+ }
201+
202+ if ( ! ( await askForGitHubDatabaseUpdate ( databaseUpdates , this . config ) ) ) {
203+ return ;
204+ }
205+
206+ await downloadDatabaseUpdateFromGitHub (
207+ octokit ,
208+ owner ,
209+ repo ,
210+ databaseUpdates ,
211+ this . databaseManager ,
212+ this . databaseStoragePath ,
213+ this . cliServer ,
214+ this . app . commands ,
215+ ) ;
216+ }
151217}
0 commit comments