renovate/lib/platform/github/gh-got-wrapper.ts

176 lines
5.4 KiB
TypeScript
Raw Normal View History

import URL from 'url';
import parseLinkHeader from 'parse-link-header';
import pAll from 'p-all';
import got from '../../util/got';
import { maskToken } from '../../util/mask';
import { GotApi } from '../common';
import { logger } from '../../logger';
2019-05-27 05:39:01 +00:00
const hostType = 'github';
let baseUrl = 'https://api.github.com/';
async function get(
path: string,
options?: any,
okToRetry = true
): Promise<any> {
2018-07-06 05:26:36 +00:00
const opts = {
2019-05-27 05:39:01 +00:00
hostType,
baseUrl,
json: true,
2018-07-06 05:26:36 +00:00
...options,
};
const method = opts.method || 'get';
if (method.toLowerCase() === 'post' && path === 'graphql') {
// GitHub Enterprise uses unversioned graphql path
opts.baseUrl = opts.baseUrl.replace('/v3/', '/');
}
2018-09-05 12:18:31 +00:00
logger.trace(`${method.toUpperCase()} ${path}`);
try {
2018-09-07 04:28:07 +00:00
if (global.appMode) {
const appAccept = 'application/vnd.github.machine-man-preview+json';
opts.headers = Object.assign(
{},
{
accept: appAccept,
2018-08-28 15:07:00 +00:00
'user-agent':
process.env.RENOVATE_USER_AGENT ||
'https://github.com/renovatebot/renovate',
},
opts.headers
);
if (opts.headers.accept !== appAccept) {
opts.headers.accept = `${appAccept}, ${opts.headers.accept}`;
}
}
const res = await got(path, opts);
2018-04-09 04:07:05 +00:00
if (opts.paginate) {
// Check if result is paginated
2018-07-03 09:53:09 +00:00
const pageLimit = opts.pageLimit || 10;
2019-08-15 06:26:21 +00:00
const linkHeader = parseLinkHeader(res.headers.link as string);
if (linkHeader && linkHeader.next && linkHeader.last) {
let lastPage = +linkHeader.last.page;
2018-12-19 14:31:20 +00:00
if (!process.env.RENOVATE_PAGINATE_ALL && opts.paginate !== 'all') {
2018-07-03 09:53:09 +00:00
lastPage = Math.min(pageLimit, lastPage);
}
const pageNumbers = Array.from(
new Array(lastPage),
(x, i) => i + 1
).slice(1);
const queue = pageNumbers.map(page => () => {
const nextUrl = URL.parse(linkHeader.next.url, true);
delete nextUrl.search;
nextUrl.query.page = page.toString();
return get(
URL.format(nextUrl),
{ ...opts, paginate: false },
2019-05-27 06:28:14 +00:00
okToRetry
);
});
const pages = await pAll<{ body: any[] }>(queue, { concurrency: 5 });
res.body = res.body.concat(
...pages.filter(Boolean).map(page => page.body)
);
}
}
// istanbul ignore if
if (method === 'POST' && path === 'graphql') {
2018-09-11 13:31:59 +00:00
const goodResult = '{"data":{';
if (res.body.startsWith(goodResult)) {
2019-05-27 06:28:14 +00:00
if (!okToRetry) {
2018-09-11 13:31:59 +00:00
logger.info('Recovered graphql query');
}
2019-05-27 06:28:14 +00:00
} else if (okToRetry) {
logger.info('Retrying graphql query');
opts.body = opts.body.replace('first: 100', 'first: 25');
2019-05-27 06:28:14 +00:00
return get(path, opts, !okToRetry);
}
}
return res;
} catch (err) /* istanbul ignore next */ {
2019-06-04 02:10:22 +00:00
let message = err.message;
if (err.body && err.body.message) {
message = err.body.message;
}
if (
err.name === 'RequestError' &&
2019-06-06 16:57:08 +00:00
(err.code === 'ENOTFOUND' ||
err.code === 'ETIMEDOUT' ||
err.code === 'EAI_AGAIN')
) {
logger.info({ err }, 'GitHub failure: RequestError');
throw new Error('platform-failure');
}
2018-12-03 09:49:07 +00:00
if (err.name === 'ParseError') {
logger.info({ err }, 'GitHub failure: ParseError');
2018-12-03 09:49:07 +00:00
throw new Error('platform-failure');
}
if (err.statusCode >= 500 && err.statusCode < 600) {
logger.info({ err }, 'GitHub failure: 5xx');
throw new Error('platform-failure');
2018-07-09 09:28:33 +00:00
}
if (
2017-08-27 13:10:19 +00:00
err.statusCode === 403 &&
2019-06-04 02:10:22 +00:00
message.startsWith('You have triggered an abuse detection mechanism')
2017-08-27 13:10:19 +00:00
) {
logger.info({ err }, 'GitHub failure: abuse detection');
throw new Error('platform-failure');
2018-07-09 09:28:33 +00:00
}
2019-06-04 02:10:22 +00:00
if (err.statusCode === 403 && message.includes('Upgrade to GitHub Pro')) {
logger.debug({ path }, 'Endpoint needs paid GitHub plan');
throw err;
}
2019-06-04 02:10:22 +00:00
if (err.statusCode === 403 && message.includes('rate limit exceeded')) {
logger.info({ err }, 'GitHub failure: rate limit');
throw new Error('rate-limit-exceeded');
} else if (
err.statusCode === 403 &&
2019-06-04 02:10:22 +00:00
message.startsWith('Resource not accessible by integration')
) {
logger.info(
{ err },
'GitHub failure: Resource not accessible by integration'
);
throw new Error('integration-unauthorized');
2019-06-04 02:10:22 +00:00
} else if (err.statusCode === 401 && message.includes('Bad credentials')) {
const rateLimit = err.headers ? err.headers['x-ratelimit-limit'] : -1;
logger.info(
{
token: maskToken(opts.token),
err,
},
'GitHub failure: Bad credentials'
);
if (rateLimit === '60') {
throw new Error('platform-failure');
}
throw new Error('bad-credentials');
2019-07-17 07:44:37 +00:00
} else if (err.statusCode === 422) {
if (
err.body &&
err.body.errors &&
err.body.errors.find((e: any) => e.code === 'invalid')
) {
throw new Error('repository-changed');
}
throw new Error('platform-failure');
}
throw err;
}
}
const helpers = ['get', 'post', 'put', 'patch', 'head', 'delete'];
for (const x of helpers) {
(get as any)[x] = (url: string, opts: any) =>
get(url, Object.assign({}, opts, { method: x.toUpperCase() }));
}
get.setBaseUrl = (u: string) => {
2019-05-27 05:39:01 +00:00
baseUrl = u;
};
export const api: GotApi = get as any;
export default api;