Skip to content

Commit 7b3a55e

Browse files
committed
Refactor handling of updateStatus
1 parent a0295d6 commit 7b3a55e

1 file changed

Lines changed: 63 additions & 34 deletions

File tree

extensions/ql-vscode/src/databases/github-database-module.ts

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ import {
1111
import { GitHubDatabaseConfig, GitHubDatabaseConfigListener } from "../config";
1212
import { DatabaseManager } from "./local-databases";
1313
import { 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";
1519
import {
1620
askForGitHubDatabaseUpdate,
21+
DatabaseUpdate,
1722
downloadDatabaseUpdateFromGitHub,
1823
isNewerDatabaseAvailable,
1924
} from "./github-database-updates";
25+
import { Octokit } from "@octokit/rest";
2026

2127
export class GithubDatabaseModule extends DisposableObject {
2228
private readonly config: GitHubDatabaseConfig;
@@ -131,42 +137,39 @@ export class GithubDatabaseModule extends DisposableObject {
131137
githubRepository.name,
132138
this.databaseManager,
133139
);
134-
if (updateStatus.type === "upToDate") {
135-
return;
136-
}
137140

138-
if (updateStatus.type === "updateAvailable") {
139-
if (this.config.update === "never") {
141+
switch (updateStatus.type) {
142+
case "upToDate":
140143
return;
141-
}
142-
143-
if (
144-
!(await askForGitHubDatabaseUpdate(
144+
case "updateAvailable":
145+
await this.updateGitHubDatabase(
146+
octokit,
147+
githubRepository.owner,
148+
githubRepository.name,
145149
updateStatus.databaseUpdates,
146-
this.config,
147-
))
148-
) {
149-
return;
150-
}
151-
152-
await downloadDatabaseUpdateFromGitHub(
153-
octokit,
154-
githubRepository.owner,
155-
githubRepository.name,
156-
updateStatus.databaseUpdates,
157-
this.databaseManager,
158-
this.databaseStoragePath,
159-
this.cliServer,
160-
this.app.commands,
161-
);
162-
163-
return;
164-
}
165-
166-
if (updateStatus.type !== "noDatabase") {
167-
assertNever(updateStatus);
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);
168163
}
164+
}
169165

166+
private async downloadGitHubDatabase(
167+
octokit: Octokit,
168+
owner: string,
169+
repo: string,
170+
databases: CodeqlDatabase[],
171+
promptedForCredentials: boolean,
172+
) {
170173
// If the user already had an access token, first ask if they even want to download the DB.
171174
if (!promptedForCredentials) {
172175
if (!(await askForGitHubDatabaseDownload(databases, this.config))) {
@@ -176,13 +179,39 @@ export class GithubDatabaseModule extends DisposableObject {
176179

177180
await downloadDatabaseFromGitHub(
178181
octokit,
179-
githubRepository.owner,
180-
githubRepository.name,
182+
owner,
183+
repo,
181184
databases,
182185
this.databaseManager,
183186
this.databaseStoragePath,
184187
this.cliServer,
185188
this.app.commands,
186189
);
187190
}
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+
}
188217
}

0 commit comments

Comments
 (0)