-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathgithub-databases-module.ts
219 lines (197 loc) · 5.82 KB
/
github-databases-module.ts
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
215
216
217
218
219
import { window } from "vscode";
import { DisposableObject } from "../../common/disposable-object";
import { App } from "../../common/app";
import { findGitHubRepositoryForWorkspace } from "../github-repository-finder";
import { redactableError } from "../../common/errors";
import {
asError,
assertNever,
getErrorMessage,
} from "../../common/helpers-pure";
import {
askForGitHubDatabaseDownload,
downloadDatabaseFromGitHub,
} from "./download";
import { GitHubDatabaseConfig } from "../../config";
import { DatabaseManager } from "../local-databases";
import { CodeQLCliServer } from "../../codeql-cli/cli";
import { CodeqlDatabase, listDatabases, ListDatabasesResult } from "./api";
import {
askForGitHubDatabaseUpdate,
DatabaseUpdate,
downloadDatabaseUpdateFromGitHub,
isNewerDatabaseAvailable,
} from "./updates";
import { Octokit } from "@octokit/rest";
export class GitHubDatabasesModule extends DisposableObject {
/**
* This constructor is public only for testing purposes. Please use the `initialize` method
* instead.
*/
constructor(
private readonly app: App,
private readonly databaseManager: DatabaseManager,
private readonly databaseStoragePath: string,
private readonly cliServer: CodeQLCliServer,
private readonly config: GitHubDatabaseConfig,
) {
super();
}
public static async initialize(
app: App,
databaseManager: DatabaseManager,
databaseStoragePath: string,
cliServer: CodeQLCliServer,
config: GitHubDatabaseConfig,
): Promise<GitHubDatabasesModule> {
const githubDatabasesModule = new GitHubDatabasesModule(
app,
databaseManager,
databaseStoragePath,
cliServer,
config,
);
app.subscriptions.push(githubDatabasesModule);
await githubDatabasesModule.initialize();
return githubDatabasesModule;
}
private async initialize(): Promise<void> {
// Start the check and downloading the database asynchronously. We don't want to block on this
// in extension activation since this makes network requests and waits for user input.
void this.promptGitHubRepositoryDownload().catch((e: unknown) => {
const error = redactableError(
asError(e),
)`Failed to prompt for GitHub repository download`;
void this.app.logger.log(error.fullMessageWithStack);
this.app.telemetry?.sendError(error);
});
}
/**
* This method is public only for testing purposes.
*/
public async promptGitHubRepositoryDownload(): Promise<void> {
if (this.config.download === "never") {
return;
}
const githubRepositoryResult = await findGitHubRepositoryForWorkspace();
if (githubRepositoryResult.isFailure) {
void this.app.logger.log(
`Did not find a GitHub repository for workspace: ${githubRepositoryResult.errors.join(
", ",
)}`,
);
return;
}
const githubRepository = githubRepositoryResult.value;
let result: ListDatabasesResult | undefined;
try {
result = await listDatabases(
githubRepository.owner,
githubRepository.name,
this.app.credentials,
this.config,
);
} catch (e) {
this.app.telemetry?.sendError(
redactableError(
asError(e),
)`Failed to prompt for GitHub database download`,
);
void this.app.logger.log(
`Failed to find GitHub databases for repository: ${getErrorMessage(e)}`,
);
return;
}
// This means the user didn't want to connect, so we can just return.
if (result === undefined) {
return;
}
const { databases, promptedForCredentials, octokit } = result;
if (databases.length === 0) {
// If the user didn't have an access token, they have already been prompted,
// so we should give feedback.
if (promptedForCredentials) {
void window.showInformationMessage(
"The GitHub repository does not have any CodeQL databases.",
);
}
return;
}
const updateStatus = isNewerDatabaseAvailable(
databases,
githubRepository.owner,
githubRepository.name,
this.databaseManager,
);
switch (updateStatus.type) {
case "upToDate":
return;
case "updateAvailable":
await this.updateGitHubDatabase(
octokit,
githubRepository.owner,
githubRepository.name,
updateStatus.databaseUpdates,
);
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))) {
return;
}
}
await downloadDatabaseFromGitHub(
octokit,
owner,
repo,
databases,
this.databaseManager,
this.databaseStoragePath,
this.cliServer,
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,
);
}
}