fix: resolve sha from partially loaded DOM can be unreliable

This commit is contained in:
EnixCoda 2020-11-13 16:55:56 +08:00
parent e6cba2c7e6
commit f22f270523
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
2 changed files with 5 additions and 8 deletions

View file

@ -115,9 +115,11 @@ export async function getPullPageDocument(
const filesDOM = await getDOM(
`https://${window.location.host}/${userName}/${repoName}/pull/${pullId}/files?_pjax=%23js-repo-pjax-container`,
)
const hookElement: HTMLDivElement | null = filesDOM.querySelector('div.js-pull-refresh-on-pjax')
const hookSearchParams = new URLSearchParams(hookElement?.dataset.url)
const [baseSHA, headSHA] = [
filesDOM.querySelector('input[name="comparison_start_oid"]')?.getAttribute('value'),
filesDOM.querySelector('input[name="comparison_end_oid"]')?.getAttribute('value'),
hookSearchParams.get('start_commit_oid'),
hookSearchParams.get('end_commit_oid'),
]
if (!baseSHA || !headSHA) throw new Error(`Cannot fetch SHA for comparison`)

View file

@ -249,12 +249,7 @@ export const GitHub: Platform = {
}
async function createPullFileResolver(userName: string, repoName: string, pullId: string) {
let doc: Document
if (URLHelper.parse().path[1] === 'files') {
doc = document
} else {
doc = await API.getPullPageDocument(userName, repoName, pullId)
}
const doc = await API.getPullPageDocument(userName, repoName, pullId)
return (path: string) => {
const id = doc.querySelector(`*[data-path^="${path}"]`)?.parentElement?.id