Auto-retry ratelimited octokit requests

This commit is contained in:
Mateusz Burzyński
2026-01-07 17:10:13 +01:00
parent 0504fc42ff
commit a483711fee
9 changed files with 3759 additions and 2172 deletions
+2 -4
View File
@@ -1,6 +1,5 @@
import { Octokit } from "@octokit/rest";
import { fetchWorkflowRunInfo } from "./api.ts";
import { getGitHubInstallationToken, parseRepoContext } from "./github.ts";
import { createOctokit, getGitHubInstallationToken, parseRepoContext } from "./github.ts";
/**
* Get progress comment ID from environment variable or database.
@@ -55,8 +54,7 @@ export async function reportErrorToComment({
// update comment directly using GitHub API
const repoContext = parseRepoContext();
const token = getGitHubInstallationToken();
const octokit = new Octokit({ auth: token });
const octokit = createOctokit(getGitHubInstallationToken());
await octokit.rest.issues.updateComment({
owner: repoContext.owner,
+22 -1
View File
@@ -1,5 +1,7 @@
import { createSign } from "node:crypto";
import * as core from "@actions/core";
import { throttling } from "@octokit/plugin-throttling";
import { Octokit } from "@octokit/rest";
import { createSign } from "node:crypto";
import { log } from "./cli.ts";
import { retry } from "./retry.ts";
@@ -311,3 +313,22 @@ export function parseRepoContext(): RepoContext {
return { owner, name };
}
export type OctokitWithPlugins = InstanceType<ReturnType<typeof Octokit.plugin<typeof Octokit, [typeof throttling]>>>
export function createOctokit(token: string): OctokitWithPlugins {
// `OctokitWithPlugins` initialization based on https://github.com/actions/toolkit/blob/2506e78e82fbd2f9e94d63e75f5309118c8de1b1/packages/github/src/github.ts#L15-L22
// we can't use it directly because it's stuck on `@octokit/core@v5` and we use the hottest `@octokit/core@v7`
const OctokitWithPlugins = Octokit.plugin(throttling);
return new OctokitWithPlugins({
auth: token,
throttle: {
onRateLimit: (retryAfter, options, octokit, retryCount) => {
return retryCount <= 2;
},
onSecondaryRateLimit: (retryAfter, options, octokit, retryCount) => {
return retryCount <= 2;
},
}
});
}
+2 -2
View File
@@ -1,10 +1,10 @@
import { execSync } from "node:child_process";
import { existsSync, rmSync } from "node:fs";
import type { Octokit } from "@octokit/rest";
import type { Payload } from "../external.ts";
import type { ToolState } from "../main.ts";
import { checkoutPrBranch } from "../mcp/checkout.ts";
import { log } from "./cli.ts";
import type { OctokitWithPlugins } from "./github.ts";
import { $ } from "./shell.ts";
export interface SetupOptions {
@@ -66,7 +66,7 @@ interface SetupGitAuthParams {
owner: string;
name: string;
payload: Payload;
octokit: Octokit;
octokit: OctokitWithPlugins;
toolState: ToolState;
}