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

Function getPrDiffHunks

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

Source from the content-addressed store, hash-verified

2898// `cache` (optional) memoizes the result for the run: with several batches each
2899// failing 422, the inventory would otherwise be refetched per batch.
2900async function getPrDiffHunks({ github, owner, repo, prNumber, commitSha, log, cache }) {
2901 const logFn = typeof log === "function" ? log : () => {};
2902 if (cache && cache.diff !== undefined) return cache.diff;
2903
2904 const files = new Map();
2905 const known = new Set();
2906 let complete = true;
2907
2908 const PER_PAGE = 100;
2909 const MAX_PAGES = 30;
2910 let page = 1;
2911 while (page <= MAX_PAGES) {
2912 const res = await readWithPacing(
2913 `listFiles (page ${page})`,
2914 () => github.rest.pulls.listFiles({ owner, repo, pull_number: prNumber, per_page: PER_PAGE, page }),
2915 logFn
2916 );
2917 const batch = (res && res.data) || [];
2918 for (const file of batch) {
2919 if (!file || !file.filename) continue;
2920 known.add(file.filename);
2921 if (file.patch) {
2922 const parsed = parseDiffHunkInventory(file.patch);
2923 if (parsed.complete) {
2924 files.set(file.filename, parsed.ranges);
2925 } else {
2926 logFn(
2927 `[422-fallback] Patch data for ${file.filename} is incomplete or malformed; ` +
2928 `comments on that file will be treated as unknown rather than out-of-diff.`
2929 );
2930 }
2931 }
2932 }
2933 if (batch.length < PER_PAGE) break;
2934 page++;
2935 }
2936 if (page > MAX_PAGES) {
2937 complete = false;
2938 logFn(
2939 `[422-fallback] PR changed-file list exceeded ${MAX_PAGES * PER_PAGE} files; ` +
2940 `diff inventory is incomplete, so no comment will be discarded as out-of-diff.`
2941 );
2942 }
2943
2944 // An empty inventory proves nothing. A PR that produced review comments
2945 // necessarily has changed files, so an empty listFiles response is an anomaly
2946 // (diff not yet materialized server-side, or a malformed/empty response body)
2947 // rather than evidence that every commented path sits outside the diff.
2948 // Trusting it would classify EVERY comment "outside_diff" and discard the
2949 // batch without a single posting attempt — the exact outcome the "unknown"
2950 // classification exists to prevent. Note this is the mirror of the truncation
2951 // case above: too many files and zero files are both "cannot judge".
2952 if (known.size === 0) {
2953 complete = false;
2954 logFn(
2955 `[422-fallback] PR changed-file list came back empty, which cannot be right for a PR ` +
2956 `under review; treating the diff inventory as incomplete, so no comment will be ` +
2957 `discarded as out-of-diff.`

Calls 6

readWithPacingFunction · 0.85
parseDiffHunkInventoryFunction · 0.85
listFilesMethod · 0.80
addMethod · 0.80
setMethod · 0.45
getMethod · 0.45