diff --git a/src/platforms/GitHub/API.ts b/src/platforms/GitHub/API.ts index 29f97cf..e9df774 100644 --- a/src/platforms/GitHub/API.ts +++ b/src/platforms/GitHub/API.ts @@ -96,7 +96,7 @@ export async function getTreeData( return await request(url, { accessToken }) } -export async function getPullData( +export async function getPullRequest( userName: string, repoName: string, pullId: string, diff --git a/src/platforms/GitHub/DOMHelper.ts b/src/platforms/GitHub/DOMHelper.ts index adb2a81..9cf94a3 100644 --- a/src/platforms/GitHub/DOMHelper.ts +++ b/src/platforms/GitHub/DOMHelper.ts @@ -1,7 +1,7 @@ import { raiseError } from 'analytics' import { Clippy, ClippyClassName } from 'components/Clippy' import * as React from 'react' -import { $, formatClass } from 'utils/DOMHelper' +import { $, formatClass, parseIntFromElement } from 'utils/DOMHelper' import { renderReact, run } from 'utils/general' import { CopyFileButton, copyFileButtonClassName } from './CopyFileButton' @@ -32,7 +32,7 @@ export function isInCodePage() { } export function isInPullFilesPage() { - return $('#files_tab_counter') + return $('.tabnav-tab.selected #files_tab_counter') } export function getIssueTitle() { @@ -277,3 +277,17 @@ export function isNativePRFileTreeShown() { export function selectEnterpriseStatHeader() { return $('.stats-ui-enabled .server-stats') } + +export function getPullRequestFilesCount() { + return $('#files_tab_counter', parseIntFromElement) +} + +export function getPRDiffTotalStat() { + const [added, removed] = [$('#diffstat .color-fg-success'), $('#diffstat .color-fg-danger')].map( + e => (e ? parseIntFromElement(e) : null), + ) + return { + added, + removed, + } +} diff --git a/src/platforms/GitHub/getPullRequestTreeData.ts b/src/platforms/GitHub/getPullRequestTreeData.ts index 5c3fe47..de71c98 100644 --- a/src/platforms/GitHub/getPullRequestTreeData.ts +++ b/src/platforms/GitHub/getPullRequestTreeData.ts @@ -1,24 +1,42 @@ import { formatHash } from 'utils/general' import * as API from './API' -import { isInPullFilesPage } from './DOMHelper' +import { getPRDiffTotalStat, getPullRequestFilesCount, isInPullFilesPage } from './DOMHelper' import { processTree } from './index' import { getCommentsMap } from './utils' +function checkShouldSafeGet() { + const FAST_GET_DIFF_THRESHOLD = 10000 + const FAST_GET_FILES_THRESHOLD = 200 + const { added, removed } = getPRDiffTotalStat() + const filesCount = getPullRequestFilesCount() + return ( + added === null || + removed === null || + filesCount === null || + (added + removed < FAST_GET_DIFF_THRESHOLD && filesCount < FAST_GET_FILES_THRESHOLD) + ) +} + export async function getPullRequestTreeData( metaData: Pick, pullId: string, accessToken?: string, - useSafeRequest = true, + shouldSafeGet = checkShouldSafeGet(), ) { const { userName, repoName } = metaData const [treeData, commentData] = await Promise.all([ - useSafeRequest + shouldSafeGet ? safeGetPullRequestTreeData(metaData, pullId, accessToken) : fastGetPullRequestTreeData(metaData, pullId, accessToken), API.getPullComments(userName, repoName, pullId, accessToken), ]) - const docs = await API.getPullPageDocuments(userName, repoName, pullId, isInPullFilesPage() ? document : undefined) + const docs = await API.getPullPageDocuments( + userName, + repoName, + pullId, + isInPullFilesPage() ? document : undefined, + ) // query all elements at once to make getFileElementHash run faster const elementsHavePath = docs.map(doc => doc.querySelectorAll(`[data-path]`)) const getFileElementHash = (path: string) => { @@ -87,20 +105,17 @@ async function fastGetPullRequestTreeData( pullId: string, accessToken?: string, ) { - const [pullData, treeData] = await Promise.all([ - API.getPullData(userName, repoName, pullId, accessToken), - API.getPullTreeData( - userName, - repoName, - pullId, - 1, - GITHUB_API_RESPONSE_MAX_SIZE_PER_PAGE, - accessToken, - ), - ]) + const treeData = await API.getPullTreeData( + userName, + repoName, + pullId, + 1, + GITHUB_API_RESPONSE_MAX_SIZE_PER_PAGE, + accessToken, + ) - const count = pullData.changed_files - if (treeData.length < count) { + const count = getPullRequestFilesCount() + if (count !== null && treeData.length < count) { let page = 1 const restPages = [] while (page * GITHUB_API_RESPONSE_MAX_SIZE_PER_PAGE < count) { diff --git a/src/utils/DOMHelper.ts b/src/utils/DOMHelper.ts index be14b24..cd8383e 100644 --- a/src/utils/DOMHelper.ts +++ b/src/utils/DOMHelper.ts @@ -154,3 +154,7 @@ export function formatID(id: string) { export function formatClass(className: string) { return `.${className}` } + +export function parseIntFromElement(e: HTMLElement): number { + return parseInt((e.innerText || '').replace(/[^0-9]/g, '')) +}