fix: resolve PR files' SHA more careful

This commit is contained in:
EnixCoda 2020-11-13 00:55:55 +08:00
parent 72ebbb2047
commit 57b481c7e3
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
3 changed files with 17 additions and 36 deletions

View file

@ -110,14 +110,26 @@ export async function getPullPageDocument(
userName: string,
repoName: string,
pullId: string, // not used
baseSHA: string,
headSHA: string,
): 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`,
)
const [baseSHA, headSHA] = [
filesDOM.querySelector('input[name="comparison_start_oid"]')?.getAttribute('value'),
filesDOM.querySelector('input[name="comparison_end_oid"]')?.getAttribute('value'),
]
if (!baseSHA || !headSHA) throw new Error(`Cannot fetch SHA for comparison`)
// The SHA used to be retrieved from DOM of the pull page, but they can be unreliable if the PR has conflicts
const search = new URLSearchParams(window.location.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')
return await getDOM(`https://${window.location.host}/${userName}/${repoName}/diffs?${search}`)
async function getDOM(url: string) {
return new DOMParser().parseFromString(await (await fetch(url)).text(), 'text/html')
}
}
export async function getBlobData(

View file

@ -24,29 +24,6 @@ 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,4 +1,3 @@
import { raiseError } from 'analytics'
import { GITHUB_OAUTH } from 'env'
import { Base64 } from 'js-base64'
import { platform } from 'platforms'
@ -254,14 +253,7 @@ async function createPullFileResolver(userName: string, repoName: string, pullId
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)
}
doc = await API.getPullPageDocument(userName, repoName, pullId)
}
return (path: string) => {