diff --git a/src/platforms/GitHub/API.ts b/src/platforms/GitHub/API.ts index 01e3fd4..d0a6657 100644 --- a/src/platforms/GitHub/API.ts +++ b/src/platforms/GitHub/API.ts @@ -110,14 +110,26 @@ export async function getPullPageDocument( userName: string, repoName: string, pullId: string, // not used - baseSHA: string, - headSHA: string, ): 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`, + ) + 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( diff --git a/src/platforms/GitHub/DOMHelper.ts b/src/platforms/GitHub/DOMHelper.ts index 1c5c014..3480871 100644 --- a/src/platforms/GitHub/DOMHelper.ts +++ b/src/platforms/GitHub/DOMHelper.ts @@ -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', diff --git a/src/platforms/GitHub/index.ts b/src/platforms/GitHub/index.ts index a9eda0b..af82cdb 100644 --- a/src/platforms/GitHub/index.ts +++ b/src/platforms/GitHub/index.ts @@ -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) => {