Skip to content

Commit

Permalink
feat: enable GitHub API request throttling
Browse files Browse the repository at this point in the history
It implements best practices for using the GitHub API as described in
https://developer.github.com/v3/guides/best-practices-for-integrators/#dealing-with-abuse-rate-limits

When the primary rate limit is hit, it will log a warning and wait before
trying the API endpoint one more time before failing.
  • Loading branch information
jamacku authored and zmiklank committed Jan 22, 2024
1 parent c9dff33 commit 60bec9d
Show file tree
Hide file tree
Showing 16 changed files with 1,976 additions and 26 deletions.
1,835 changes: 1,828 additions & 7 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions dist/licenses.txt

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

6 changes: 2 additions & 4 deletions dist/main.js

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

2 changes: 1 addition & 1 deletion dist/main.js.map

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

5 changes: 5 additions & 0 deletions dist/octokit.d.ts

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

24 changes: 24 additions & 0 deletions dist/octokit.js

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

1 change: 1 addition & 0 deletions dist/octokit.js.map

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

6 changes: 3 additions & 3 deletions dist/pull-request.d.ts

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

2 changes: 1 addition & 1 deletion dist/pull-request.js.map

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

4 changes: 2 additions & 2 deletions dist/schema/input.d.ts

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 @@ -32,6 +32,7 @@
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0",
"@octokit/core": "^5.0.1",
"@octokit/plugin-throttling": "^8.1.3",
"@octokit/rest": "^20.0.2",
"@octokit/types": "^12.4.0",
"testing-farm": "^1.5.1",
Expand Down
6 changes: 2 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { getBooleanInput, getInput, setFailed } from '@actions/core';
import { context } from '@actions/github';
import { Octokit } from '@octokit/core';

import '@total-typescript/ts-reset';

import action from './action';
import { TFError } from './error';
import { getOctokit } from './octokit';
import { PullRequest } from './pull-request';

let pr: PullRequest | undefined = undefined;

try {
const octokit = new Octokit({
auth: getInput('github_token', { required: true }),
});
const octokit = getOctokit(getInput('github_token', { required: true }));

pr = await PullRequest.initialize(context.issue.number, octokit);

Expand Down
32 changes: 32 additions & 0 deletions src/octokit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { info, warning } from '@actions/core';
import { Octokit } from '@octokit/core';
import { throttling } from '@octokit/plugin-throttling';

const CustomOctokit = Octokit.plugin(throttling);

export type CustomOctokit = InstanceType<typeof CustomOctokit>;

export function getOctokit(token: string) {
return new CustomOctokit({
auth: token,
throttle: {
onRateLimit: (retryAfter, options, _octokit, retryCount) => {
warning(
`Request quota exhausted for request ${options.method} ${options.url}`
);

// Retry once after hitting a rate limit error, then give up
if (retryCount < 1) {
info(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
onSecondaryRateLimit: (_retryAfter, options) => {
// When a secondary rate limit is hit, don't retry
warning(
`SecondaryRateLimit detected for request ${options.method} ${options.url}`
);
},
},
});
}
7 changes: 4 additions & 3 deletions src/pull-request.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { debug, getInput } from '@actions/core';
import { context } from '@actions/github';
import { Octokit } from '@octokit/core';
import { Endpoints } from '@octokit/types';

import { CustomOctokit } from './octokit';

/**
* Class for holding information about a Pull Request and interacting with it via the GitHub API.
*/
Expand All @@ -16,7 +17,7 @@ export class PullRequest {
private constructor(
readonly number: number,
readonly sha: string,
readonly octokit: Octokit
readonly octokit: CustomOctokit
) {}

/**
Expand Down Expand Up @@ -72,7 +73,7 @@ export class PullRequest {
*/
static async initialize(
number: number,
octokit: Octokit
octokit: CustomOctokit
): Promise<PullRequest> {
const { data } = await octokit.request(
'GET /repos/{owner}/{repo}/pulls/{pull_number}',
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,18 @@ __metadata:
languageName: node
linkType: hard

"@octokit/plugin-throttling@npm:^8.1.3":
version: 8.1.3
resolution: "@octokit/plugin-throttling@npm:8.1.3"
dependencies:
"@octokit/types": ^12.2.0
bottleneck: ^2.15.3
peerDependencies:
"@octokit/core": ^5.0.0
checksum: 98963ef2eab825015702b1ca1ef4ccbda0c009242e93001144e51014d3b53d3ecb1282b67488680c7f5f4e805d0c3af4020ad673079fff92c6353f04903a9a64
languageName: node
linkType: hard

"@octokit/request-error@npm:^5.0.0":
version: 5.0.1
resolution: "@octokit/request-error@npm:5.0.1"
Expand Down Expand Up @@ -1171,6 +1183,13 @@ __metadata:
languageName: node
linkType: hard

"bottleneck@npm:^2.15.3":
version: 2.19.5
resolution: "bottleneck@npm:2.19.5"
checksum: c5eef1bbea12cef1f1405e7306e7d24860568b0f7ac5eeab706a86762b3fc65ef6d1c641c8a166e4db90f412fc5c948fc5ce8008a8cd3d28c7212ef9c3482bda
languageName: node
linkType: hard

"brace-expansion@npm:^1.1.7":
version: 1.1.11
resolution: "brace-expansion@npm:1.1.11"
Expand Down Expand Up @@ -3035,6 +3054,7 @@ __metadata:
"@actions/core": ^1.10.1
"@actions/github": ^6.0.0
"@octokit/core": ^5.0.1
"@octokit/plugin-throttling": ^8.1.3
"@octokit/rest": ^20.0.2
"@octokit/types": ^12.4.0
"@total-typescript/ts-reset": ^0.5.1
Expand Down

0 comments on commit 60bec9d

Please sign in to comment.