Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions packages/gittensory-miner/lib/opportunity-fanout.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function fetchCandidateIssuesWithSummary(
apiBaseUrl?: string;
concurrency?: number;
perPage?: number;
sleepFn?: (ms: number) => Promise<unknown>;
},
): Promise<CandidateIssueSummary>;

Expand All @@ -48,6 +49,7 @@ export function fetchCandidateIssues(
apiBaseUrl?: string;
concurrency?: number;
perPage?: number;
sleepFn?: (ms: number) => Promise<unknown>;
},
): Promise<RawCandidateIssue[]>;

Expand All @@ -58,6 +60,7 @@ export function searchCandidateIssuesWithSummary(
apiBaseUrl?: string;
concurrency?: number;
perPage?: number;
sleepFn?: (ms: number) => Promise<unknown>;
},
): Promise<CandidateIssueSummary>;

Expand All @@ -68,5 +71,6 @@ export function searchCandidateIssues(
apiBaseUrl?: string;
concurrency?: number;
perPage?: number;
sleepFn?: (ms: number) => Promise<unknown>;
},
): Promise<RawCandidateIssue[]>;
23 changes: 15 additions & 8 deletions packages/gittensory-miner/lib/opportunity-fanout.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Buffer } from "node:buffer";
import { resolveAiPolicyVerdict } from "@jsonbored/gittensory-engine";
import { fetchWithRetry } from "./http-retry.js";

const defaultApiBaseUrl = "https://api.github.com";
const defaultConcurrency = 5;
Expand Down Expand Up @@ -101,11 +102,15 @@ function recordRateLimit(summary, response) {
}
}

async function githubGetJson(url, githubToken, summary) {
const response = await fetch(url, {
method: "GET",
headers: githubHeaders(githubToken),
});
async function githubGetJson(url, githubToken, summary, options) {
// Retry a transient 5xx from GitHub before dropping this target's results for the whole run (#4830) — the same
// discipline as the CI/gate-verdict pollers. A thrown network error still propagates to each caller's try/catch.
const response = await fetchWithRetry(
fetch,
url,
{ method: "GET", headers: githubHeaders(githubToken) },
{ sleepFn: options?.sleepFn },
);
recordRateLimit(summary, response);
const payload = await response.json().catch(() => null);
return { response, payload };
Expand All @@ -130,7 +135,7 @@ async function fetchRepoDoc(target, path, githubToken, options, summary, warning
repoPath(target, `/contents/${encodeURIComponent(path)}`),
);
try {
const { response, payload } = await githubGetJson(url, githubToken, summary);
const { response, payload } = await githubGetJson(url, githubToken, summary, options);
if (response.status === 404) return null;
if (!response.ok) {
warnings.push(warning(target, `policy:${path}`, `GitHub returned ${response.status}`));
Expand Down Expand Up @@ -212,7 +217,7 @@ async function fetchTargetIssues(target, githubToken, options, summary, warnings
`?state=open&per_page=${options.perPage}`,
);
try {
const { response, payload } = await githubGetJson(url, githubToken, summary);
const { response, payload } = await githubGetJson(url, githubToken, summary, options);
if (!response.ok) {
warnings.push(warning(target, "issues", `GitHub returned ${response.status}`));
return [];
Expand Down Expand Up @@ -242,7 +247,7 @@ async function fetchSearchIssues(searchQuery, githubToken, options, summary, war
`?q=${encodeURIComponent(qualifiedQuery)}&per_page=${options.perPage}`,
);
try {
const { response, payload } = await githubGetJson(url, githubToken, summary);
const { response, payload } = await githubGetJson(url, githubToken, summary, options);
if (!response.ok) {
warnings.push({
repoFullName: "*",
Expand Down Expand Up @@ -292,6 +297,8 @@ function normalizeOptions(options = {}) {
: defaultApiBaseUrl,
concurrency: normalizeLimit(options.concurrency, defaultConcurrency, 1, 10),
perPage: normalizeLimit(options.perPage, defaultPerPage, 1, 100),
// Passed through to the per-fetch retry so tests can inject an instant sleep; undefined uses the real backoff.
sleepFn: typeof options.sleepFn === "function" ? options.sleepFn : undefined,
};
}

Expand Down
27 changes: 26 additions & 1 deletion test/unit/miner-opportunity-fanout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe("fetchCandidateIssues (#2307)", () => {
{ owner: "acme", repo: "up" },
],
"token",
{ apiBaseUrl: API },
{ apiBaseUrl: API, sleepFn: () => Promise.resolve() }, // instant retry: a persistent 503 still warns
);

expect(result.issues.map((entry) => entry.issueNumber)).toEqual([11]);
Expand Down Expand Up @@ -301,11 +301,36 @@ describe("fetchCandidateIssues (#2307)", () => {

const result = await searchCandidateIssuesWithSummary("label:feature", "token", {
apiBaseUrl: API,
sleepFn: () => Promise.resolve(), // instant retry: a persistent 502 still warns
});

expect(result.issues).toEqual([]);
expect(result.warnings).toEqual([
{ repoFullName: "*", stage: "search", message: "GitHub returned 502" },
]);
});

it("retries a transient 5xx and keeps the target's issues instead of dropping them (#4830)", async () => {
let issuesAttempts = 0;
vi.stubGlobal("fetch", async (input: RequestInfo | URL) => {
const url = String(input);
if (url.endsWith("/contents/AI-USAGE.md")) return jsonResponse({}, { status: 404 });
if (url.endsWith("/contents/CONTRIBUTING.md")) return contentResponse("Contributions welcome.");
if (url.includes("/repos/acme/blip/issues?")) {
issuesAttempts += 1;
if (issuesAttempts === 1) return jsonResponse({ message: "server error" }, { status: 503 }); // a blip
return jsonResponse([issue(7)]);
}
return jsonResponse({}, { status: 404 });
});

const result = await fetchCandidateIssuesWithSummary([{ owner: "acme", repo: "blip" }], "token", {
apiBaseUrl: API,
sleepFn: () => Promise.resolve(),
});

expect(issuesAttempts).toBe(2); // the 503 was retried, then succeeded
expect(result.issues.map((entry) => entry.issueNumber)).toEqual([7]); // results kept, not dropped
expect(result.warnings).toEqual([]); // no warning — the transient blip recovered
});
});