-
Notifications
You must be signed in to change notification settings - Fork 770
Expand file tree
/
Copy pathloggingOctokit.ts
More file actions
331 lines (298 loc) · 11.8 KB
/
Copy pathloggingOctokit.ts
File metadata and controls
331 lines (298 loc) · 11.8 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Octokit } from '@octokit/rest';
import { ApolloClient, ApolloQueryResult, FetchResult, MutationOptions, NormalizedCacheObject, OperationVariables, QueryOptions } from 'apollo-boost';
import { bulkhead, BulkheadPolicy } from 'cockatiel';
import * as vscode from 'vscode';
import { RateLimit } from './graphql';
import { IRawFileChange } from './interface';
import { restPaginate } from './utils';
import { GitHubRef } from '../common/githubRef';
import Logger from '../common/logger';
import { GitHubRemote } from '../common/remote';
import { ITelemetry } from '../common/telemetry';
interface RestResponse {
headers: {
'x-ratelimit-limit': string;
'x-ratelimit-remaining': string;
}
}
interface RateLimitResult {
data: {
rateLimit: RateLimit | undefined
} | undefined;
}
export enum GraphQLErrorType {
Unprocessable = 'UNPROCESSABLE',
}
export interface GraphQLError {
extensions?: {
code: string;
};
type?: GraphQLErrorType;
message?: string;
}
function isObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}
/**
* Detects whether an error from a REST (Octokit) or GraphQL (Apollo) call
* indicates that the GitHub authentication token is no longer valid. This
* happens when the token has been revoked or has expired and surfaces as
* either a 401 status, a "Bad credentials" message (REST), or a
* "401 Unauthorized" network error (GraphQL).
*/
export function isAuthError(e: unknown): boolean {
if (!isObject(e)) {
return false;
}
if (e.status === 401) {
return true;
}
const networkError = e.networkError;
if (isObject(networkError) && networkError.statusCode === 401) {
return true;
}
if (typeof e.message === 'string') {
if (e.message.includes('Bad credentials')) {
return true;
}
if (e.message.includes('401 Unauthorized')) {
return true;
}
}
return false;
}
export function getErrorCode(e: unknown): string | undefined {
if (!isObject(e)) {
return undefined;
}
if (e.status !== undefined) {
return String(e.status);
}
const networkError = e.networkError;
if (isObject(networkError) && networkError.statusCode !== undefined) {
return String(networkError.statusCode);
}
const graphQLErrors = e.graphQLErrors;
if (Array.isArray(graphQLErrors) && graphQLErrors.length > 0) {
const firstGraphQLError = graphQLErrors[0] as GraphQLError | undefined;
if (firstGraphQLError) {
if (firstGraphQLError.extensions?.code !== undefined) {
return String(firstGraphQLError.extensions.code);
}
if (firstGraphQLError.type !== undefined) {
return String(firstGraphQLError.type);
}
}
}
if (e.code !== undefined) {
return String(e.code);
}
if (typeof e.name === 'string' && e.name) {
const message = typeof e.message === 'string' ? e.message : '';
if (e.name !== 'Error') {
return message ? `${e.name}: ${message}` : e.name;
}
if (message) {
return message;
}
}
return undefined;
}
export class RateLogger {
private bulkhead: BulkheadPolicy = bulkhead(140);
private static ID = 'RateLimit';
private hasLoggedLowRateLimit: boolean = false;
private readonly _isInsiders: boolean;
constructor(private readonly telemetry: ITelemetry, private readonly errorOnFlood: boolean, private readonly authErrorHandler?: (e: unknown) => void) {
this._isInsiders = vscode.env.appName.toLowerCase().includes('insider');
}
private static sanitizeOperationName(info: string): string {
// REST URLs like /repos/{owner}/{repo}/pulls get redacted because they look
// like file paths. Convert slashes to dots to avoid redaction.
return info.replace(/\/+/g, '.').replace(/^\.+|\.+$/g, '');
}
public logAndLimit<T extends Promise<any>>(info: string | undefined, apiRequest: () => T): T | undefined {
if (this._isInsiders && info) {
/* __GDPR__
"pr.apiCall" : {
"operation" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
*/
this.telemetry.sendTelemetryEvent('pr.apiCall', { operation: RateLogger.sanitizeOperationName(info) });
}
if (this.bulkhead.executionSlots === 0) {
Logger.error('API call count has exceeded 140 concurrent calls.', RateLogger.ID);
// We have hit more than 140 concurrent API requests.
/* __GDPR__
"pr.highApiCallRate" : {}
*/
this.telemetry.sendTelemetryErrorEvent('pr.highApiCallRate');
if (!this.errorOnFlood) {
// We don't want to error on flood, so try to execute the API request anyway.
return apiRequest();
} else {
vscode.window.showErrorMessage(vscode.l10n.t('The GitHub Pull Requests extension is making too many requests to GitHub. This indicates a bug in the extension. Please file an issue on GitHub and include the output from "GitHub Pull Request".'));
return undefined;
}
}
const log = `Extension rate limit remaining: ${this.bulkhead.executionSlots}, ${info}`;
if (this.bulkhead.executionSlots < 5) {
Logger.appendLine(log, RateLogger.ID);
} else {
Logger.debug(log, RateLogger.ID);
}
return this.bulkhead.execute<T>(() => apiRequest()) as T;
}
public async logRateLimit(info: string | undefined, result: Promise<RateLimitResult | undefined>, isRest: boolean = false) {
let rateLimitInfo: { limit: number, remaining: number, cost: number } | undefined;
try {
const resolvedResult = await result;
rateLimitInfo = resolvedResult?.data?.rateLimit;
} catch (e) {
// Ignore errors here since we're just trying to log the rate limit.
return;
}
const isSearch = info?.startsWith('/search/');
if ((rateLimitInfo?.limit ?? 5000) < 5000) {
if (!isSearch) {
Logger.appendLine(`Unexpectedly low rate limit: ${rateLimitInfo?.limit}`, RateLogger.ID);
} else if ((rateLimitInfo?.limit ?? 30) < 30) {
Logger.appendLine(`Unexpectedly low SEARCH rate limit: ${rateLimitInfo?.limit}`, RateLogger.ID);
}
}
const remaining = `${isRest ? 'REST' : 'GraphQL'} Rate limit remaining: ${rateLimitInfo?.remaining}, cost: ${rateLimitInfo?.cost}, ${info}`;
if (((rateLimitInfo?.remaining ?? 1000) < 1000) && !isSearch) {
if (!this.hasLoggedLowRateLimit) {
/* __GDPR__
"pr.lowRateLimitRemaining" : {}
*/
this.telemetry.sendTelemetryErrorEvent('pr.lowRateLimitRemaining');
this.hasLoggedLowRateLimit = true;
}
Logger.warn(remaining, RateLogger.ID);
} else {
Logger.debug(remaining, RateLogger.ID);
}
}
public logApiError(info: string | undefined, apiResult: Promise<unknown>): void {
apiResult.catch(e => {
const properties: { operation: string; errorCode?: string } = {
operation: RateLogger.sanitizeOperationName(info ?? 'unknown'),
};
const errorCode = getErrorCode(e);
if (errorCode) {
properties.errorCode = errorCode;
}
/* __GDPR__
"pr.apiCallFailed" : {
"operation": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth" },
"errorCode": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth" }
}
*/
this.telemetry.sendTelemetryErrorEvent('pr.apiCallFailed', properties);
if (this.authErrorHandler && isAuthError(e)) {
try {
this.authErrorHandler(e);
} catch {
// Ignore errors from the auth error handler so they don't propagate.
}
}
});
}
public async logRestRateLimit(info: string | undefined, restResponse: Promise<RestResponse>) {
let result;
try {
result = await restResponse;
} catch (e) {
// Ignore errors here since we're just trying to log the rate limit.
return;
}
const rateLimit: RateLimit = {
cost: -1,
limit: Number(result.headers['x-ratelimit-limit']),
remaining: Number(result.headers['x-ratelimit-remaining']),
resetAt: ''
};
this.logRateLimit(info, Promise.resolve({ data: { rateLimit } }), true);
}
}
export class LoggingApolloClient {
constructor(private readonly _graphql: ApolloClient<NormalizedCacheObject>, private _rateLogger: RateLogger) { }
query<T = any, TVariables = OperationVariables>(options: QueryOptions<TVariables>): Promise<ApolloQueryResult<T>> {
const logInfo = (options.query.definitions[0] as { name: { value: string } | undefined }).name?.value;
const result = this._rateLogger.logAndLimit(logInfo, () => this._graphql.query(options));
if (result === undefined) {
throw new Error('API call count has exceeded a rate limit.');
}
this._rateLogger.logRateLimit(logInfo, result as Promise<RateLimitResult>);
this._rateLogger.logApiError(logInfo, result);
return result;
}
mutate<T = any, TVariables = OperationVariables>(options: MutationOptions<T, TVariables>): Promise<FetchResult<T>> {
const logInfo = String(options.context);
const result = this._rateLogger.logAndLimit(logInfo, () => this._graphql.mutate(options));
if (result === undefined) {
throw new Error('API call count has exceeded a rate limit.');
}
this._rateLogger.logRateLimit(logInfo, result as Promise<RateLimitResult>);
this._rateLogger.logApiError(logInfo, result);
return result;
}
}
export class LoggingOctokit {
constructor(public readonly api: Octokit, private _rateLogger: RateLogger) { }
call<T extends (...args: any[]) => Promise<any>>(api: T, ...args: Parameters<T>): ReturnType<T> {
const logInfo = (api as unknown as { endpoint: { DEFAULTS: { url: string } | undefined } | undefined }).endpoint?.DEFAULTS?.url;
const result = this._rateLogger.logAndLimit<ReturnType<T>>(logInfo, ((() => api(...args)) as () => ReturnType<T>));
if (result === undefined) {
throw new Error('API call count has exceeded a rate limit.');
}
this._rateLogger.logRestRateLimit(logInfo, result as Promise<unknown> as Promise<RestResponse>);
this._rateLogger.logApiError(logInfo, result);
return result;
}
}
export async function compareCommits(remote: GitHubRemote, octokit: LoggingOctokit, base: GitHubRef, head: GitHubRef, compareWithBaseRef: string, prNumber: number, logId: string): Promise<{ mergeBaseSha: string; files: IRawFileChange[] }> {
Logger.debug(`Comparing commits for ${remote.owner}/${remote.repositoryName} with base ${base.repositoryCloneUrl.owner}:${compareWithBaseRef} and head ${head.repositoryCloneUrl.owner}:${head.sha}`, logId);
let files: IRawFileChange[] | undefined;
let mergeBaseSha: string | undefined;
const listFiles = async (perPage?: number) => {
return restPaginate<typeof octokit.api.pulls.listFiles, IRawFileChange>(octokit.api.pulls.listFiles, {
owner: base.repositoryCloneUrl.owner,
pull_number: prNumber,
repo: remote.repositoryName,
}, perPage);
};
try {
const { data } = await octokit.call(octokit.api.repos.compareCommits, {
repo: remote.repositoryName,
owner: remote.owner,
base: `${base.repositoryCloneUrl.owner}:${compareWithBaseRef}`,
head: `${head.repositoryCloneUrl.owner}:${head.sha}`,
});
const MAX_FILE_CHANGES_IN_COMPARE_COMMITS = 100;
if (data.files && data.files.length >= MAX_FILE_CHANGES_IN_COMPARE_COMMITS) {
// compareCommits will return a maximum of 100 changed files
// If we have (maybe) more than that, we'll need to fetch them with listFiles API call
Logger.appendLine(`More than ${MAX_FILE_CHANGES_IN_COMPARE_COMMITS} files changed in #${prNumber}`, logId);
files = await listFiles();
} else {
// if we're under the limit, just use the result from compareCommits, don't make additional API calls.
files = data.files ? data.files as IRawFileChange[] : [];
}
mergeBaseSha = data.merge_base_commit.sha;
} catch (e) {
if (e.message === 'Server Error') {
// Happens when github times out. Let's try to get a few at a time.
files = await listFiles(3);
mergeBaseSha = base.sha;
} else {
throw e;
}
}
return { mergeBaseSha, files };
}