diff --git a/__tests__/github.test.ts b/__tests__/github.test.ts index 4244f2be..6aa8a2a0 100644 --- a/__tests__/github.test.ts +++ b/__tests__/github.test.ts @@ -1,50 +1,44 @@ import { - getGitHubMetaWithFallback, + getGitHubMeta, getGitHubPublicFork, } from '@/platforms/github'; import { TEMPLATE_REPO_OWNER, TEMPLATE_REPO_NAME } from '@/app-core/config'; describe('GitHub', () => { it('fetches base repo meta', async () => { - const meta = await getGitHubMetaWithFallback({ + const meta = await getGitHubMeta({ owner: TEMPLATE_REPO_OWNER, repo: TEMPLATE_REPO_NAME, }); expect(meta).toBeDefined(); expect(meta.urlRepo).toBeDefined(); expect(meta.isForkedFromBase).toEqual(false); - expect(meta.label).toBeDefined(); - expect(meta.description).toBeDefined(); expect(meta.isBehind).toEqual(false); expect(meta.isBaseRepo).toBe(true); }); it('fetches fork meta', async () => { const fork = await getGitHubPublicFork(); - const metaFork = await getGitHubMetaWithFallback(fork); + const metaFork = await getGitHubMeta(fork); expect(metaFork.isForkedFromBase).toEqual(true); }); it('handles nonexistent repos', async () => { - const meta = await getGitHubMetaWithFallback({ + const meta = await getGitHubMeta({ owner: 'nonexistent', repo: 'nonexistent', }); expect(meta).toBeDefined(); expect(meta.urlRepo).toBeDefined(); expect(meta.isForkedFromBase).toEqual(false); - expect(meta.label).toEqual('Unknown'); - expect(meta.description).toEqual('Unknown'); expect(meta.isBehind).toBeUndefined(); }); it('handles fetch errors', async () => { - const meta = await getGitHubMetaWithFallback({ + const meta = await getGitHubMeta({ owner: 'gibberish / / *', repo: 'bad text for a url.com', }); expect(meta).toBeDefined(); expect(meta.urlRepo).toBeDefined(); expect(meta.isForkedFromBase).toEqual(false); - expect(meta.label).toEqual('Unknown'); - expect(meta.description).toEqual('Unknown'); expect(meta.isBehind).toBeUndefined(); }); }); diff --git a/src/admin/insights/AdminAppInsights.tsx b/src/admin/insights/AdminAppInsights.tsx index 36fabd54..a5cbb379 100644 --- a/src/admin/insights/AdminAppInsights.tsx +++ b/src/admin/insights/AdminAppInsights.tsx @@ -19,7 +19,7 @@ import { VERCEL_GIT_REPO_OWNER, VERCEL_GIT_REPO_SLUG, } from '@/app-core/config'; -import { getGitHubMetaWithFallback } from '../../platforms/github'; +import { getGitHubMeta } from '../../platforms/github'; import { OUTDATED_THRESHOLD } from '@/photo'; const BASIC_PHOTO_INSTALLATION_COUNT = 32; @@ -50,7 +50,7 @@ export default async function AdminAppInsights() { getUniqueFilmSimulations(), getUniqueLenses(), IS_VERCEL_GIT_PROVIDER_GITHUB || IS_DEVELOPMENT - ? getGitHubMetaWithFallback({ + ? getGitHubMeta({ owner, repo, branch, diff --git a/src/admin/insights/AdminAppInsightsClient.tsx b/src/admin/insights/AdminAppInsightsClient.tsx index 3f7e558c..b9b413da 100644 --- a/src/admin/insights/AdminAppInsightsClient.tsx +++ b/src/admin/insights/AdminAppInsightsClient.tsx @@ -11,7 +11,7 @@ import { HiOutlinePhotograph } from 'react-icons/hi'; import { MdAspectRatio } from 'react-icons/md'; import { PiWarningBold } from 'react-icons/pi'; import { TbCone, TbSparkles } from 'react-icons/tb'; -import { getGitHubMetaWithFallback } from '../../platforms/github'; +import { getGitHubMeta } from '../../platforms/github'; import { BiGitBranch, BiGitCommit, BiLogoGithub } from 'react-icons/bi'; import { TEMPLATE_REPO_BRANCH, @@ -74,7 +74,7 @@ export default function AdminAppInsightsClient({ }, debug, }: { - codeMeta?: Awaited> + codeMeta?: Awaited> insights: AdminAppInsights photoStats: PhotoStats debug?: boolean diff --git a/src/platforms/github.ts b/src/platforms/github.ts index c2ed6d75..a9b78f1b 100644 --- a/src/platforms/github.ts +++ b/src/platforms/github.ts @@ -5,7 +5,6 @@ import { } from '@/app-core/config'; const DEFAULT_BRANCH = 'main'; -const FALLBACK_TEXT = 'Unknown'; const CACHE_GITHUB_REQUESTS = false; // Cache all results for 2 minutes to avoid rate limiting @@ -130,7 +129,7 @@ export const getGitHubPublicFork = async (): Promise => { }; }; -const getGitHubMeta = async (params: RepoParams) => { +export const getGitHubMeta = async (params: RepoParams) => { const urlOwner = getGitHubUrlOwner(params); const urlRepo = getGitHubUrlRepo(params); const urlBranch = getGitHubUrlBranch(params); @@ -138,33 +137,29 @@ const getGitHubMeta = async (params: RepoParams) => { const isBaseRepo = isRepoBaseRepo(params); - const [ - isForkedFromBase, - behindBy, - ] = await Promise.all([ - getIsRepoForkedFromBase(params), - isBaseRepo && params.commit - ? getGitHubCommitsBehindFromCommit(params) - : getGitHubCommitsBehindFromRepo(params), - ]); + let isForkedFromBase: boolean | undefined; + let isBehind: boolean | undefined; + let behindBy: number | undefined; + let didError: boolean = false; - const isBehind = behindBy === undefined - ? undefined - : behindBy > 0; + try { + const results = await Promise.all([ + getIsRepoForkedFromBase(params), + isBaseRepo && params.commit + ? getGitHubCommitsBehindFromCommit(params) + : getGitHubCommitsBehindFromRepo(params), + ]); - const label = isBehind === undefined - ? FALLBACK_TEXT - : isBehind - ? `${behindBy} Behind` - : 'Synced'; - - const description = isBehind === undefined - ? FALLBACK_TEXT - : isBehind - ? `This fork is ${behindBy} commit${behindBy === 1 ? '' : 's'} behind.` - : isBaseRepo - ? 'This build is up to date.' - : 'This fork is up to date.'; + isForkedFromBase = results[0]; + behindBy = results[1]; + + isBehind = behindBy === undefined + ? undefined + : behindBy > 0; + } catch (error) { + didError = true; + console.error('Error retrieving GitHub meta', { params, error }); + } return { ...params, @@ -176,29 +171,6 @@ const getGitHubMeta = async (params: RepoParams) => { isBaseRepo, behindBy, isBehind, - label, - description, - didError: false, + didError, }; }; - -export const getGitHubMetaWithFallback = (params: RepoParams) => - getGitHubMeta(params) - .catch(e => { - console.error('Error retrieving GitHub meta', { params, error: e }); - return { - ...params, - commitMessage: undefined, - urlOwner: undefined, - urlRepo: undefined, - urlBranch: undefined, - urlCommit: undefined, - isForkedFromBase: false, - isBaseRepo: undefined, - behindBy: undefined, - isBehind: undefined, - label: FALLBACK_TEXT, - description: 'Could not connect to GitHub.', - didError: true, - }; - });