Skip to content

Commit

Permalink
Add throttling plugin for Octokit
Browse files Browse the repository at this point in the history
  • Loading branch information
alexesprit committed Oct 14, 2020
1 parent 805f047 commit 81bf2e6
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dependencies": {
"@actions/core": "1.2.6",
"@actions/github": "4.0.0",
"@octokit/plugin-throttling": "3.3.1",
"fast-glob": "3.2.4"
},
"devDependencies": {
Expand Down
56 changes: 56 additions & 0 deletions src/octokit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Octokit } from '@octokit/core';
import { throttling } from '@octokit/plugin-throttling';

import { GitHub, getOctokitOptions } from '@actions/github/lib/utils';

const OctokitConstructor = GitHub.plugin(throttling);

const maxRetryCount = 1;

interface RequestOptions {
method: string;
url: string;
request: RequestInfo;
}

interface RequestInfo {
retryCount: number;
}

export function createOctokit(
accessToken: string
): InstanceType<typeof GitHub> {
const octokit = new OctokitConstructor(
getOctokitOptions(accessToken, {
throttle: { onRateLimit, onAbuseLimit },
})
);

return octokit;
}

function onRateLimit(
retryAfter: number,
options: RequestOptions,
octokit: Octokit
): boolean {
octokit.log.warn(
`Request quota exhausted for request ${options.method} ${options.url}`
);
if (options.request.retryCount < maxRetryCount) {
octokit.log.info(`Retrying after ${retryAfter} seconds!`);
return true;
}

return false;
}

function onAbuseLimit(
_: number,
options: RequestOptions,
octokit: Octokit
): void {
octokit.log.warn(
`Abuse detected for request ${options.method} ${options.url}`
);
}
8 changes: 5 additions & 3 deletions src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import { readFile, existsSync } from 'fs';
import { promisify } from 'util';

import { getOctokit, context } from '@actions/github';
import { GitHub } from '@actions/github/lib/utils';
import { context } from '@actions/github';

import { UpdaterOptions, isNotNull } from './util';
import { createOctokit } from './octokit';

const readFileAsync = promisify(readFile);

Expand All @@ -27,12 +29,12 @@ interface UpdateResult {
}

export class Updater {
private octokit: ReturnType<typeof getOctokit>;
private octokit: InstanceType<typeof GitHub>;
private message: string;
private defaultBranch: string | null;

constructor(options: UpdaterOptions) {
this.octokit = getOctokit(options.token);
this.octokit = createOctokit(options.token);

this.message = options.message;
this.defaultBranch = options.branch || null;
Expand Down

0 comments on commit 81bf2e6

Please sign in to comment.