mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-03 19:24:05 +00:00
fix(github): bound proof comment API bodies
This commit is contained in:
@@ -6,9 +6,13 @@ import {
|
||||
evaluateClawSweeperExactHeadProof,
|
||||
evaluateRealBehaviorProof,
|
||||
isMaintainerTeamMember,
|
||||
readBoundedGitHubApiJson,
|
||||
withGitHubApiTimeout,
|
||||
} from "./real-behavior-proof-policy.mjs";
|
||||
|
||||
const PROOF_COMMENTS_PER_PAGE = 100;
|
||||
const MAX_PROOF_COMMENT_PAGES = 10;
|
||||
|
||||
function escapeCommandValue(value) {
|
||||
return String(value)
|
||||
.replace(/%/g, "%25")
|
||||
@@ -17,6 +21,100 @@ function escapeCommandValue(value) {
|
||||
.replace(/:/g, "%3A");
|
||||
}
|
||||
|
||||
function isTooLargeBodyError(error) {
|
||||
return error?.code === "ETOOBIG";
|
||||
}
|
||||
|
||||
async function fetchProofCommentPage({
|
||||
owner,
|
||||
repo,
|
||||
issueNumber,
|
||||
token,
|
||||
fetchImpl,
|
||||
timeoutMs,
|
||||
page,
|
||||
perPage,
|
||||
}) {
|
||||
const url = new URL(
|
||||
`https://api.github.com/repos/${owner}/${repo}/issues/${issueNumber}/comments`,
|
||||
);
|
||||
url.searchParams.set("per_page", String(perPage));
|
||||
url.searchParams.set("page", String(page));
|
||||
const response = await withGitHubApiTimeout(
|
||||
`proof comment lookup page ${page}`,
|
||||
timeoutMs,
|
||||
(signal) =>
|
||||
fetchImpl(url, {
|
||||
headers: {
|
||||
Accept: "application/vnd.github+json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
signal,
|
||||
}),
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error(`comments API returned ${response.status}`);
|
||||
}
|
||||
return await withGitHubApiTimeout(`proof comment response page ${page}`, timeoutMs, (signal) =>
|
||||
readBoundedGitHubApiJson(response, `proof comment response page ${page}`, undefined, {
|
||||
signal,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async function fetchOversizedProofCommentPageIndividually({
|
||||
owner,
|
||||
repo,
|
||||
issueNumber,
|
||||
token,
|
||||
fetchImpl,
|
||||
timeoutMs,
|
||||
page,
|
||||
perPage,
|
||||
}) {
|
||||
const comments = [];
|
||||
const firstSinglePage = (page - 1) * perPage + 1;
|
||||
for (let offset = 0; offset < perPage; offset += 1) {
|
||||
try {
|
||||
const pageComments = await fetchProofCommentPage({
|
||||
owner,
|
||||
repo,
|
||||
issueNumber,
|
||||
token,
|
||||
fetchImpl,
|
||||
timeoutMs,
|
||||
page: firstSinglePage + offset,
|
||||
perPage: 1,
|
||||
});
|
||||
comments.push(...pageComments);
|
||||
if (pageComments.length === 0) {
|
||||
return { comments, exhausted: true };
|
||||
}
|
||||
} catch (error) {
|
||||
if (!isTooLargeBodyError(error)) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
return { comments, exhausted: false };
|
||||
}
|
||||
|
||||
async function fetchProofCommentPageWithFallback(params) {
|
||||
try {
|
||||
const comments = await fetchProofCommentPage(params);
|
||||
return {
|
||||
comments,
|
||||
exhausted: comments.length < params.perPage,
|
||||
};
|
||||
} catch (error) {
|
||||
if (!isTooLargeBodyError(error) || params.perPage === 1) {
|
||||
throw error;
|
||||
}
|
||||
return await fetchOversizedProofCommentPageIndividually(params);
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchProofComments({
|
||||
owner,
|
||||
repo,
|
||||
@@ -29,35 +127,19 @@ export async function fetchProofComments({
|
||||
for (const token of tokens.filter(Boolean)) {
|
||||
const comments = [];
|
||||
try {
|
||||
for (let page = 1; page <= 10; page += 1) {
|
||||
const url = new URL(
|
||||
`https://api.github.com/repos/${owner}/${repo}/issues/${issueNumber}/comments`,
|
||||
);
|
||||
url.searchParams.set("per_page", "100");
|
||||
url.searchParams.set("page", String(page));
|
||||
const response = await withGitHubApiTimeout(
|
||||
`proof comment lookup page ${page}`,
|
||||
for (let page = 1; page <= MAX_PROOF_COMMENT_PAGES; page += 1) {
|
||||
const result = await fetchProofCommentPageWithFallback({
|
||||
owner,
|
||||
repo,
|
||||
issueNumber,
|
||||
token,
|
||||
fetchImpl,
|
||||
timeoutMs,
|
||||
(signal) =>
|
||||
fetchImpl(url, {
|
||||
headers: {
|
||||
Accept: "application/vnd.github+json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
signal,
|
||||
}),
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error(`comments API returned ${response.status}`);
|
||||
}
|
||||
const pageComments = await withGitHubApiTimeout(
|
||||
`proof comment response page ${page}`,
|
||||
timeoutMs,
|
||||
() => response.json(),
|
||||
);
|
||||
comments.push(...pageComments);
|
||||
if (pageComments.length < 100) {
|
||||
page,
|
||||
perPage: PROOF_COMMENTS_PER_PAGE,
|
||||
});
|
||||
comments.push(...result.comments);
|
||||
if (result.exhausted) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { readBoundedResponseText } from "../lib/bounded-response.mjs";
|
||||
|
||||
export const PROOF_OVERRIDE_LABEL = "proof: override";
|
||||
export const PROOF_SUPPLIED_LABEL = "proof: supplied";
|
||||
export const PROOF_SUFFICIENT_LABEL = "proof: sufficient";
|
||||
@@ -5,6 +7,7 @@ export const NEEDS_REAL_BEHAVIOR_PROOF_LABEL = "triage: needs-real-behavior-proo
|
||||
export const MOCK_ONLY_PROOF_LABEL = "triage: mock-only-proof";
|
||||
export const MAINTAINER_TEAM_SLUG = "maintainer";
|
||||
export const DEFAULT_GITHUB_API_TIMEOUT_MS = 30_000;
|
||||
export const GITHUB_API_RESPONSE_BODY_MAX_BYTES = 1024 * 1024;
|
||||
|
||||
export const CLAWSWEEPER_PROOF_VERDICT_STATUS = "clawsweeper_exact_head_pass";
|
||||
const CLAWSWEEPER_BOT_LOGINS = new Set(["clawsweeper[bot]", "openclaw-clawsweeper[bot]"]);
|
||||
@@ -88,6 +91,12 @@ function createTimeoutError(label, timeoutMs) {
|
||||
return error;
|
||||
}
|
||||
|
||||
function createTooLargeGitHubApiBodyError(label, maxBytes) {
|
||||
const error = new Error(`${label} response body exceeded ${maxBytes} bytes`);
|
||||
error.code = "ETOOBIG";
|
||||
return error;
|
||||
}
|
||||
|
||||
export async function withGitHubApiTimeout(label, timeoutMs, run) {
|
||||
const boundedTimeoutMs = Math.max(1, timeoutMs);
|
||||
const controller = new AbortController();
|
||||
@@ -110,6 +119,19 @@ export async function withGitHubApiTimeout(label, timeoutMs, run) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function readBoundedGitHubApiJson(
|
||||
response,
|
||||
label,
|
||||
maxBytes = GITHUB_API_RESPONSE_BODY_MAX_BYTES,
|
||||
options = {},
|
||||
) {
|
||||
const text = await readBoundedResponseText(response, label, maxBytes, {
|
||||
...options,
|
||||
createTooLargeError: () => createTooLargeGitHubApiBodyError(label, maxBytes),
|
||||
});
|
||||
return JSON.parse(text);
|
||||
}
|
||||
|
||||
function normalizeLineEndings(text = "") {
|
||||
return text.replace(/\r\n?/g, "\n");
|
||||
}
|
||||
@@ -178,7 +200,10 @@ export async function isMaintainerTeamMember({
|
||||
const body = await withGitHubApiTimeout(
|
||||
`maintainer membership response for ${login}`,
|
||||
timeoutMs,
|
||||
() => response.json(),
|
||||
(signal) =>
|
||||
readBoundedGitHubApiJson(response, `maintainer membership response for ${login}`, undefined, {
|
||||
signal,
|
||||
}),
|
||||
);
|
||||
return body?.state === "active";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user