MCPcopy Create free account
hub / github.com/alibaba/open-code-review / getPrDiffHunks

Function getPrDiffHunks

scripts/github-actions/post-review-comments.js:2041–2127  ·  view source on GitHub ↗
({ github, owner, repo, prNumber, commitSha, log, cache })

Source from the content-addressed store, hash-verified

2039// `cache` (optional) memoizes the result for the run: with several batches each
2040// failing 422, the inventory would otherwise be refetched per batch.
2041async function getPrDiffHunks({ github, owner, repo, prNumber, commitSha, log, cache }) {
2042 const logFn = typeof log === "function" ? log : () => {};
2043 if (cache && cache.diff !== undefined) return cache.diff;
2044
2045 const files = new Map();
2046 const known = new Set();
2047 let complete = true;
2048
2049 const PER_PAGE = 100;
2050 const MAX_PAGES = 30;
2051 let page = 1;
2052 while (page <= MAX_PAGES) {
2053 const res = await readWithPacing(
2054 `listFiles (page ${page})`,
2055 () => github.rest.pulls.listFiles({ owner, repo, pull_number: prNumber, per_page: PER_PAGE, page }),
2056 logFn
2057 );
2058 const batch = (res && res.data) || [];
2059 for (const file of batch) {
2060 if (!file || !file.filename) continue;
2061 known.add(file.filename);
2062 if (file.patch) {
2063 const parsed = parseDiffHunkInventory(file.patch);
2064 if (parsed.complete) {
2065 files.set(file.filename, parsed.ranges);
2066 } else {
2067 logFn(
2068 `[422-fallback] Patch data for ${file.filename} is incomplete or malformed; ` +
2069 `comments on that file will be treated as unknown rather than out-of-diff.`
2070 );
2071 }
2072 }
2073 }
2074 if (batch.length < PER_PAGE) break;
2075 page++;
2076 }
2077 if (page > MAX_PAGES) {
2078 complete = false;
2079 logFn(
2080 `[422-fallback] PR changed-file list exceeded ${MAX_PAGES * PER_PAGE} files; ` +
2081 `diff inventory is incomplete, so no comment will be discarded as out-of-diff.`
2082 );
2083 }
2084
2085 // An empty inventory proves nothing. A PR that produced review comments
2086 // necessarily has changed files, so an empty listFiles response is an anomaly
2087 // (diff not yet materialized server-side, or a malformed/empty response body)
2088 // rather than evidence that every commented path sits outside the diff.
2089 // Trusting it would classify EVERY comment "invalid" and discard the whole
2090 // batch without a single posting attempt — the exact outcome the tri-state
2091 // classification exists to prevent. Note this is the mirror of the truncation
2092 // case above: too many files and zero files are both "cannot judge".
2093 if (known.size === 0) {
2094 complete = false;
2095 logFn(
2096 `[422-fallback] PR changed-file list came back empty, which cannot be right for a PR ` +
2097 `under review; treating the diff inventory as incomplete, so no comment will be ` +
2098 `discarded as out-of-diff.`

Calls 4

readWithPacingFunction · 0.85
parseDiffHunkInventoryFunction · 0.85
listFilesMethod · 0.80
setMethod · 0.80