/
github.ts
380 lines (351 loc) · 10.8 KB
/
github.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import is from '@sindresorhus/is';
import pAll from 'p-all';
import { PlatformId } from '../../constants';
import {
PLATFORM_BAD_CREDENTIALS,
PLATFORM_INTEGRATION_UNAUTHORIZED,
PLATFORM_RATE_LIMIT_EXCEEDED,
REPOSITORY_CHANGED,
} from '../../constants/error-messages';
import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import { maskToken } from '../mask';
import { range } from '../range';
import { regEx } from '../regex';
import { parseLinkHeader } from '../url';
import { GotLegacyError } from './legacy';
import { Http, HttpPostOptions, HttpResponse, InternalHttpOptions } from '.';
const githubBaseUrl = 'https://api.github.com/';
let baseUrl = githubBaseUrl;
export const setBaseUrl = (url: string): void => {
baseUrl = url;
};
interface GithubInternalOptions extends InternalHttpOptions {
body?: string;
}
export interface GithubHttpOptions extends InternalHttpOptions {
paginate?: boolean | string;
paginationField?: string;
pageLimit?: number;
token?: string;
}
interface GithubGraphqlRepoData<T = unknown> {
repository?: T;
}
interface GithubGraphqlResponse<T = unknown> {
data?: T;
errors?: {
type?: string;
message: string;
locations: unknown;
}[];
}
function handleGotError(
err: GotLegacyError,
url: string | URL,
opts: GithubHttpOptions
): Error {
const path = url.toString();
let message = err.message || '';
const body = err.response?.body;
if (is.plainObject(body) && 'message' in body) {
message = String(body.message);
}
if (
err.code === 'ENOTFOUND' ||
err.code === 'ETIMEDOUT' ||
err.code === 'EAI_AGAIN' ||
err.code === 'ECONNRESET'
) {
logger.debug({ err }, 'GitHub failure: RequestError');
return new ExternalHostError(err, PlatformId.Github);
}
if (err.name === 'ParseError') {
logger.debug({ err }, '');
return new ExternalHostError(err, PlatformId.Github);
}
if (err.statusCode && err.statusCode >= 500 && err.statusCode < 600) {
logger.debug({ err }, 'GitHub failure: 5xx');
return new ExternalHostError(err, PlatformId.Github);
}
if (
err.statusCode === 403 &&
message.startsWith('You have triggered an abuse detection mechanism')
) {
logger.debug({ err }, 'GitHub failure: abuse detection');
return new Error(PLATFORM_RATE_LIMIT_EXCEEDED);
}
if (
err.statusCode === 403 &&
message.startsWith('You have exceeded a secondary rate limit')
) {
logger.debug({ err }, 'GitHub failure: secondary rate limit');
return new Error(PLATFORM_RATE_LIMIT_EXCEEDED);
}
if (err.statusCode === 403 && message.includes('Upgrade to GitHub Pro')) {
logger.debug({ path }, 'Endpoint needs paid GitHub plan');
return err;
}
if (err.statusCode === 403 && message.includes('rate limit exceeded')) {
logger.debug({ err }, 'GitHub failure: rate limit');
return new Error(PLATFORM_RATE_LIMIT_EXCEEDED);
}
if (
err.statusCode === 403 &&
message.startsWith('Resource not accessible by integration')
) {
logger.debug(
{ err },
'GitHub failure: Resource not accessible by integration'
);
return new Error(PLATFORM_INTEGRATION_UNAUTHORIZED);
}
if (err.statusCode === 401 && message.includes('Bad credentials')) {
const rateLimit = err.headers?.['x-ratelimit-limit'] ?? -1;
logger.debug(
{
token: maskToken(opts.token),
err,
},
'GitHub failure: Bad credentials'
);
if (rateLimit === '60') {
return new ExternalHostError(err, PlatformId.Github);
}
return new Error(PLATFORM_BAD_CREDENTIALS);
}
if (err.statusCode === 422) {
if (
message.includes('Review cannot be requested from pull request author')
) {
return err;
} else if (err.body?.errors?.find((e: any) => e.code === 'invalid')) {
logger.debug({ err }, 'Received invalid response - aborting');
return new Error(REPOSITORY_CHANGED);
} else if (
err.body?.errors?.find((e: any) =>
e.message?.startsWith('A pull request already exists')
)
) {
return err;
}
logger.debug({ err }, '422 Error thrown from GitHub');
return new ExternalHostError(err, PlatformId.Github);
}
if (
err.statusCode === 410 &&
err.body?.message === 'Issues are disabled for this repo'
) {
return err;
}
if (err.statusCode === 404) {
logger.debug({ url: path }, 'GitHub 404');
} else {
logger.debug({ err }, 'Unknown GitHub error');
}
return err;
}
interface GraphqlOptions {
variables?: Record<string, string | number | null>;
paginate?: boolean;
count?: number;
limit?: number;
cursor?: string | null;
acceptHeader?: string;
}
interface GraphqlPaginatedContent<T = unknown> {
nodes: T[];
edges: T[];
pageInfo: { hasNextPage: boolean; endCursor: string };
}
function constructAcceptString(input?: any): string {
const defaultAccept = 'application/vnd.github.v3+json';
const acceptStrings =
typeof input === 'string' ? input.split(regEx(/\s*,\s*/)) : [];
if (
!acceptStrings.some((x) => x.startsWith('application/vnd.github.')) ||
acceptStrings.length < 2
) {
acceptStrings.push(defaultAccept);
}
return acceptStrings.join(', ');
}
export class GithubHttp extends Http<GithubHttpOptions, GithubHttpOptions> {
constructor(
hostType: string = PlatformId.Github,
options?: GithubHttpOptions
) {
super(hostType, options);
}
protected override async request<T>(
url: string | URL,
options?: GithubInternalOptions & GithubHttpOptions,
okToRetry = true
): Promise<HttpResponse<T>> {
const opts = {
baseUrl,
...options,
throwHttpErrors: true,
};
const accept = constructAcceptString(opts.headers?.accept);
opts.headers = {
...opts.headers,
accept,
};
try {
const result = await super.request<T>(url, opts);
if (opts.paginate) {
// Check if result is paginated
const pageLimit = opts.pageLimit ?? 10;
const linkHeader = parseLinkHeader(result?.headers?.link);
if (linkHeader?.next && linkHeader?.last) {
let lastPage = parseInt(linkHeader.last.page, 10);
// istanbul ignore else: needs a test
if (!process.env.RENOVATE_PAGINATE_ALL && opts.paginate !== 'all') {
lastPage = Math.min(pageLimit, lastPage);
}
const queue = [...range(2, lastPage)].map(
(pageNumber) => (): Promise<HttpResponse<T>> => {
const nextUrl = new URL(linkHeader.next.url, baseUrl);
nextUrl.searchParams.set('page', String(pageNumber));
return this.request<T>(
nextUrl,
{ ...opts, paginate: false },
okToRetry
);
}
);
const pages = await pAll(queue, { concurrency: 5 });
if (opts.paginationField && is.plainObject(result.body)) {
const paginatedResult = result.body[opts.paginationField];
if (is.array<T>(paginatedResult)) {
for (const nextPage of pages) {
if (is.plainObject(nextPage.body)) {
const nextPageResults = nextPage.body[opts.paginationField];
if (is.array<T>(nextPageResults)) {
paginatedResult.push(...nextPageResults);
}
}
}
}
} else if (is.array<T>(result.body)) {
for (const nextPage of pages) {
if (is.array<T>(nextPage.body)) {
result.body.push(...nextPage.body);
}
}
}
}
}
return result;
} catch (err) {
throw handleGotError(err, url, opts);
}
}
public async requestGraphql<T = unknown>(
query: string,
options: GraphqlOptions = {}
): Promise<GithubGraphqlResponse<T> | null> {
const path = 'graphql';
const { paginate, count = 100, cursor = null } = options;
let { variables } = options;
if (paginate) {
variables = {
...variables,
count,
cursor,
};
}
const body = variables ? { query, variables } : { query };
const opts: HttpPostOptions = {
baseUrl: baseUrl.replace('/v3/', '/'), // GHE uses unversioned graphql path
body,
headers: { accept: options?.acceptHeader },
};
logger.trace(`Performing Github GraphQL request`);
try {
const res = await this.postJson<GithubGraphqlResponse<T>>(
'graphql',
opts
);
return res?.body;
} catch (err) {
logger.debug({ err, query, options }, 'Unexpected GraphQL Error');
if (err instanceof ExternalHostError && count && count > 10) {
logger.info('Reducing pagination count to workaround graphql errors');
return null;
}
throw handleGotError(err, path, opts);
}
}
async queryRepoField<T = Record<string, unknown>>(
query: string,
fieldName: string,
options: GraphqlOptions = {}
): Promise<T[]> {
const result: T[] = [];
const { paginate = true } = options;
let optimalCount: null | number = null;
const initialCount = options.count ?? 100;
let count = initialCount;
let limit = options.limit ?? 1000;
let cursor: string | null = null;
let isIterating = true;
while (isIterating) {
const res = await this.requestGraphql<GithubGraphqlRepoData<T>>(query, {
...options,
count: Math.min(count, limit),
cursor,
paginate,
});
const repositoryData = res?.data?.repository;
if (
repositoryData &&
is.plainObject(repositoryData) &&
repositoryData[fieldName]
) {
optimalCount = count;
const {
nodes = [],
edges = [],
pageInfo,
} = repositoryData[fieldName] as GraphqlPaginatedContent<T>;
result.push(...nodes);
result.push(...edges);
limit = Math.max(0, limit - nodes.length - edges.length);
if (limit === 0) {
isIterating = false;
} else if (paginate && pageInfo) {
const { hasNextPage, endCursor } = pageInfo;
if (hasNextPage && endCursor) {
cursor = endCursor;
} else {
isIterating = false;
}
}
} else {
count = Math.floor(count / 2);
if (count === 0) {
logger.warn({ query, options, res }, 'Error fetching GraphQL nodes');
isIterating = false;
}
}
if (!paginate) {
isIterating = false;
}
}
// See: https://github.com/renovatebot/renovate/issues/12703
// istanbul ignore if
if (
optimalCount &&
optimalCount < initialCount && // log only shrinked results
baseUrl === githubBaseUrl
) {
logger.debug(
{ fieldName, optimalCount },
'Successful GraphQL query with shrinked pagination size'
);
}
return result;
}
}