From 32ce2235a92fdb0c2f86737b172d286af2e93785 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Sat, 11 Jul 2020 15:37:39 +0800 Subject: [PATCH] fix: fetch all pull files --- src/platforms/GitHub/API.ts | 16 ++++++++++++++-- src/platforms/GitHub/Request.d.ts | 9 ++++++++- src/platforms/GitHub/index.ts | 27 ++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/platforms/GitHub/API.ts b/src/platforms/GitHub/API.ts index 518a568..d28db7a 100644 --- a/src/platforms/GitHub/API.ts +++ b/src/platforms/GitHub/API.ts @@ -80,13 +80,25 @@ export async function getTreeData( return await request(url, { accessToken }) } -export async function getPullTreeData( +export async function getPullData( userName: string, repoName: string, pullId: string, accessToken?: string, +): Promise { + const url = `https://${API_ENDPOINT}/repos/${userName}/${repoName}/pulls/${pullId}` + return await request(url, { accessToken }) +} + +export async function getPullTreeData( + userName: string, + repoName: string, + pullId: string, + page: number, + accessToken?: string, ): Promise { - const url = `https://${API_ENDPOINT}/repos/${userName}/${repoName}/pulls/${pullId}/files` + const search = new URLSearchParams({ page: page.toString() }) + const url = `https://${API_ENDPOINT}/repos/${userName}/${repoName}/pulls/${pullId}/files?${search}` return await request(url, { accessToken }) } diff --git a/src/platforms/GitHub/Request.d.ts b/src/platforms/GitHub/Request.d.ts index 03c84e8..a010a1c 100644 --- a/src/platforms/GitHub/Request.d.ts +++ b/src/platforms/GitHub/Request.d.ts @@ -25,7 +25,14 @@ declare namespace GitHubAPI { patch: string raw_url: string sha: string - status: 'modified' | 'added' | 'removed' + status: 'modified' | 'added' | 'removed' | 'renamed' + } + + type PullData = { + state: 'open' | 'closed' + title: string + body: string + changed_files: number } type PullTreeData = PullTreeItem[] diff --git a/src/platforms/GitHub/index.ts b/src/platforms/GitHub/index.ts index f9f3b96..57a770e 100644 --- a/src/platforms/GitHub/index.ts +++ b/src/platforms/GitHub/index.ts @@ -102,7 +102,32 @@ export const GitHub: Platform = { const pullId = URLHelper.isInPullPage() if (pullId) { - const treeData = await API.getPullTreeData(userName, repoName, pullId, accessToken) + // https://developer.github.com/v3/pulls/#list-pull-requests-files + const GITHUB_API_RESPONSE_LENGTH_LIMIT = 3000 + const GITHUB_API_PAGED_RESPONSE_LENGTH_LIMIT = 30 + const MAX_PAGE = Math.ceil( + GITHUB_API_RESPONSE_LENGTH_LIMIT / GITHUB_API_PAGED_RESPONSE_LENGTH_LIMIT, + ) + let page = 1 + const [pullData, treeData] = await Promise.all([ + API.getPullData(userName, repoName, pullId, accessToken), + API.getPullTreeData(userName, repoName, pullId, page, accessToken), + ]) + + const count = pullData.changed_files + if (treeData.length < count) { + const restPages = [] + while (page * GITHUB_API_PAGED_RESPONSE_LENGTH_LIMIT < count) { + restPages.push(++page) + } + if (page > MAX_PAGE) { + // TODO: hint + } + const moreFiles = await Promise.all( + restPages.map(page => API.getPullTreeData(userName, repoName, pullId, page, accessToken)), + ) + treeData.push(...([] as GitHubAPI.PullTreeData).concat(...moreFiles)) + } const creator = await createPullFileResolver(userName, repoName, pullId)