From a05e2c93fffafcee9b696f7adf463885792fc94d Mon Sep 17 00:00:00 2001 From: Ryan Atkinson Date: Fri, 16 Feb 2024 17:25:28 -0800 Subject: [PATCH] fix fetch failures (#5) --- .changeset/witty-singers-mix.md | 5 ++ src/lib/deployments.json | 3 +- src/lib/deployments.ts | 2 +- src/lib/fetch_deployments.ts | 96 ++++++++++++++++++--------------- src/lib/package_meta.ts | 2 - 5 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 .changeset/witty-singers-mix.md diff --git a/.changeset/witty-singers-mix.md b/.changeset/witty-singers-mix.md new file mode 100644 index 000000000..84ca13952 --- /dev/null +++ b/.changeset/witty-singers-mix.md @@ -0,0 +1,5 @@ +--- +"@ryanatkn/fuz": patch +--- + +fix fetch failures diff --git a/src/lib/deployments.json b/src/lib/deployments.json index e891586b4..db6dccaf7 100644 --- a/src/lib/deployments.json +++ b/src/lib/deployments.json @@ -617,8 +617,9 @@ "npm_url": "https://www.npmjs.com/package/@ryanatkn/fuz", "changelog_url": "https://github.com/ryanatkn/fuz/blob/main/CHANGELOG.md", "published": true, - "check_runs": {"status": "in_progress", "conclusion": null}, + "check_runs": {"status": "completed", "conclusion": "success"}, "pull_requests": [ + {"number": 5, "title": "fix fetch failures", "user": {"login": "ryanatkn"}, "draft": false}, {"number": 1, "title": "add colors to buttons", "user": {"login": "ryanatkn"}, "draft": true} ] }, diff --git a/src/lib/deployments.ts b/src/lib/deployments.ts index aa872dea0..3d12e9f39 100644 --- a/src/lib/deployments.ts +++ b/src/lib/deployments.ts @@ -4,7 +4,7 @@ import {parse_package_meta} from '$lib/package_meta.js'; import type {Fetched_Deployment, Deployment, Unfetched_Deployment} from '$lib/fetch_deployments.js'; export interface Deployments { - deployment: Fetched_Deployment; // TODO this type is wrong because it may not be fetched, but should it even be here? + deployment: Fetched_Deployment; deployments: Fetched_Deployment[]; unfetched_deployments: Unfetched_Deployment[]; } diff --git a/src/lib/fetch_deployments.ts b/src/lib/fetch_deployments.ts index 65a3db10c..88c87561e 100644 --- a/src/lib/fetch_deployments.ts +++ b/src/lib/fetch_deployments.ts @@ -12,6 +12,7 @@ import { fetch_github_check_runs, fetch_github_pull_requests, Github_Check_Runs_Item, + Github_Pull_Requests, type Github_Pull_Request, } from '$lib/github.js'; @@ -32,7 +33,7 @@ export interface Unfetched_Deployment { /* eslint-disable no-await-in-loop */ -// TODO probably refactor to an object API +// TODO this is all very hacky export const fetch_deployments = async ( homepage_urls: Url[], token?: string, @@ -57,39 +58,51 @@ export const fetch_deployments = async ( const deployments: Deployment[] = []; for (const raw_homepage_url of homepage_urls) { const homepage_url = ensure_end(raw_homepage_url, '/'); - try { - let package_json: Package_Json; - let src_json: Src_Json; - - // Handle the local package data, if available - if (homepage_url === local_homepage_url) { - log?.info('resolving data locally for', homepage_url); - package_json = local_package_json; - src_json = await create_src_json( - local_package_json, - log, - dir ? join(dir, 'src/lib') : undefined, - ); - } else { - log?.info('fetching data for', homepage_url); - - // `${base}/.well-known/package.json` - const fetched_package_json = await fetch_package_json(homepage_url, cache, log); - if (!fetched_package_json) throw Error('failed to load package_json: ' + homepage_url); - package_json = fetched_package_json; - await wait(delay); - - // `${base}/.well-known/src.json` - const fetched_src_json = await fetch_src_json(homepage_url, cache, log); - if (!fetched_src_json) throw Error('failed to load src_json: ' + homepage_url); - src_json = fetched_src_json; - await wait(delay); - } + let package_json: Package_Json | null; + let src_json: Src_Json | null; + let pkg: Package_Meta | null; + let check_runs: Github_Check_Runs_Item | null; + let pull_requests: Github_Pull_Requests | null; + + // Handle the local package data, if available + if (homepage_url === local_homepage_url) { + log?.info('resolving data locally for', homepage_url); + package_json = local_package_json; + + src_json = await create_src_json( + local_package_json, + log, + dir ? join(dir, 'src/lib') : undefined, + ); + if (!src_json) log?.error('failed to fetch src_json: ' + homepage_url); + } else { + // Fetch the remote package data + log?.info('fetching data for', homepage_url); + + await wait(delay); + package_json = await fetch_package_json(homepage_url, cache, log); + if (!package_json) log?.error('failed to load package_json: ' + homepage_url); - const pkg = parse_package_meta(homepage_url, package_json, src_json); + await wait(delay); + src_json = await fetch_src_json(homepage_url, cache, log); + if (!src_json) log?.error('failed to load src_json: ' + homepage_url); + } + + if (package_json && src_json) { + try { + pkg = parse_package_meta(homepage_url, package_json, src_json); + } catch (err) { + pkg = null; + log?.error('failed to parse package meta: ' + err); + } + } else { + pkg = null; + } + if (pkg) { // CI status - const check_runs = await fetch_github_check_runs( + await wait(delay); + check_runs = await fetch_github_check_runs( pkg, cache, log, @@ -97,22 +110,20 @@ export const fetch_deployments = async ( github_api_version, github_refs?.[raw_homepage_url], ); - if (!check_runs) throw Error('failed to fetch CI status: ' + homepage_url); - await wait(delay); + if (!check_runs) log?.error('failed to fetch CI status: ' + homepage_url); // pull requests - const pull_requests = await fetch_github_pull_requests( - pkg, - cache, - log, - token, - github_api_version, - ); - if (!pull_requests) throw Error('failed to fetch issues: ' + homepage_url); await wait(delay); + pull_requests = await fetch_github_pull_requests(pkg, cache, log, token, github_api_version); + if (!pull_requests) log?.error('failed to fetch issues: ' + homepage_url); + } else { + check_runs = null; + pull_requests = null; + } + if (pkg) { deployments.push({...pkg, check_runs, pull_requests}); - } catch (err) { + } else { deployments.push({ url: homepage_url, package_json: null, @@ -120,7 +131,6 @@ export const fetch_deployments = async ( check_runs: null, pull_requests: null, }); - log?.error(err); } } return deployments; diff --git a/src/lib/package_meta.ts b/src/lib/package_meta.ts index bbc16caf2..727b9621e 100644 --- a/src/lib/package_meta.ts +++ b/src/lib/package_meta.ts @@ -31,8 +31,6 @@ export const parse_package_meta = ( ): Package_Meta => { const {name} = package_json; - // TODO think through with other presentations - Details, Summary, Card - // TODO hacky const parse_repo = (r: string | null | undefined) => { if (!r) return null;