From edf96f2c9ab872e2d4aa84c06564f136b0086018 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Sat, 27 Jun 2020 16:58:07 +0800 Subject: [PATCH] fix: fetch complete diff data --- src/platforms/GitHub/API.ts | 9 ++++++--- src/platforms/GitHub/DOMHelper.ts | 23 +++++++++++++++++++++++ src/platforms/GitHub/index.ts | 18 ++++++++++++++---- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/platforms/GitHub/API.ts b/src/platforms/GitHub/API.ts index 6b7cbf7..518a568 100644 --- a/src/platforms/GitHub/API.ts +++ b/src/platforms/GitHub/API.ts @@ -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 { 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') } diff --git a/src/platforms/GitHub/DOMHelper.ts b/src/platforms/GitHub/DOMHelper.ts index 0d141f6..6919671 100644 --- a/src/platforms/GitHub/DOMHelper.ts +++ b/src/platforms/GitHub/DOMHelper.ts @@ -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', diff --git a/src/platforms/GitHub/index.ts b/src/platforms/GitHub/index.ts index c0b33e4..9a6ce27 100644 --- a/src/platforms/GitHub/index.ts +++ b/src/platforms/GitHub/index.ts @@ -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