Skip to content

Commit

Permalink
Wire up @octokit/plugin-throttling with all GitHub Octokit instances (
Browse files Browse the repository at this point in the history
#291)

* fix: use throttling-wired octokit everywhere

* test: fix run.test.ts

* Update .changeset/new-worms-knock.md

---------

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
varl and Andarist authored May 9, 2023
1 parent 4cb1c2f commit db8a109
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-worms-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/action": patch
---

Wire up [`@octokit/plugin-throttling`](https://github.com/octokit/plugin-throttling.js) with all GitHub Octokit instances
18 changes: 14 additions & 4 deletions src/run.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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");

Expand All @@ -30,9 +43,6 @@ let mockedGithubMethods = {
createRelease: jest.fn(),
},
};
(github.getOctokit as any).mockImplementation(() => ({
rest: mockedGithubMethods,
}));

let f = fixturez(__dirname);

Expand Down
72 changes: 39 additions & 33 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,42 @@ 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<typeof github.getOctokit>,
octokit: ReturnType<typeof GitHub>,
{ pkg, tagName }: { pkg: Package; tagName: string }
) => {
try {
Expand Down Expand Up @@ -83,37 +117,7 @@ export async function runPublish({
createGithubReleases,
cwd = process.cwd(),
}: PublishOptions): Promise<PublishResult> {
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+/);

Expand Down Expand Up @@ -302,10 +306,12 @@ export async function runVersion({
hasPublishScript = false,
prBodyMaxCharacters = MAX_CHARACTERS_PER_MESSAGE,
}: VersionOptions): Promise<RunVersionResult> {
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);
Expand Down

0 comments on commit db8a109

Please sign in to comment.