Streamline github error handling

This commit is contained in:
Sam Becker 2025-02-15 17:49:02 -06:00
parent 13b8cdcf62
commit 7027e85530
4 changed files with 32 additions and 66 deletions

View File

@ -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();
});
});

View File

@ -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,

View File

@ -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<ReturnType<typeof getGitHubMetaWithFallback>>
codeMeta?: Awaited<ReturnType<typeof getGitHubMeta>>
insights: AdminAppInsights
photoStats: PhotoStats
debug?: boolean

View File

@ -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<RepoParams> => {
};
};
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,
};
});