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