Skip to content

Commit 4698b88

Browse files
authored
PR Approvals Workflow: Fetch Multiple Pages (#223)
The `check_approvals` workflow in #107 fails because by default Github [only returns the first 30 results](https://docs.github.com/en/rest/using-the-rest-api/using-pagination-in-the-rest-api?apiVersion=2022-11-28#about-pagination) for GET requests to their API. #107 has more review activity than that, so the approvals are all on the second or third pages, hence why the workflow says zero approvals. So, this PR updates the script to fetch 100 responses per page, up to 100 pages. These are arbitrary values, but I didn't want to have any infinite loops, and I can't imagine a realistic PR ever going above these numbers (and I put a warning in the script if they do). I checked that this new workflow runs without error [on my fork](https://github.com/carolynzech/verify-rust-std/actions/runs/12267930897/job/34228889979?pr=20). I also did some experimentation in my local bash with the branch from #107 and verified that upping the pagination limits like this does indeed return the approvals on the later pages. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
1 parent 367d8ff commit 4698b88

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

.github/workflows/pr_approval.yml

+33-7
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,42 @@ jobs:
7373
return;
7474
}
7575
76-
// Get all reviews
77-
const reviews = await github.rest.pulls.listReviews({
78-
owner,
79-
repo,
80-
pull_number
81-
});
76+
// Get all reviews with pagination
77+
async function getAllReviews() {
78+
let allReviews = [];
79+
let page = 1;
80+
let page_limit = 100;
81+
82+
while (page < page_limit) {
83+
const response = await github.rest.pulls.listReviews({
84+
owner,
85+
repo,
86+
pull_number,
87+
per_page: 100,
88+
page
89+
});
90+
91+
allReviews = allReviews.concat(response.data);
92+
93+
if (response.data.length < 100) {
94+
break;
95+
}
96+
97+
page++;
98+
}
99+
100+
if (page == page_limit) {
101+
console.log(`WARNING: Reached page limit of ${page_limit} while fetching reviews data; approvals count may be less than the real total.`)
102+
}
103+
104+
return allReviews;
105+
}
106+
107+
const reviews = await getAllReviews();
82108
83109
// Example: approvers = ["celina", "zyad"]
84110
const approvers = new Set(
85-
reviews.data
111+
reviews
86112
.filter(review => review.state === 'APPROVED')
87113
.map(review => review.user.login)
88114
);

0 commit comments

Comments
 (0)