Refactor handling of updateStatus

This commit is contained in:
Koen Vlaswinkel
2023-11-23 10:11:36 +01:00
parent a0295d62f8
commit 7b3a55e2bf

View File

@@ -11,12 +11,18 @@ import {
import { GitHubDatabaseConfig, GitHubDatabaseConfigListener } from "../config";
import { DatabaseManager } from "./local-databases";
import { CodeQLCliServer } from "../codeql-cli/cli";
import { listDatabases, ListDatabasesResult } from "./github-database-api";
import {
CodeqlDatabase,
listDatabases,
ListDatabasesResult,
} from "./github-database-api";
import {
askForGitHubDatabaseUpdate,
DatabaseUpdate,
downloadDatabaseUpdateFromGitHub,
isNewerDatabaseAvailable,
} from "./github-database-updates";
import { Octokit } from "@octokit/rest";
export class GithubDatabaseModule extends DisposableObject {
private readonly config: GitHubDatabaseConfig;
@@ -131,42 +137,39 @@ export class GithubDatabaseModule extends DisposableObject {
githubRepository.name,
this.databaseManager,
);
if (updateStatus.type === "upToDate") {
return;
}
if (updateStatus.type === "updateAvailable") {
if (this.config.update === "never") {
switch (updateStatus.type) {
case "upToDate":
return;
}
if (
!(await askForGitHubDatabaseUpdate(
case "updateAvailable":
await this.updateGitHubDatabase(
octokit,
githubRepository.owner,
githubRepository.name,
updateStatus.databaseUpdates,
this.config,
))
) {
return;
}
await downloadDatabaseUpdateFromGitHub(
octokit,
githubRepository.owner,
githubRepository.name,
updateStatus.databaseUpdates,
this.databaseManager,
this.databaseStoragePath,
this.cliServer,
this.app.commands,
);
return;
}
if (updateStatus.type !== "noDatabase") {
assertNever(updateStatus);
);
break;
case "noDatabase":
await this.downloadGitHubDatabase(
octokit,
githubRepository.owner,
githubRepository.name,
databases,
promptedForCredentials,
);
break;
default:
assertNever(updateStatus);
}
}
private async downloadGitHubDatabase(
octokit: Octokit,
owner: string,
repo: string,
databases: CodeqlDatabase[],
promptedForCredentials: boolean,
) {
// If the user already had an access token, first ask if they even want to download the DB.
if (!promptedForCredentials) {
if (!(await askForGitHubDatabaseDownload(databases, this.config))) {
@@ -176,8 +179,8 @@ export class GithubDatabaseModule extends DisposableObject {
await downloadDatabaseFromGitHub(
octokit,
githubRepository.owner,
githubRepository.name,
owner,
repo,
databases,
this.databaseManager,
this.databaseStoragePath,
@@ -185,4 +188,30 @@ export class GithubDatabaseModule extends DisposableObject {
this.app.commands,
);
}
private async updateGitHubDatabase(
octokit: Octokit,
owner: string,
repo: string,
databaseUpdates: DatabaseUpdate[],
): Promise<void> {
if (this.config.update === "never") {
return;
}
if (!(await askForGitHubDatabaseUpdate(databaseUpdates, this.config))) {
return;
}
await downloadDatabaseUpdateFromGitHub(
octokit,
owner,
repo,
databaseUpdates,
this.databaseManager,
this.databaseStoragePath,
this.cliServer,
this.app.commands,
);
}
}