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