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: prevent hitting github secondary rate limits #286

Merged
merged 7 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
21 changes: 21 additions & 0 deletions .changeset/rotten-carrots-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
"@changesets/action": patch
---

add rate limit plugin for octokit

The changesets GitHub Action triggers 403 (secondary rate limits)[1]
against the GitHub API, which causes the CI jobs to fail, and the only
known workaround is to simply re-run the job.

This patch implements the `@octokit/plugin-throttling`[2] plugin and wires
it up with the GitHub Octokit instance[3].

This plugin is recommended by the Octokit docs[4] as it implements all
the GitHub best practices for integrators[5].

[1]: https://github.com/changesets/action/issues/192
[2]: https://github.com/octokit/plugin-throttling.js
[3]: https://github.com/actions/toolkit/blob/main/packages/github/src/github.ts#LL18C40-L18C40
[4]: https://octokit.github.io/rest.js/v19#throttling
[5]: https://docs.github.com/en/rest/guides/best-practices-for-integrators?apiVersion=2022-11-28
Andarist marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"fixturez": "^1.1.0",
"parcel": "^1.12.3",
"prettier": "^2.0.5",
"typescript": "^3.5.3"
"typescript": "^5.0.4"
},
"scripts": {
"build": "parcel build ./src/index.ts --no-source-maps --target=node --bundle-node-modules",
Expand All @@ -33,6 +33,7 @@
"@changesets/pre": "^1.0.9",
"@changesets/read": "^0.5.3",
"@manypkg/get-packages": "^1.1.3",
"@octokit/plugin-throttling": "5.1.1",
"@types/fs-extra": "^8.0.0",
"@types/jest": "^24.0.18",
"@types/node": "^12.7.1",
Expand All @@ -51,5 +52,8 @@
"husky": {
"hooks": {}
},
"prettier": {}
"prettier": {},
"resolutions": {
"**/@octokit/core": "4.2.0"
}
}
37 changes: 36 additions & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { exec, getExecOutput } from "@actions/exec";
import { GitHub, getOctokitOptions } from "@actions/github/lib/utils";
import * as github from "@actions/github";
import fs from "fs-extra";
import { getPackages, Package } from "@manypkg/get-packages";
Expand All @@ -14,6 +15,9 @@ import {
import * as gitUtils from "./gitUtils";
import readChangesetState from "./readChangesetState";
import resolveFrom from "resolve-from";
import { throttling } from "@octokit/plugin-throttling";
// temporary workaround for https://github.com/octokit/plugin-throttling.js/pull/590
import type {} from "@octokit/plugin-throttling/dist-types/types.d";

// GitHub Issues/PRs messages have a max size limit on the
// message body payload.
Expand Down Expand Up @@ -78,7 +82,38 @@ export async function runPublish({
createGithubReleases,
cwd = process.cwd(),
}: PublishOptions): Promise<PublishResult> {
let octokit = github.getOctokit(githubToken);
const octokit = new (GitHub.plugin(throttling))(
getOctokitOptions(githubToken, {
throttle: {
onRateLimit: (retryAfter, options: any, octokit, retryCount) => {
varl marked this conversation as resolved.
Show resolved Hide resolved
console.log(
`Request quota exhausted for request ${options.method} ${options.url}`
);

if (retryCount <= 2) {
console.log(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
onSecondaryRateLimit: (
retryAfter,
options: any,
octokit,
retryCount
) => {
console.log(
`SecondaryRateLimit detected for request ${options.method} ${options.url}`
);

if (retryCount <= 2) {
console.log(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
},
})
);

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

let changesetPublishOutput = await getExecOutput(
Expand Down
Loading