fix: resolve incomplete diffs DOM

This commit is contained in:
EnixCoda 2021-07-28 01:07:27 +08:00
parent 9f57bc52d0
commit d96be044c7
2 changed files with 31 additions and 14 deletions

View file

@ -116,11 +116,11 @@ export async function getPullComments(
return await request(url, { accessToken })
}
export async function getPullPageDocument(
export async function getPullPageDocuments(
userName: string,
repoName: string,
pullId: string, // not used
): Promise<Document> {
): Promise<Document[]> {
// Response of this API contains view of few files but is not complete.
const filesDOM = await getDOM(
`https://${window.location.host}/${userName}/${repoName}/pull/${pullId}/files?_pjax=%23js-repo-pjax-container`,
@ -137,11 +137,26 @@ export async function getPullPageDocument(
const search = new URLSearchParams(window.location.search)
search.set('sha1', baseSHA)
search.set('sha2', headSHA)
return await getDOM(`https://${window.location.host}/${userName}/${repoName}/diffs?${search}`)
let lines = 0
const diffsDOMs: Document[] = []
while (true) {
search.set('lines', lines.toString())
const diffsDOM = await getDOM(
`https://${window.location.host}/${userName}/${repoName}/diffs?${search}`,
)
diffsDOMs.push(diffsDOM)
async function getDOM(url: string) {
return new DOMParser().parseFromString(await (await fetch(url)).text(), 'text/html')
if (diffsDOM.querySelector('.js-diff-progressive-container')) {
lines += 3000
} else {
break
}
}
return diffsDOMs
}
async function getDOM(url: string) {
return new DOMParser().parseFromString(await (await fetch(url)).text(), 'text/html')
}
export async function getBlobData(

View file

@ -281,7 +281,13 @@ async function getPullRequestTreeData(
treeData.push(...([] as GitHubAPI.PullTreeData).concat(...moreFiles))
}
const creator = await createPullFileResolver(userName, repoName, pullId)
const docs = await API.getPullPageDocuments(userName, repoName, pullId)
const getFileElementHash = (path: string) => {
for (const doc of docs) {
const id = doc.querySelector(`*[data-path^="${path}"]`)?.parentElement?.id
if (id) return id
}
}
const nodes: TreeNode[] = treeData.map(item => ({
path: item.filename || '',
@ -289,7 +295,7 @@ async function getPullRequestTreeData(
name: item.filename?.replace(/^.*\//, '') || '',
url: `https://${window.location.host}/${userName}/${repoName}/pull/${pullId}/files${
window.location.search
}#${creator(item.filename) || ''}`,
}${formatHash(getFileElementHash(item.filename))}`,
sha: item.sha,
comments: commentData?.filter(comment => item.filename === comment.path).length,
}))
@ -298,11 +304,7 @@ async function getPullRequestTreeData(
return { root }
}
async function createPullFileResolver(userName: string, repoName: string, pullId: string) {
const doc = await API.getPullPageDocument(userName, repoName, pullId)
return (path: string) => {
const id = doc.querySelector(`*[data-path^="${path}"]`)?.parentElement?.id
return id
}
function formatHash(hash?: string) {
if (hash) return '#' + hash
return ''
}