Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(release): retry transient GitHub reads (#800)

mldangelo-oai8b30f769fe4b
diff --git a/sdk/typescript/scripts/release-pr.mjs b/sdk/typescript/scripts/release-pr.mjs
index 0f67e439c..ac3edd4e6 100644
--- a/sdk/typescript/scripts/release-pr.mjs
+++ b/sdk/typescript/scripts/release-pr.mjs
@@ -1,5 +1,6 @@
import { execFileSync } from "node:child_process";
import { createHash } from "node:crypto";
+import { setTimeout as delay } from "node:timers/promises";
import { fileURLToPath, pathToFileURL } from "node:url";
import { isDeepStrictEqual } from "node:util";
import { assertStableVersion, releaseVersion } from "./release-automation.mjs";
@@ -513,7 +514,7 @@ export async function reconcileReleasePullRequest({
github,
dryRun = true,
}) {
- // Re-read only when main or the proposal actually moves; API failures are not retried blindly.
+ // Rebuild the plan only when main or the proposal actually moves.
for (;;) {
const mainSha = await branchHead(github, "main");
const history = await readReleaseHistory(repo, mainSha, github);
@@ -630,24 +631,33 @@ export async function reconcileReleasePullRequest({
export function createGitHubClient(repository, token, fetcher = fetch) {
const apiRoot = `https://api.github.com/repos/${repository}/`;
async function responseFor(method, path, body) {
- const response = await fetcher(`${apiRoot}${path}`, {
- method,
- headers: {
- Accept: "application/vnd.github+json",
- Authorization: `Bearer ${token}`,
- "X-GitHub-Api-Version": "2026-03-10",
- "Content-Type": "application/json",
- },
- body: body === undefined ? undefined : JSON.stringify(body),
- });
- if (!response.ok) {
+ for (let retry = 0; ; retry += 1) {
+ const response = await fetcher(`${apiRoot}${path}`, {
+ method,
+ headers: {
+ Accept: "application/vnd.github+json",
+ Authorization: `Bearer ${token}`,
+ "X-GitHub-Api-Version": "2026-03-10",
+ "Content-Type": "application/json",
+ },
+ body: body === undefined ? undefined : JSON.stringify(body),
+ });
+ if (response.ok) return response;
+ if (
+ method === "GET" &&
+ retry < 2 &&
+ [500, 502, 503, 504].includes(response.status)
+ ) {
+ await response.body?.cancel().catch(() => undefined);
+ await delay(1000 * 2 ** retry);
+ continue;
+ }
const error = new Error(
`GitHub ${method} ${path} failed with HTTP ${response.status}.`,
);
error.status = response.status;
throw error;
}
- return response;
}
return {
repository,
diff --git a/sdk/typescript/tests-ts/release-pr.test.ts b/sdk/typescript/tests-ts/release-pr.test.ts
index 354762d21..6549d9f10 100644
--- a/sdk/typescript/tests-ts/release-pr.test.ts
+++ b/sdk/typescript/tests-ts/release-pr.test.ts
@@ -435,6 +435,84 @@ class FakeGitHub {
}
describe("GitHub request transport", () => {
+ test("recovers from an errored response body without duplicating paginated results", async () => {
+ const pages: number[] = [];
+ const github = createGitHubClient(
+ "example/release-fixture",
+ "synthetic-release-token",
+ async (url, options) => {
+ expect(options.method).toBe("GET");
+ const page = Number(new URL(url).searchParams.get("page"));
+ pages.push(page);
+ if (pages.length === 2) {
+ const body = new ReadableStream({
+ start(controller) {
+ controller.error(new Error("Synthetic response body failure"));
+ },
+ });
+ return new Response(body, { status: 500 });
+ }
+ return Response.json([{ number: page }], {
+ headers:
+ page === 1
+ ? {
+ link: '<https://api.github.com/repos/example/release-fixture/pulls?per_page=1&page=2>; rel="next"',
+ }
+ : {},
+ });
+ },
+ );
+ expect(await github.list("pulls?per_page=1")).toEqual([
+ { number: 1 },
+ { number: 2 },
+ ]);
+ expect(pages).toEqual([1, 2, 2]);
+ });
+
+ test("reports a persistent server error after bounded read retries", async () => {
+ let requests = 0;
+ const github = createGitHubClient(
+ "example/release-fixture",
+ "synthetic-release-token",
+ async () => {
+ requests++;
+ return Response.json({ message: "Unavailable" }, { status: 503 });
+ },
+ );
+ await expect(
+ github.request("GET", "git/ref/heads/main"),
+ ).rejects.toMatchObject({
+ status: 503,
+ message: "GitHub GET git/ref/heads/main failed with HTTP 503.",
+ });
+ expect(requests).toBe(3);
+ });
+
+ test.each([
+ ["POST", 500],
+ ["PATCH", 503],
+ ["GET", 403],
+ ["GET", 404],
+ ["GET", 422],
+ ])(
+ "does not replay a %s request that fails with HTTP %i",
+ async (method, status) => {
+ let requests = 0;
+ const github = createGitHubClient(
+ "example/release-fixture",
+ "synthetic-release-token",
+ async () => {
+ requests++;
+ return Response.json({ message: "Failed request" }, { status });
+ },
+ );
+ await expect(github.request(method, "pulls")).rejects.toMatchObject({
+ status,
+ });
+ expect(requests).toBe(1);
+ },
+ );
+
test("creates and updates a draft through serialized requests, including a concurrent commit conflict", async () => {
const fixture = new Fixture();
fixture.merge("feat: initial feature");