Skip to content

Commit b91aede

Browse files
Copilotsam-byng
andcommitted
Add pagination support to handle more than 100 PRs
Co-authored-by: sam-byng <43856946+sam-byng@users.noreply.github.com>
1 parent 230973f commit b91aede

2 files changed

Lines changed: 53 additions & 21 deletions

File tree

.github/workflows/close-prs.yml

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,38 @@ jobs:
3737
console.log(`Dry run mode: ${dryRun}`);
3838
console.log('Fetching open pull requests...');
3939
40-
// Get all open PRs
41-
const { data: pullRequests } = await github.rest.pulls.list({
42-
owner: context.repo.owner,
43-
repo: context.repo.repo,
44-
state: 'open',
45-
per_page: 100
46-
});
40+
// Get all open PRs (handle pagination)
41+
const allPullRequests = [];
42+
let page = 1;
43+
let hasMore = true;
4744
48-
if (pullRequests.length === 0) {
45+
while (hasMore) {
46+
const { data: pullRequests } = await github.rest.pulls.list({
47+
owner: context.repo.owner,
48+
repo: context.repo.repo,
49+
state: 'open',
50+
per_page: 100,
51+
page: page
52+
});
53+
54+
allPullRequests.push(...pullRequests);
55+
56+
// If we got less than 100 PRs, we've reached the end
57+
if (pullRequests.length < 100) {
58+
hasMore = false;
59+
} else {
60+
page++;
61+
}
62+
}
63+
64+
if (allPullRequests.length === 0) {
4965
console.log('No open pull requests found.');
5066
return;
5167
}
5268
53-
console.log(`Found ${pullRequests.length} open pull request(s):`);
69+
console.log(`Found ${allPullRequests.length} open pull request(s):`);
5470
55-
for (const pr of pullRequests) {
71+
for (const pr of allPullRequests) {
5672
console.log(`\nPR #${pr.number}: ${pr.title}`);
5773
console.log(` Author: ${pr.user.login}`);
5874
console.log(` Created: ${pr.created_at}`);

close-prs.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,37 @@ function makeRequest(options, data = null) {
6161
}
6262

6363
/**
64-
* Get all open pull requests
64+
* Get all open pull requests (handles pagination)
6565
*/
6666
async function getOpenPRs() {
67-
const options = {
68-
hostname: 'api.github.com',
69-
path: `/repos/${OWNER}/${REPO}/pulls?state=open&per_page=100`,
70-
method: 'GET',
71-
headers: {
72-
'User-Agent': 'close-prs-script',
73-
'Authorization': `Bearer ${GITHUB_TOKEN}`,
74-
'Accept': 'application/vnd.github.v3+json'
67+
let allPRs = [];
68+
let page = 1;
69+
let hasMore = true;
70+
71+
while (hasMore) {
72+
const options = {
73+
hostname: 'api.github.com',
74+
path: `/repos/${OWNER}/${REPO}/pulls?state=open&per_page=100&page=${page}`,
75+
method: 'GET',
76+
headers: {
77+
'User-Agent': 'close-prs-script',
78+
'Authorization': `Bearer ${GITHUB_TOKEN}`,
79+
'Accept': 'application/vnd.github.v3+json'
80+
}
81+
};
82+
83+
const prs = await makeRequest(options);
84+
allPRs = allPRs.concat(prs);
85+
86+
// If we got less than 100 PRs, we've reached the end
87+
if (prs.length < 100) {
88+
hasMore = false;
89+
} else {
90+
page++;
7591
}
76-
};
92+
}
7793

78-
return makeRequest(options);
94+
return allPRs;
7995
}
8096

8197
/**

0 commit comments

Comments
 (0)