|
| 1 | +import { CodeqlDatabase } from "./github-database-api"; |
| 2 | +import { DatabaseItem, DatabaseManager } from "./local-databases"; |
| 3 | +import { Octokit } from "@octokit/rest"; |
| 4 | +import { CodeQLCliServer } from "../codeql-cli/cli"; |
| 5 | +import { AppCommandManager } from "../common/commands"; |
| 6 | +import { getLanguageDisplayName } from "../common/query-language"; |
| 7 | +import { showNeverAskAgainDialog } from "../common/vscode/dialog"; |
| 8 | +import { downloadGitHubDatabaseFromUrl } from "./database-fetcher"; |
| 9 | +import { withProgress } from "../common/vscode/progress"; |
| 10 | +import { window } from "vscode"; |
| 11 | +import { GitHubDatabaseConfig } from "../config"; |
| 12 | +import { joinLanguages, promptForDatabases } from "./github-database-download"; |
| 13 | + |
| 14 | +export type DatabaseUpdate = { |
| 15 | + database: CodeqlDatabase; |
| 16 | + databaseItem: DatabaseItem; |
| 17 | +}; |
| 18 | + |
| 19 | +type DatabaseUpdateStatusUpdateAvailable = { |
| 20 | + type: "updateAvailable"; |
| 21 | + databaseUpdates: DatabaseUpdate[]; |
| 22 | +}; |
| 23 | + |
| 24 | +type DatabaseUpdateStatusUpToDate = { |
| 25 | + type: "upToDate"; |
| 26 | +}; |
| 27 | + |
| 28 | +type DatabaseUpdateStatusNoDatabase = { |
| 29 | + type: "noDatabase"; |
| 30 | +}; |
| 31 | + |
| 32 | +type DatabaseUpdateStatus = |
| 33 | + | DatabaseUpdateStatusUpdateAvailable |
| 34 | + | DatabaseUpdateStatusUpToDate |
| 35 | + | DatabaseUpdateStatusNoDatabase; |
| 36 | + |
| 37 | +export function isNewerDatabaseAvailable( |
| 38 | + databases: CodeqlDatabase[], |
| 39 | + owner: string, |
| 40 | + name: string, |
| 41 | + databaseManager: DatabaseManager, |
| 42 | +): DatabaseUpdateStatus { |
| 43 | + // Sorted by date added ascending |
| 44 | + const existingDatabasesForRepository = databaseManager.databaseItems |
| 45 | + .filter( |
| 46 | + (db) => |
| 47 | + db.origin?.type === "github" && |
| 48 | + db.origin.repository === `${owner}/${name}`, |
| 49 | + ) |
| 50 | + .sort((a, b) => (a.dateAdded ?? 0) - (b.dateAdded ?? 0)); |
| 51 | + |
| 52 | + if (existingDatabasesForRepository.length === 0) { |
| 53 | + return { |
| 54 | + type: "noDatabase", |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + // Sort order is guaranteed by the sort call above. The newest database is the last one. |
| 59 | + const newestExistingDatabasesByLanguage = new Map<string, DatabaseItem>(); |
| 60 | + for (const existingDatabase of existingDatabasesForRepository) { |
| 61 | + newestExistingDatabasesByLanguage.set( |
| 62 | + existingDatabase.language, |
| 63 | + existingDatabase, |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + const databaseUpdates = Array.from(newestExistingDatabasesByLanguage.values()) |
| 68 | + .map((newestExistingDatabase): DatabaseUpdate | null => { |
| 69 | + const origin = newestExistingDatabase.origin; |
| 70 | + if (origin?.type !== "github") { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + const matchingDatabase = databases.find( |
| 75 | + (db) => db.language === newestExistingDatabase.language, |
| 76 | + ); |
| 77 | + if (!matchingDatabase) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + if (matchingDatabase.commit_oid === origin.commitOid) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + return { |
| 86 | + database: matchingDatabase, |
| 87 | + databaseItem: newestExistingDatabase, |
| 88 | + }; |
| 89 | + }) |
| 90 | + .filter((update): update is DatabaseUpdate => update !== null) |
| 91 | + .sort((a, b) => a.database.language.localeCompare(b.database.language)); |
| 92 | + |
| 93 | + if (databaseUpdates.length === 0) { |
| 94 | + return { |
| 95 | + type: "upToDate", |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + return { |
| 100 | + type: "updateAvailable", |
| 101 | + databaseUpdates, |
| 102 | + }; |
| 103 | +} |
| 104 | + |
| 105 | +export async function askForGitHubDatabaseUpdate( |
| 106 | + updates: DatabaseUpdate[], |
| 107 | + config: GitHubDatabaseConfig, |
| 108 | +): Promise<boolean> { |
| 109 | + const languages = updates.map((update) => update.database.language); |
| 110 | + |
| 111 | + const message = |
| 112 | + updates.length === 1 |
| 113 | + ? `There is a newer ${getLanguageDisplayName( |
| 114 | + languages[0], |
| 115 | + )} CodeQL database available for this repository. Download the database update from GitHub?` |
| 116 | + : `There are newer ${joinLanguages( |
| 117 | + languages, |
| 118 | + )} CodeQL databases available for this repository. Download the database updates from GitHub?`; |
| 119 | + |
| 120 | + const answer = await showNeverAskAgainDialog( |
| 121 | + message, |
| 122 | + false, |
| 123 | + "Download", |
| 124 | + "Not now", |
| 125 | + "Never", |
| 126 | + ); |
| 127 | + |
| 128 | + if (answer === "Not now" || answer === undefined) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + if (answer === "Never") { |
| 133 | + await config.setUpdate("never"); |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + return true; |
| 138 | +} |
| 139 | + |
| 140 | +export async function downloadDatabaseUpdateFromGitHub( |
| 141 | + octokit: Octokit, |
| 142 | + owner: string, |
| 143 | + repo: string, |
| 144 | + updates: DatabaseUpdate[], |
| 145 | + databaseManager: DatabaseManager, |
| 146 | + storagePath: string, |
| 147 | + cliServer: CodeQLCliServer, |
| 148 | + commandManager: AppCommandManager, |
| 149 | +): Promise<void> { |
| 150 | + const selectedDatabases = await promptForDatabases( |
| 151 | + updates.map((update) => update.database), |
| 152 | + { |
| 153 | + title: "Select databases to update", |
| 154 | + }, |
| 155 | + ); |
| 156 | + if (selectedDatabases.length === 0) { |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + await Promise.all( |
| 161 | + selectedDatabases.map((database) => { |
| 162 | + const update = updates.find((update) => update.database === database); |
| 163 | + if (!update) { |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | + return withProgress( |
| 168 | + async (progress) => { |
| 169 | + const newDatabase = await downloadGitHubDatabaseFromUrl( |
| 170 | + database.url, |
| 171 | + database.id, |
| 172 | + database.created_at, |
| 173 | + database.commit_oid ?? null, |
| 174 | + owner, |
| 175 | + repo, |
| 176 | + octokit, |
| 177 | + progress, |
| 178 | + databaseManager, |
| 179 | + storagePath, |
| 180 | + cliServer, |
| 181 | + databaseManager.currentDatabaseItem === update.databaseItem, |
| 182 | + update.databaseItem.hasSourceArchiveInExplorer(), |
| 183 | + ); |
| 184 | + if (newDatabase === undefined) { |
| 185 | + return; |
| 186 | + } |
| 187 | + |
| 188 | + await databaseManager.removeDatabaseItem(update.databaseItem); |
| 189 | + |
| 190 | + await commandManager.execute("codeQLDatabases.focus"); |
| 191 | + void window.showInformationMessage( |
| 192 | + `Updated ${getLanguageDisplayName( |
| 193 | + database.language, |
| 194 | + )} database from GitHub.`, |
| 195 | + ); |
| 196 | + }, |
| 197 | + { |
| 198 | + title: `Updating ${getLanguageDisplayName( |
| 199 | + database.language, |
| 200 | + )} database from GitHub`, |
| 201 | + }, |
| 202 | + ); |
| 203 | + }), |
| 204 | + ); |
| 205 | +} |
0 commit comments