mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
fix: fetch all pull files
This commit is contained in:
parent
968a030941
commit
32ce2235a9
3 changed files with 48 additions and 4 deletions
|
|
@ -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 })
|
||||
}
|
||||
|
||||
|
|
|
|||
9
src/platforms/GitHub/Request.d.ts
vendored
9
src/platforms/GitHub/Request.d.ts
vendored
|
|
@ -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[]
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue