@@ -3,9 +3,10 @@ import type {
33 ChangelogInfo ,
44 ChangelogReleaseInfo ,
55} from '~~/shared/types/changelog'
6- import { type RepoRef , parseRepoUrl } from '~~/shared/utils/git-providers '
6+ import type { FetchError } from 'ofetch '
77import type { ExtendedPackageJson } from '~~/shared/utils/package-analysis'
8- import { ERROR_CHANGELOG_NOT_FOUND } from '~~/shared/utils/constants'
8+ import { type RepoRef , parseRepoUrl } from '~~/shared/utils/git-providers'
9+ import { ERROR_CHANGELOG_NOT_FOUND , ERROR_PROXY_API_KEY_EXHAUSTED } from '~~/shared/utils/constants'
910import * as v from 'valibot'
1011import { GithubReleaseSchama } from '~~/shared/schemas/changelog/release'
1112import { resolveURL } from 'ufo'
@@ -25,14 +26,23 @@ export async function detectChangelog(pkg: ExtendedPackageJson) {
2526 return false
2627 }
2728
28- const changelog =
29- ( await checkReleases ( repoRef , pkg . repository . directory ) ) ||
30- ( await checkChangelogFile ( repoRef , pkg . repository . directory ) )
29+ const releases = await checkReleases ( repoRef , pkg . repository . directory )
30+ if ( releases ) {
31+ return releases
32+ }
3133
34+ const changelog = await checkChangelogFile ( repoRef , pkg . repository . directory )
3235 if ( changelog ) {
3336 return changelog
3437 }
3538
39+ if ( releases === 0 ) {
40+ throw createError ( {
41+ statusCode : 502 ,
42+ statusMessage : ERROR_PROXY_API_KEY_EXHAUSTED ,
43+ } )
44+ }
45+
3646 throw createError ( {
3747 statusCode : 404 ,
3848 statusMessage : ERROR_CHANGELOG_NOT_FOUND ,
@@ -41,9 +51,9 @@ export async function detectChangelog(pkg: ExtendedPackageJson) {
4151
4252/**
4353 * check whether releases are being used with this repo
44- * @returns true if in use
54+ * @returns true if in use, 0 in case proxy api (ungh.cc) exhausted api keys, false if not in use
4555 */
46- async function checkReleases ( ref : RepoRef , directory ?: string ) : Promise < ChangelogInfo | false > {
56+ async function checkReleases ( ref : RepoRef , directory ?: string ) : Promise < ChangelogInfo | false | 0 > {
4757 switch ( ref . provider ) {
4858 case 'github' : {
4959 return checkLatestGithubRelease ( ref , directory )
@@ -61,8 +71,8 @@ const ROOT_ONLY_REGEX = /^\/[^/]+$/
6171function checkLatestGithubRelease (
6272 ref : RepoRef ,
6373 directory ?: string ,
64- ) : Promise < ChangelogInfo | false > {
65- return $fetch ( `https://ungh.cc/repos/${ ref . owner } /${ ref . repo } /releases/latest` )
74+ ) : Promise < ChangelogInfo | false | 0 > {
75+ return $fetch ( `https://ungh.cc/repos/${ ref . owner } /${ ref . repo } /releases/latest/notexisting ` )
6676 . then ( r => {
6777 const { release } = v . parse ( v . object ( { release : GithubReleaseSchama } ) , r )
6878
@@ -91,7 +101,12 @@ function checkLatestGithubRelease(
91101 link : matchedChangelog ,
92102 } satisfies ChangelogMarkdownInfo
93103 } )
94- . catch ( ( ) => {
104+ . catch ( ( e : FetchError ) => {
105+ if ( e . statusCode == 403 ) {
106+ // with 403 ungh.cc has exhausted it's api keys, returning 0 to indicate this
107+ return 0
108+ }
109+
95110 return false as const
96111 } )
97112}
0 commit comments