Skip to content

Commit

Permalink
Add plugin-throttling to github bot (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Dec 23, 2023
1 parent 2371878 commit 39fb9e9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@nestjs/schedule": "^2.1.0",
"@nestjs/websockets": "^8.4.4",
"@octokit/auth-app": "^4.0.6",
"@octokit/plugin-throttling": "^8.1.3",
"@octokit/rest": "^19.0.4",
"@octokit/webhooks": "^10.1.5",
"@octokit/webhooks-types": "^6.4.0",
Expand Down
7 changes: 5 additions & 2 deletions services/bots/src/github-webhook/github-webhook.model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { throttling } from '@octokit/plugin-throttling';
import { Octokit } from '@octokit/rest';
import {
EventType,
Expand All @@ -11,9 +12,11 @@ import {
Organization,
Repository,
} from './github-webhook.const';
import { markdownParser, MarkdownSection } from './utils/markdown';
import { MarkdownSection, markdownParser } from './utils/markdown';

export class GithubClient extends Octokit {
const OctokitWithPlugins = Octokit.plugin(throttling);

export class GithubClient extends OctokitWithPlugins {
async issuesGetLabel(params: GetIssueLabelParams): Promise<GetIssueLabelResponse | undefined> {
try {
const labelResponse = await this.issues.getLabel(params);
Expand Down
28 changes: 28 additions & 0 deletions services/bots/src/github-webhook/github-webhook.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { createAppAuth } from '@octokit/auth-app';

import { Octokit } from '@octokit/rest';
import type { EndpointDefaults } from '@octokit/types';
import { EventType, WEBHOOK_HANDLERS } from './github-webhook.const';
import { GithubClient, WebhookContext } from './github-webhook.model';
import { uniqueEntries } from './utils/list';
Expand All @@ -19,6 +21,32 @@ export class GithubWebhookService {
installationId: Number(configService.get('github.installationId')),
privateKey: configService.get('github.keyContents'),
},
throttle: {
onRateLimit: (
retryAfter: number,
options: Required<EndpointDefaults>,
octokit: Octokit,
retryCount: number,
): boolean => {
octokit.log.warn(`Request quota exhausted for request ${options.method} ${options.url}`);

if (retryCount < 2) {
// Retry twice after hitting a rate limit error, then give up
octokit.log.info(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
onSecondaryRateLimit: (
retryAfter: number,
options: Required<EndpointDefaults>,
octokit: Octokit,
) => {
// does not retry, only logs a warning
octokit.log.warn(
`SecondaryRateLimit detected for request ${options.method} ${options.url}`,
);
},
},
});
}

Expand Down
36 changes: 36 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,13 @@ __metadata:
languageName: node
linkType: hard

"@octokit/openapi-types@npm:^19.1.0":
version: 19.1.0
resolution: "@octokit/openapi-types@npm:19.1.0"
checksum: 9d1b188741609a9832b964df2bc337ee77c1fc89d5f686faebb743c7cb27721e214180d623ee28227427b4c43719b79ee4890e338a709b78a9f249a7c369ac3e
languageName: node
linkType: hard

"@octokit/plugin-paginate-rest@npm:^4.0.0":
version: 4.2.2
resolution: "@octokit/plugin-paginate-rest@npm:4.2.2"
Expand Down Expand Up @@ -1755,6 +1762,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:^3.0.0":
version: 3.0.1
resolution: "@octokit/request-error@npm:3.0.1"
Expand Down Expand Up @@ -1792,6 +1811,15 @@ __metadata:
languageName: node
linkType: hard

"@octokit/types@npm:^12.2.0":
version: 12.4.0
resolution: "@octokit/types@npm:12.4.0"
dependencies:
"@octokit/openapi-types": ^19.1.0
checksum: 17bca450efc5433f14e1f93a24232316a0fb490aec8d886c3a430e853f10a74e6664751a44e0e199336b23c20658c4afcb3590e422ba7c155c2cb31f433ebbb7
languageName: node
linkType: hard

"@octokit/types@npm:^7.0.0, @octokit/types@npm:^7.3.0":
version: 7.3.0
resolution: "@octokit/types@npm:7.3.0"
Expand Down Expand Up @@ -3358,6 +3386,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 @@ -8225,6 +8260,7 @@ __metadata:
"@nestjs/testing": ^8.4.4
"@nestjs/websockets": ^8.4.4
"@octokit/auth-app": ^4.0.6
"@octokit/plugin-throttling": ^8.1.3
"@octokit/rest": ^19.0.4
"@octokit/webhooks": ^10.1.5
"@octokit/webhooks-types": ^6.4.0
Expand Down

0 comments on commit 39fb9e9

Please sign in to comment.