fix: fetch all pull files

This commit is contained in:
EnixCoda 2020-07-11 15:37:39 +08:00
parent 968a030941
commit 32ce2235a9
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
3 changed files with 48 additions and 4 deletions

View file

@ -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<GitHubAPI.PullData> {
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<GitHubAPI.PullTreeData> {
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 })
}

View file

@ -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[]

View file

@ -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)