Skip to content

Commit 9df70fe

Browse files
committed
refactor(compare): return null for missing or malformed GitHub metrics
1 parent 3e4fccd commit 9df70fe

3 files changed

Lines changed: 43 additions & 5 deletions

File tree

app/composables/usePackageComparison.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ export function usePackageComparison(packageNames: MaybeRefOrGetter<string[]>) {
144144
? $fetch<{ repo: { stars: number } }>(
145145
`https://ungh.cc/repos/${repoInfo.owner}/${repoInfo.repo}`,
146146
)
147-
.then(res => res?.repo.stars || 0)
147+
.then(res => (typeof res?.repo?.stars === 'number' ? res.repo.stars : null))
148148
.catch(() => null)
149149
: Promise.resolve(null),
150150
isGitHub
151-
? $fetch<{ issues: number }>(
151+
? $fetch<{ issues: number | null }>(
152152
`/api/github/issues/${repoInfo.owner}/${repoInfo.repo}`,
153153
)
154-
.then(res => res?.issues || 0)
154+
.then(res => (typeof res?.issues === 'number' ? res.issues : null))
155155
.catch(() => null)
156156
: Promise.resolve(null),
157157
])

server/api/github/issues/[owner]/[repo].get.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface GitHubSearchResponse {
1414
export interface GithubIssueCountResponse {
1515
owner: string
1616
repo: string
17-
issues: number
17+
issues: number | null
1818
}
1919

2020
export default defineCachedEventHandler(
@@ -46,7 +46,8 @@ export default defineCachedEventHandler(
4646
return {
4747
owner,
4848
repo,
49-
issues: response._data?.total_count ?? 0,
49+
issues:
50+
typeof response._data?.total_count === 'number' ? response._data.total_count : null,
5051
}
5152
}
5253

test/nuxt/composables/use-package-comparison.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,43 @@ describe('usePackageComparison', () => {
232232
expect(issues).toMatchObject({ raw: 50, status: 'neutral' })
233233
})
234234

235+
it('returns null for missing or non-numeric github metrics', async () => {
236+
const pkgName = 'missing-metrics-pkg'
237+
vi.stubGlobal(
238+
'$fetch',
239+
vi.fn().mockImplementation((url: string, options?: { baseURL?: string }) => {
240+
const fullUrl = options?.baseURL ? `${options.baseURL}${url}` : url
241+
if (fullUrl.startsWith('https://registry.npmjs.org/')) {
242+
return Promise.resolve({
243+
'name': pkgName,
244+
'dist-tags': { latest: '1.0.0' },
245+
'repository': { type: 'git', url: 'https://github.com/owner/repo' },
246+
'versions': {
247+
'1.0.0': { dist: { unpackedSize: 1000 } },
248+
},
249+
})
250+
}
251+
if (fullUrl.includes('ungh.cc/repos/owner/repo')) {
252+
// Return malformed data (stars missing)
253+
return Promise.resolve({ repo: {} })
254+
}
255+
if (fullUrl.includes('/api/github/issues/owner/repo')) {
256+
// Return non-numeric data
257+
return Promise.resolve({ issues: 'not-a-number' })
258+
}
259+
return Promise.resolve(null)
260+
}),
261+
)
262+
263+
const { status, getFacetValues } = await usePackageComparisonInComponent([pkgName])
264+
await vi.waitFor(() => {
265+
expect(status.value).toBe('success')
266+
})
267+
268+
expect(getFacetValues('githubStars')[0]).toBeNull()
269+
expect(getFacetValues('githubIssues')[0]).toBeNull()
270+
})
271+
235272
it('skips github fetches for non-github repositories', async () => {
236273
const pkgName = 'gitlab-pkg'
237274
const fetchMock = vi

0 commit comments

Comments
 (0)