From 64b071d03709c0df093088068f15aa5c4e0fa68e Mon Sep 17 00:00:00 2001 From: Andy Edwards Date: Mon, 6 May 2019 22:38:15 -0500 Subject: [PATCH] fix: use @octokit/plugin-throttling --- package.json | 1 + src/index.js | 30 ++++++++++++++++++++++++++++-- yarn.lock | 12 ++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 2b297f4..7ba3eb8 100644 --- a/package.json +++ b/package.json @@ -132,6 +132,7 @@ }, "dependencies": { "@babel/runtime": "^7.1.5", + "@octokit/plugin-throttling": "^2.4.0", "@octokit/rest": "^16.25.1", "chalk": "^2.4.2", "js-base64": "^2.5.1", diff --git a/src/index.js b/src/index.js index c252e84..c399a13 100644 --- a/src/index.js +++ b/src/index.js @@ -6,13 +6,39 @@ import npmRegistryFetch from 'npm-registry-fetch' import { Base64 } from 'js-base64' import parseChangelog, { type Release } from './changelog-parser' import semver from 'semver' -import Octokit from '@octokit/rest' +import _Octokit from '@octokit/rest' +import octokitThrottling from '@octokit/plugin-throttling' import getNpmToken from './getNpmToken' import memoize from './util/memoize' +const Octokit = _Octokit.plugin(octokitThrottling) + const { GH_TOKEN } = process.env -const octokitOptions = {} +type LimitOptions = { + method: string, + url: string, + request: { + retryCount: number, + }, +} + +const octokitOptions: Object = { + throttle: { + onRateLimit: (retryAfter: number, options: LimitOptions) => { + octokit.log.warn( + `Request quota exhausted for request ${options.method} ${options.url}` + ) + return options.request.retryCount < 3 + }, + onAbuseLimit: (retryAfter: number, options: LimitOptions) => { + // does not retry, only logs a warning + octokit.log.warn( + `Abuse detected for request ${options.method} ${options.url}` + ) + }, + }, +} if (GH_TOKEN) octokitOptions.auth = `token ${GH_TOKEN}` const octokit = new Octokit(octokitOptions) diff --git a/yarn.lock b/yarn.lock index 817749e..ce088bc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -922,6 +922,13 @@ universal-user-agent "^2.0.1" url-template "^2.0.8" +"@octokit/plugin-throttling@^2.4.0": + version "2.4.0" + resolved "https://registry.yarnpkg.com/@octokit/plugin-throttling/-/plugin-throttling-2.4.0.tgz#add1bd6a797aa210ce860e8bbe7f7505ba5e4d99" + integrity sha512-0bYfgQdqAtWiqXvS6oKMiousTno+e3pFoaYS57LpAp8p56Far49/vpGvbEvBUuNyoUFgOcTGm5WTpO1IPptHuQ== + dependencies: + bottleneck "^2.15.3" + "@octokit/request@3.0.1": version "3.0.1" resolved "https://registry.yarnpkg.com/@octokit/request/-/request-3.0.1.tgz#21e888c6dce80566ec69477360bab79f2f14861f" @@ -1518,6 +1525,11 @@ bottleneck@^2.0.1: resolved "https://registry.yarnpkg.com/bottleneck/-/bottleneck-2.13.0.tgz#875df17df9e62c76bea42b62af3a45c73a995c4f" integrity sha512-9YmZ0aiKta2OAxTujKCS/INjGWCIGWK4Ff1nQpgHnR4CTjlk9jcnpaHOjPnMZPtqRXkqwKdtxZgvJ9udsXylaw== +bottleneck@^2.15.3: + version "2.18.0" + resolved "https://registry.yarnpkg.com/bottleneck/-/bottleneck-2.18.0.tgz#41fa63ae185b65435d789d1700334bc48222dacf" + integrity sha512-U1xiBRaokw4yEguzikOl0VrnZp6uekjpmfrh6rKtr1D+/jFjYCL6J83ZXlGtlBDwVdTmJJ+4Lg5FpB3xmLSiyA== + boxen@^1.2.1: version "1.3.0" resolved "https://registry.yarnpkg.com/boxen/-/boxen-1.3.0.tgz#55c6c39a8ba58d9c61ad22cd877532deb665a20b"