diff --git a/src/platforms/GitHub/API.ts b/src/platforms/GitHub/API.ts index 0491ef9..d6c1d7c 100644 --- a/src/platforms/GitHub/API.ts +++ b/src/platforms/GitHub/API.ts @@ -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 { +): Promise { // 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( diff --git a/src/platforms/GitHub/index.ts b/src/platforms/GitHub/index.ts index adc8c29..e2455fb 100644 --- a/src/platforms/GitHub/index.ts +++ b/src/platforms/GitHub/index.ts @@ -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 '' }