| 183 | } |
| 184 | |
| 185 | async function fetchOpenPullRequests() { |
| 186 | const prs: PullRequest[] = [] |
| 187 | let endCursor: string | null = null |
| 188 | |
| 189 | while (true) { |
| 190 | const page = await graphql({ |
| 191 | query: `query($owner: String!, $name: String!, $endCursor: String) { |
| 192 | rateLimit { |
| 193 | cost |
| 194 | remaining |
| 195 | resetAt |
| 196 | } |
| 197 | repository(owner: $owner, name: $name) { |
| 198 | pullRequests(first: 100, states: OPEN, orderBy: { field: CREATED_AT, direction: ASC }, after: $endCursor) { |
| 199 | pageInfo { |
| 200 | hasNextPage |
| 201 | endCursor |
| 202 | } |
| 203 | nodes { |
| 204 | number |
| 205 | title |
| 206 | url |
| 207 | createdAt |
| 208 | reactionGroups { |
| 209 | content |
| 210 | users { |
| 211 | totalCount |
| 212 | } |
| 213 | } |
| 214 | labels(first: 100) { |
| 215 | nodes { |
| 216 | name |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | }`, |
| 223 | variables: { |
| 224 | owner: repo.owner, |
| 225 | name: repo.name, |
| 226 | endCursor, |
| 227 | }, |
| 228 | }) |
| 229 | |
| 230 | prs.push(...page.repository.pullRequests.nodes) |
| 231 | console.log( |
| 232 | `Fetched ${prs.length} PRs, GraphQL rate limit remaining ${page.rateLimit.remaining} (cost ${page.rateLimit.cost})`, |
| 233 | ) |
| 234 | |
| 235 | if (page.rateLimit.remaining < 100) { |
| 236 | const delay = Math.max(0, new Date(page.rateLimit.resetAt).getTime() - Date.now()) + 1_000 |
| 237 | console.warn(`GraphQL rate limit low; sleeping ${Math.ceil(delay / 1000)}s until reset`) |
| 238 | await sleep(delay) |
| 239 | } |
| 240 | |
| 241 | if (!page.repository.pullRequests.pageInfo.hasNextPage) return prs |
| 242 | endCursor = page.repository.pullRequests.pageInfo.endCursor |