From fa6776cd6b86567a1960259acec4c283de0b6e7b Mon Sep 17 00:00:00 2001 From: Viktor Varland Date: Tue, 9 May 2023 13:33:47 +0200 Subject: [PATCH 1/3] fix: use throttling-wired octokit everywhere --- .changeset/new-worms-knock.md | 5 +++ src/run.ts | 70 +++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 .changeset/new-worms-knock.md diff --git a/.changeset/new-worms-knock.md b/.changeset/new-worms-knock.md new file mode 100644 index 00000000..9b3ec916 --- /dev/null +++ b/.changeset/new-worms-knock.md @@ -0,0 +1,5 @@ +--- +"@changesets/action": patch +--- + +use throttling plugin for all octokit instances diff --git a/src/run.ts b/src/run.ts index e1789a3c..a3886add 100644 --- a/src/run.ts +++ b/src/run.ts @@ -26,6 +26,40 @@ import type {} from "@octokit/plugin-throttling/dist-types/types.d"; // To avoid that, we ensure to cap the message to 60k chars. const MAX_CHARACTERS_PER_MESSAGE = 60000; +const setupOctokit = (githubToken) => { + return new (GitHub.plugin(throttling))( + getOctokitOptions(githubToken, { + throttle: { + onRateLimit: (retryAfter, options: any, octokit, retryCount) => { + core.warning( + `Request quota exhausted for request ${options.method} ${options.url}` + ); + + if (retryCount <= 2) { + core.info(`Retrying after ${retryAfter} seconds!`); + return true; + } + }, + onSecondaryRateLimit: ( + retryAfter, + options: any, + octokit, + retryCount + ) => { + core.warning( + `SecondaryRateLimit detected for request ${options.method} ${options.url}` + ); + + if (retryCount <= 2) { + core.info(`Retrying after ${retryAfter} seconds!`); + return true; + } + }, + }, + }) + ); +}; + const createRelease = async ( octokit: ReturnType, { pkg, tagName }: { pkg: Package; tagName: string } @@ -83,37 +117,7 @@ export async function runPublish({ createGithubReleases, cwd = process.cwd(), }: PublishOptions): Promise { - const octokit = new (GitHub.plugin(throttling))( - getOctokitOptions(githubToken, { - throttle: { - onRateLimit: (retryAfter, options: any, octokit, retryCount) => { - core.warning( - `Request quota exhausted for request ${options.method} ${options.url}` - ); - - if (retryCount <= 2) { - core.info(`Retrying after ${retryAfter} seconds!`); - return true; - } - }, - onSecondaryRateLimit: ( - retryAfter, - options: any, - octokit, - retryCount - ) => { - core.warning( - `SecondaryRateLimit detected for request ${options.method} ${options.url}` - ); - - if (retryCount <= 2) { - core.info(`Retrying after ${retryAfter} seconds!`); - return true; - } - }, - }, - }) - ); + const octokit = setupOctokit(githubToken); let [publishCommand, ...publishArgs] = script.split(/\s+/); @@ -302,10 +306,12 @@ export async function runVersion({ hasPublishScript = false, prBodyMaxCharacters = MAX_CHARACTERS_PER_MESSAGE, }: VersionOptions): Promise { + const octokit = setupOctokit(githubToken); + let repo = `${github.context.repo.owner}/${github.context.repo.repo}`; let branch = github.context.ref.replace("refs/heads/", ""); let versionBranch = `changeset-release/${branch}`; - let octokit = github.getOctokit(githubToken); + let { preState } = await readChangesetState(cwd); await gitUtils.switchToMaybeExistingBranch(versionBranch); From e70152dfbddc3b4d3973450a31fe9b862ae35d95 Mon Sep 17 00:00:00 2001 From: Viktor Varland Date: Tue, 9 May 2023 16:56:48 +0200 Subject: [PATCH 2/3] test: fix run.test.ts --- src/run.test.ts | 18 ++++++++++++++---- src/run.ts | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/run.test.ts b/src/run.test.ts index c8053c37..ff9ccfdf 100644 --- a/src/run.test.ts +++ b/src/run.test.ts @@ -1,5 +1,6 @@ import fixturez from "fixturez"; import * as github from "@actions/github"; +import * as githubUtils from "@actions/github/lib/utils"; import fs from "fs-extra"; import path from "path"; import writeChangeset from "@changesets/write"; @@ -15,7 +16,19 @@ jest.mock("@actions/github", () => ({ ref: "refs/heads/some-branch", sha: "xeac7", }, - getOctokit: jest.fn(), +})); +jest.mock("@actions/github/lib/utils", () => ({ + GitHub: { + plugin: () => { + // function necessary to be used as constructor + return function() { + return { + rest: mockedGithubMethods, + } + } + }, + }, + getOctokitOptions: jest.fn(), })); jest.mock("./gitUtils"); @@ -30,9 +43,6 @@ let mockedGithubMethods = { createRelease: jest.fn(), }, }; -(github.getOctokit as any).mockImplementation(() => ({ - rest: mockedGithubMethods, -})); let f = fixturez(__dirname); diff --git a/src/run.ts b/src/run.ts index a3886add..45a29c15 100644 --- a/src/run.ts +++ b/src/run.ts @@ -61,7 +61,7 @@ const setupOctokit = (githubToken) => { }; const createRelease = async ( - octokit: ReturnType, + octokit: ReturnType, { pkg, tagName }: { pkg: Package; tagName: string } ) => { try { From 3ea6ad456e18fc5717952c70b2bfbe27f4dd00bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Tue, 9 May 2023 17:19:17 +0200 Subject: [PATCH 3/3] Update .changeset/new-worms-knock.md --- .changeset/new-worms-knock.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/new-worms-knock.md b/.changeset/new-worms-knock.md index 9b3ec916..f81688d3 100644 --- a/.changeset/new-worms-knock.md +++ b/.changeset/new-worms-knock.md @@ -2,4 +2,4 @@ "@changesets/action": patch --- -use throttling plugin for all octokit instances +Wire up [`@octokit/plugin-throttling`](https://github.com/octokit/plugin-throttling.js) with all GitHub Octokit instances