diff --git a/src/@types/@octokit/plugin-retry/index.d.ts b/src/@types/@octokit/plugin-retry/index.d.ts new file mode 100644 index 00000000..f9de61f9 --- /dev/null +++ b/src/@types/@octokit/plugin-retry/index.d.ts @@ -0,0 +1,5 @@ +declare module '@octokit/plugin-retry' { + import Octokit from '@octokit/rest' + declare function plugin(octokit: Octokit, options: Octokit.Options): void + export = plugin +} \ No newline at end of file diff --git a/src/@types/@octokit/plugin-throttling/index.d.ts b/src/@types/@octokit/plugin-throttling/index.d.ts new file mode 100644 index 00000000..8f9f272a --- /dev/null +++ b/src/@types/@octokit/plugin-throttling/index.d.ts @@ -0,0 +1,5 @@ +declare module '@octokit/plugin-throttling' { + import Octokit from '@octokit/rest' + declare function plugin(octokit: Octokit, options: Octokit.Options): void + export = plugin +} \ No newline at end of file diff --git a/src/github/index.ts b/src/github/index.ts index 644107d9..b68c3d8a 100644 --- a/src/github/index.ts +++ b/src/github/index.ts @@ -1,8 +1,14 @@ +import retryPlugin from '@octokit/plugin-retry' +import throttlePlugin from '@octokit/plugin-throttling' import Octokit from '@octokit/rest' + import { addGraphQL } from './graphql' import { addLogging, Logger } from './logging' import { addPagination } from './pagination' -import { addRateLimiting } from './rate-limiting' + +const ProbotOctokit = Octokit + .plugin(throttlePlugin) + .plugin(retryPlugin) /** * the [@octokit/rest Node.js module](https://github.com/octokit/rest.js), @@ -12,10 +18,14 @@ import { addRateLimiting } from './rate-limiting' * @see {@link https://github.com/octokit/rest.js} */ export function GitHubAPI (options: Options = {} as any) { - const octokit = new Octokit(options) as GitHubAPI + const octokit = new ProbotOctokit(Object.assign(options, { + throttle: { + onAbuseLimit: (retryAfter: number) => options.logger.warn(`Abuse limit hit, retrying in ${retryAfter} seconds`), + onRateLimit: (retryAfter: number) => options.logger.warn(`Rate limit hit, retrying in ${retryAfter} seconds`) + } + })) as GitHubAPI addPagination(octokit) - addRateLimiting(octokit, options.limiter) addLogging(octokit, options.logger) addGraphQL(octokit) @@ -25,7 +35,6 @@ export function GitHubAPI (options: Options = {} as any) { export interface Options extends Octokit.Options { debug?: boolean logger: Logger - limiter?: any } export interface RequestOptions { diff --git a/src/github/rate-limiting.ts b/src/github/rate-limiting.ts deleted file mode 100644 index 84de6918..00000000 --- a/src/github/rate-limiting.ts +++ /dev/null @@ -1,14 +0,0 @@ -import Bottleneck from 'bottleneck' -import { GitHubAPI } from './' - -export function addRateLimiting (octokit: GitHubAPI, limiter: Bottleneck) { - if (!limiter) { - limiter = new Bottleneck({ - maxConcurrent: 1, - minTime: 1000 - }) - } - - const noop = () => Promise.resolve() - octokit.hook.before('request', () => limiter.schedule(noop)) -} diff --git a/test/github.test.ts b/test/github.test.ts index a0e98122..b77d7fd9 100644 --- a/test/github.test.ts +++ b/test/github.test.ts @@ -1,4 +1,3 @@ -import Bottleneck from 'bottleneck' import nock from 'nock' import { GitHubAPI, Options } from '../src/github' import { logger } from '../src/logger' @@ -7,11 +6,7 @@ describe('GitHubAPI', () => { let github: GitHubAPI beforeEach(() => { - // Set a shorter limiter, otherwise tests are _slow_ - const limiter = new Bottleneck() - const options: Options = { - limiter, logger } diff --git a/test/github/graphql.test.ts b/test/github/graphql.test.ts index f1a32985..6917187b 100644 --- a/test/github/graphql.test.ts +++ b/test/github/graphql.test.ts @@ -1,4 +1,3 @@ -import Bottleneck from 'bottleneck' import nock from 'nock' import { GitHubAPI, Options } from '../../src/github' import { logger } from '../../src/logger' @@ -11,11 +10,7 @@ describe('github/graphql', () => { afterEach(() => expect(nock.pendingMocks()).toEqual([])) beforeEach(() => { - // Set a shorter limiter, otherwise tests are _slow_ - const limiter = new Bottleneck() - const options: Options = { - limiter, logger } @@ -97,11 +92,7 @@ describe('github/graphql', () => { beforeEach(() => { process.env.GHE_HOST = 'notreallygithub.com' - // Set a shorter limiter, otherwise tests are _slow_ - const limiter = new Bottleneck() - const options: Options = { - limiter, logger }