Skip to content

Commit db8a109

Browse files
varlAndarist
andauthored
Wire up @octokit/plugin-throttling with all GitHub Octokit instances (#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>
1 parent 4cb1c2f commit db8a109

File tree

3 files changed

+58
-37
lines changed

3 files changed

+58
-37
lines changed

.changeset/new-worms-knock.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@changesets/action": patch
3+
---
4+
5+
Wire up [`@octokit/plugin-throttling`](https://github.com/octokit/plugin-throttling.js) with all GitHub Octokit instances

src/run.test.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fixturez from "fixturez";
22
import * as github from "@actions/github";
3+
import * as githubUtils from "@actions/github/lib/utils";
34
import fs from "fs-extra";
45
import path from "path";
56
import writeChangeset from "@changesets/write";
@@ -15,7 +16,19 @@ jest.mock("@actions/github", () => ({
1516
ref: "refs/heads/some-branch",
1617
sha: "xeac7",
1718
},
18-
getOctokit: jest.fn(),
19+
}));
20+
jest.mock("@actions/github/lib/utils", () => ({
21+
GitHub: {
22+
plugin: () => {
23+
// function necessary to be used as constructor
24+
return function() {
25+
return {
26+
rest: mockedGithubMethods,
27+
}
28+
}
29+
},
30+
},
31+
getOctokitOptions: jest.fn(),
1932
}));
2033
jest.mock("./gitUtils");
2134

@@ -30,9 +43,6 @@ let mockedGithubMethods = {
3043
createRelease: jest.fn(),
3144
},
3245
};
33-
(github.getOctokit as any).mockImplementation(() => ({
34-
rest: mockedGithubMethods,
35-
}));
3646

3747
let f = fixturez(__dirname);
3848

src/run.ts

+39-33
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,42 @@ import type {} from "@octokit/plugin-throttling/dist-types/types.d";
2626
// To avoid that, we ensure to cap the message to 60k chars.
2727
const MAX_CHARACTERS_PER_MESSAGE = 60000;
2828

29+
const setupOctokit = (githubToken) => {
30+
return new (GitHub.plugin(throttling))(
31+
getOctokitOptions(githubToken, {
32+
throttle: {
33+
onRateLimit: (retryAfter, options: any, octokit, retryCount) => {
34+
core.warning(
35+
`Request quota exhausted for request ${options.method} ${options.url}`
36+
);
37+
38+
if (retryCount <= 2) {
39+
core.info(`Retrying after ${retryAfter} seconds!`);
40+
return true;
41+
}
42+
},
43+
onSecondaryRateLimit: (
44+
retryAfter,
45+
options: any,
46+
octokit,
47+
retryCount
48+
) => {
49+
core.warning(
50+
`SecondaryRateLimit detected for request ${options.method} ${options.url}`
51+
);
52+
53+
if (retryCount <= 2) {
54+
core.info(`Retrying after ${retryAfter} seconds!`);
55+
return true;
56+
}
57+
},
58+
},
59+
})
60+
);
61+
};
62+
2963
const createRelease = async (
30-
octokit: ReturnType<typeof github.getOctokit>,
64+
octokit: ReturnType<typeof GitHub>,
3165
{ pkg, tagName }: { pkg: Package; tagName: string }
3266
) => {
3367
try {
@@ -83,37 +117,7 @@ export async function runPublish({
83117
createGithubReleases,
84118
cwd = process.cwd(),
85119
}: PublishOptions): Promise<PublishResult> {
86-
const octokit = new (GitHub.plugin(throttling))(
87-
getOctokitOptions(githubToken, {
88-
throttle: {
89-
onRateLimit: (retryAfter, options: any, octokit, retryCount) => {
90-
core.warning(
91-
`Request quota exhausted for request ${options.method} ${options.url}`
92-
);
93-
94-
if (retryCount <= 2) {
95-
core.info(`Retrying after ${retryAfter} seconds!`);
96-
return true;
97-
}
98-
},
99-
onSecondaryRateLimit: (
100-
retryAfter,
101-
options: any,
102-
octokit,
103-
retryCount
104-
) => {
105-
core.warning(
106-
`SecondaryRateLimit detected for request ${options.method} ${options.url}`
107-
);
108-
109-
if (retryCount <= 2) {
110-
core.info(`Retrying after ${retryAfter} seconds!`);
111-
return true;
112-
}
113-
},
114-
},
115-
})
116-
);
120+
const octokit = setupOctokit(githubToken);
117121

118122
let [publishCommand, ...publishArgs] = script.split(/\s+/);
119123

@@ -302,10 +306,12 @@ export async function runVersion({
302306
hasPublishScript = false,
303307
prBodyMaxCharacters = MAX_CHARACTERS_PER_MESSAGE,
304308
}: VersionOptions): Promise<RunVersionResult> {
309+
const octokit = setupOctokit(githubToken);
310+
305311
let repo = `${github.context.repo.owner}/${github.context.repo.repo}`;
306312
let branch = github.context.ref.replace("refs/heads/", "");
307313
let versionBranch = `changeset-release/${branch}`;
308-
let octokit = github.getOctokit(githubToken);
314+
309315
let { preState } = await readChangesetState(cwd);
310316

311317
await gitUtils.switchToMaybeExistingBranch(versionBranch);

0 commit comments

Comments
 (0)