2018-05-16 05:23:59 +00:00
|
|
|
const URL = require('url');
|
2017-08-20 07:49:39 +00:00
|
|
|
const ghGot = require('gh-got');
|
2018-04-09 04:07:05 +00:00
|
|
|
const delay = require('delay');
|
2017-10-17 18:44:40 +00:00
|
|
|
const parseLinkHeader = require('parse-link-header');
|
2017-08-20 07:49:39 +00:00
|
|
|
|
2017-11-16 21:13:54 +00:00
|
|
|
let cache = {};
|
|
|
|
|
2017-10-17 05:15:01 +00:00
|
|
|
async function get(path, opts, retries = 5) {
|
2018-04-09 04:07:05 +00:00
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
opts = Object.assign({}, opts);
|
|
|
|
const method = opts.method ? opts.method : 'get';
|
2017-11-16 21:13:54 +00:00
|
|
|
if (method === 'get' && cache[path]) {
|
2018-02-06 13:17:46 +00:00
|
|
|
logger.trace({ path }, 'Returning cached result');
|
2017-11-16 21:13:54 +00:00
|
|
|
return cache[path];
|
|
|
|
}
|
2018-03-30 11:33:37 +00:00
|
|
|
logger.debug(`${method.toUpperCase()} ${path} [retries=${retries}]`);
|
2017-08-20 07:49:39 +00:00
|
|
|
try {
|
2017-10-16 07:45:31 +00:00
|
|
|
if (appMode) {
|
2017-10-17 06:04:58 +00:00
|
|
|
const appAccept = 'application/vnd.github.machine-man-preview+json';
|
2017-10-16 07:45:31 +00:00
|
|
|
opts.headers = Object.assign(
|
2017-10-17 06:04:58 +00:00
|
|
|
{},
|
2017-10-16 07:45:31 +00:00
|
|
|
{
|
2017-10-17 06:04:58 +00:00
|
|
|
accept: appAccept,
|
2017-11-23 13:08:31 +00:00
|
|
|
'user-agent': 'https://github.com/renovateapp/renovate',
|
2017-10-16 07:45:31 +00:00
|
|
|
},
|
|
|
|
opts.headers
|
|
|
|
);
|
2017-10-17 06:04:58 +00:00
|
|
|
if (opts.headers.accept !== appAccept) {
|
|
|
|
opts.headers.accept = `${appAccept}, ${opts.headers.accept}`;
|
|
|
|
}
|
2017-10-16 07:45:31 +00:00
|
|
|
}
|
2017-08-20 07:49:39 +00:00
|
|
|
const res = await ghGot(path, opts);
|
2018-04-09 04:07:05 +00:00
|
|
|
if (opts.paginate) {
|
2017-10-17 11:45:17 +00:00
|
|
|
// Check if result is paginated
|
2017-10-17 18:44:40 +00:00
|
|
|
const linkHeader = parseLinkHeader(res.headers.link);
|
2018-05-16 05:23:59 +00:00
|
|
|
if (linkHeader && linkHeader.next && linkHeader.last) {
|
|
|
|
let lastPage = +linkHeader.last.page;
|
|
|
|
if (!process.env.RENOVATE_PAGINATE_ALL) {
|
|
|
|
lastPage = Math.min(10, lastPage);
|
|
|
|
}
|
|
|
|
const pageNumbers = Array.from(
|
|
|
|
new Array(lastPage),
|
|
|
|
(x, i) => i + 1
|
|
|
|
).slice(1);
|
|
|
|
const pages = await Promise.all(
|
|
|
|
pageNumbers.map(page => {
|
|
|
|
const url = URL.parse(linkHeader.next.url, true);
|
|
|
|
delete url.search;
|
|
|
|
url.query.page = page;
|
|
|
|
return get(URL.format(url), { ...opts, paginate: false }, retries);
|
|
|
|
})
|
|
|
|
);
|
2018-03-06 14:54:27 +00:00
|
|
|
res.body = res.body.concat(
|
2018-05-16 05:23:59 +00:00
|
|
|
...pages.filter(Boolean).map(page => page.body)
|
2018-03-06 14:54:27 +00:00
|
|
|
);
|
2017-10-17 08:12:40 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-30 11:37:06 +00:00
|
|
|
if (
|
|
|
|
method === 'get' &&
|
|
|
|
(path.startsWith('repos/') ||
|
|
|
|
path.startsWith('https://api.github.com/repos/'))
|
|
|
|
) {
|
2017-11-16 21:13:54 +00:00
|
|
|
cache[path] = res;
|
|
|
|
}
|
2017-08-20 07:49:39 +00:00
|
|
|
return res;
|
|
|
|
} catch (err) {
|
2017-08-27 13:10:19 +00:00
|
|
|
if (err.statusCode >= 500 && err.statusCode < 600 && retries > 0) {
|
2017-09-22 05:04:41 +00:00
|
|
|
logger.info(
|
|
|
|
{ statusCode: err.statusCode, message: err.message },
|
|
|
|
`Retrying request`
|
|
|
|
);
|
2018-04-09 04:07:05 +00:00
|
|
|
|
|
|
|
await delay(5000 / retries);
|
|
|
|
|
2017-10-17 05:15:01 +00:00
|
|
|
return get(path, opts, retries - 1);
|
2017-10-20 04:24:09 +00:00
|
|
|
} else if (
|
2017-08-27 13:10:19 +00:00
|
|
|
retries > 0 &&
|
|
|
|
err.statusCode === 403 &&
|
|
|
|
err.message &&
|
|
|
|
err.message.indexOf('You have triggered an abuse detection mechanism') ===
|
|
|
|
0
|
|
|
|
) {
|
2017-09-22 05:04:41 +00:00
|
|
|
logger.info(
|
|
|
|
{ statusCode: err.statusCode, message: err.message },
|
|
|
|
`Retrying request`
|
|
|
|
);
|
2018-04-09 04:07:05 +00:00
|
|
|
|
|
|
|
await delay(180000 / (retries * retries));
|
|
|
|
|
2017-10-17 05:15:01 +00:00
|
|
|
return get(path, opts, retries - 1);
|
2017-10-20 04:24:09 +00:00
|
|
|
} else if (
|
2017-09-16 04:39:04 +00:00
|
|
|
err.statusCode === 403 &&
|
|
|
|
err.message &&
|
2018-03-22 08:26:20 +00:00
|
|
|
err.message.includes('rate limit exceeded')
|
2017-09-16 04:39:04 +00:00
|
|
|
) {
|
2018-03-22 08:26:20 +00:00
|
|
|
logger.info({ err }, 'Rate limit exceeded');
|
|
|
|
throw new Error('rate-limit-exceeded');
|
2018-06-03 07:44:35 +00:00
|
|
|
} else if (
|
|
|
|
err.statusCode === 403 &&
|
|
|
|
err.message &&
|
|
|
|
err.message.includes('blobs up to 1 MB in size')
|
|
|
|
) {
|
|
|
|
throw err;
|
2018-03-22 08:26:20 +00:00
|
|
|
} else if (err.statusCode === 403) {
|
2017-10-11 13:18:50 +00:00
|
|
|
if (retries > 0) {
|
|
|
|
logger.info(
|
|
|
|
{ statusCode: err.statusCode, message: err.message },
|
|
|
|
`Retrying request`
|
|
|
|
);
|
2018-04-09 04:07:05 +00:00
|
|
|
|
|
|
|
await delay(60000 / (retries * retries));
|
|
|
|
|
2017-10-17 05:15:01 +00:00
|
|
|
return get(path, opts, retries - 1);
|
2017-09-16 04:39:04 +00:00
|
|
|
}
|
2017-10-20 04:24:09 +00:00
|
|
|
} else if (
|
|
|
|
err.statusCode === 401 &&
|
|
|
|
err.message &&
|
2018-05-16 04:01:02 +00:00
|
|
|
err.message.includes('Bad credentials')
|
2017-10-20 04:24:09 +00:00
|
|
|
) {
|
2018-05-16 04:01:02 +00:00
|
|
|
logger.debug(
|
|
|
|
{
|
|
|
|
err,
|
|
|
|
message: err.message,
|
|
|
|
body: err.response ? err.response.body : undefined,
|
|
|
|
},
|
|
|
|
'Bad credentials'
|
|
|
|
);
|
|
|
|
throw new Error('bad-credentials');
|
2017-09-16 04:39:04 +00:00
|
|
|
}
|
2017-08-20 07:49:39 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const helpers = ['get', 'post', 'put', 'patch', 'head', 'delete'];
|
|
|
|
|
|
|
|
for (const x of helpers) {
|
2017-12-14 10:47:00 +00:00
|
|
|
get[x] = (url, opts) =>
|
|
|
|
get(url, Object.assign({}, opts, { method: x.toUpperCase() }));
|
2017-08-20 07:49:39 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 07:45:31 +00:00
|
|
|
let appMode = false;
|
2017-10-17 05:15:01 +00:00
|
|
|
get.setAppMode = function setAppMode(val) {
|
2017-10-16 07:45:31 +00:00
|
|
|
appMode = val;
|
|
|
|
};
|
|
|
|
|
2017-12-14 10:47:00 +00:00
|
|
|
get.reset = function reset() {
|
2018-04-04 12:16:36 +00:00
|
|
|
cache = null;
|
2017-11-16 21:13:54 +00:00
|
|
|
cache = {};
|
|
|
|
};
|
|
|
|
|
2017-10-17 05:15:01 +00:00
|
|
|
module.exports = get;
|