Skip to content
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

fix: use throttling-wired octokit everywhere #291

Merged
merged 3 commits into from May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/new-worms-knock.md
@@ -0,0 +1,5 @@
---
"@changesets/action": patch
---

use throttling plugin for all octokit instances
Andarist marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 14 additions & 4 deletions 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";
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
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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, now it makes sense... I totally missed the fact that each command~ has its own octokit instance

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