Skip to content

fix: add warnings to log for GitHub rate limits #3988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lambdas/functions/control-plane/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const config: Config = {
...defaultConfig,
coverageThreshold: {
global: {
statements: 97.99,
branches: 96.04,
functions: 97.53,
lines: 98.3,
statements: 97.79,
branches: 96.13,
functions: 95.4,
lines: 98.06,
},
},
};
Expand Down
1 change: 1 addition & 0 deletions lambdas/functions/control-plane/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@aws-sdk/types": "^3.433.0",
"@middy/core": "^4.7.0",
"@octokit/auth-app": "6.0.3",
"@octokit/plugin-throttling": "8.1.3",
"@octokit/rest": "20.0.2",
"@octokit/types": "^13.5.0",
"@terraform-aws-github-runner/aws-powertools-util": "*",
Expand Down
18 changes: 17 additions & 1 deletion lambdas/functions/control-plane/src/gh-auth/gh-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import {
import { OctokitOptions } from '@octokit/core/dist-types/types';
import { request } from '@octokit/request';
import { Octokit } from '@octokit/rest';
import { throttling } from '@octokit/plugin-throttling';
import { createChildLogger } from '@terraform-aws-github-runner/aws-powertools-util';
import { getParameter } from '@terraform-aws-github-runner/aws-ssm-util';

import { axiosFetch } from '../axios/fetch-override';

const logger = createChildLogger('gh-auth');

export async function createOctoClient(token: string, ghesApiUrl = ''): Promise<Octokit> {
const CustomOctokit = Octokit.plugin(throttling);
const ocktokitOptions: OctokitOptions = {
auth: token,
request: { fetch: axiosFetch },
Expand All @@ -25,7 +28,20 @@ export async function createOctoClient(token: string, ghesApiUrl = ''): Promise<
ocktokitOptions.baseUrl = ghesApiUrl;
ocktokitOptions.previews = ['antiope'];
}
return new Octokit(ocktokitOptions);

return new CustomOctokit({
...ocktokitOptions,
throttle: {
onRateLimit: (retryAfter, options) => {
logger.warn(
`GitHub rate limit: Request quota exhausted for request ${options.method} ${options.url}. Requested `,
);
},
onSecondaryRateLimit: (retryAfter, options) => {
logger.warn(`GitHub rate limit: SecondaryRateLimit detected for request ${options.method} ${options.url}`);
},
},
});
}

export async function createGithubAppAuth(
Expand Down
15 changes: 14 additions & 1 deletion lambdas/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2736,6 +2736,14 @@
dependencies:
"@octokit/types" "^12.0.0"

"@octokit/[email protected]":
version "8.1.3"
resolved "https://registry.yarnpkg.com/@octokit/plugin-throttling/-/plugin-throttling-8.1.3.tgz#7fb0e001c0cb9383c6be07740b8ec326ed990f6b"
integrity sha512-pfyqaqpc0EXh5Cn4HX9lWYsZ4gGbjnSmUILeu4u2gnuM50K/wIk9s1Pxt3lVeVwekmITgN/nJdoh43Ka+vye8A==
dependencies:
"@octokit/types" "^12.2.0"
bottleneck "^2.15.3"

"@octokit/request-error@^5.0.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@octokit/request-error/-/request-error-5.1.0.tgz#ee4138538d08c81a60be3f320cd71063064a3b30"
Expand Down Expand Up @@ -2766,7 +2774,7 @@
"@octokit/plugin-request-log" "^4.0.0"
"@octokit/plugin-rest-endpoint-methods" "^10.0.0"

"@octokit/types@^11.0.0", "@octokit/types@^11.1.0", "@octokit/types@^12.0.0", "@octokit/types@^13.0.0", "@octokit/types@^13.1.0", "@octokit/types@^13.5.0":
"@octokit/types@^11.0.0", "@octokit/types@^11.1.0", "@octokit/types@^12.0.0", "@octokit/types@^12.2.0", "@octokit/types@^13.0.0", "@octokit/types@^13.1.0", "@octokit/types@^13.5.0":
version "13.5.0"
resolved "https://registry.yarnpkg.com/@octokit/types/-/types-13.5.0.tgz#4796e56b7b267ebc7c921dcec262b3d5bfb18883"
integrity sha512-HdqWTf5Z3qwDVlzCrP8UJquMwunpDiMPt5er+QjGzL4hqr/vBVY/MauQgS1xWxCDT1oMx1EULyqxncdCY/NVSQ==
Expand Down Expand Up @@ -4323,6 +4331,11 @@ [email protected], body-parser@^1.20.2:
type-is "~1.6.18"
unpipe "1.0.0"

bottleneck@^2.15.3:
version "2.19.5"
resolved "https://registry.yarnpkg.com/bottleneck/-/bottleneck-2.19.5.tgz#5df0b90f59fd47656ebe63c78a98419205cadd91"
integrity sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw==

bowser@^2.11.0:
version "2.11.0"
resolved "https://registry.yarnpkg.com/bowser/-/bowser-2.11.0.tgz#5ca3c35757a7aa5771500c70a73a9f91ef420a8f"
Expand Down