fix: fetch complete diff data

This commit is contained in:
EnixCoda 2020-06-27 16:58:07 +08:00
parent 09a97f30ed
commit edf96f2c9a
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
3 changed files with 43 additions and 7 deletions

View file

@ -93,11 +93,14 @@ export async function getPullTreeData(
export async function getPullPageDocument(
userName: string,
repoName: string,
pullId: string,
pullId: string, // not used
baseSHA: string,
headSHA: string,
): Promise<Document> {
const search = new URLSearchParams(window.location.search)
search.set('_pjax', '#js-repo-pjax-container')
const url = `https://${window.location.host}/${userName}/${repoName}/pull/${pullId}/files?${search}`
search.set('sha1', baseSHA)
search.set('sha2', headSHA)
const url = `https://${window.location.host}/${userName}/${repoName}/diffs?${search}`
return new DOMParser().parseFromString(await (await fetch(url)).text(), 'text/html')
}

View file

@ -16,6 +16,29 @@ export function getIssueTitle() {
if (title && id) return `${id} ${title}`
}
export function getPullSHA() {
return $('#js-repo-pjax-container', e => {
const commentNodes: ChildNode[] = []
e.childNodes.forEach(node => {
if (node.nodeType === document.COMMENT_NODE) {
commentNodes.push(node)
}
})
let baseSHA, headSHA
for (const node of commentNodes) {
const matchBase = node.textContent?.match(/base sha1:.*?(\w{40})/)
if (matchBase) baseSHA = matchBase[1]
const matchHead = node.textContent?.match(/head sha1:.*?(\w{40})/)
if (matchHead) headSHA = matchHead[1]
}
if (baseSHA && headSHA)
return {
baseSHA,
headSHA,
}
})
}
export function getCurrentBranch() {
const selectedBranchButtonSelector = [
'.repository-content #branch-select-menu summary',

View file

@ -1,3 +1,4 @@
import { raiseError } from 'analytics'
import { GITHUB_OAUTH } from 'env'
import { Base64 } from 'js-base64'
import { platform } from 'platforms'
@ -194,10 +195,19 @@ export const GitHub: Platform = {
}
async function createPullFileResolver(userName: string, repoName: string, pullId: string) {
const doc =
URLHelper.parse().path[1] === 'files'
? document
: await API.getPullPageDocument(userName, repoName, pullId)
let doc: Document
if (URLHelper.parse().path[1] === 'files') {
doc = document
} else {
const shas = DOMHelper.getPullSHA()
if (!shas) {
raiseError(new Error(`Cannot resolve sha from DOM`))
doc = document
// fallback, at least not throw error
} else {
doc = await API.getPullPageDocument(userName, repoName, pullId, shas.baseSHA, shas.headSHA)
}
}
return (path: string) => {
const id = doc.querySelector(`*[data-path^="${path}"]`)?.parentElement?.id