Skip to content

Commit 34faad0

Browse files
committed
the changelog info api endpoint will now give the info when a changelog.md is being used.
1 parent 8d5ee71 commit 34faad0

File tree

7 files changed

+50
-42
lines changed

7 files changed

+50
-42
lines changed

app/composables/usePackageChangelog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export function usePackageChangelog(
44
packageName: MaybeRefOrGetter<string>,
55
version?: MaybeRefOrGetter<string | null | undefined>,
66
) {
7-
return useLazyFetch<ChangelogInfo | false>(() => {
7+
return useLazyFetch<ChangelogInfo>(() => {
88
const name = toValue(packageName)
99
const ver = toValue(version)
1010
const base = `/api/changelog/info/${name}`

app/pages/package/[[org]]/[name].vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,11 @@ const { data: skillsData } = useLazyFetch<SkillsListResponse>(
195195
196196
const { data: packageAnalysis } = usePackageAnalysis(packageName, requestedVersion)
197197
const { data: moduleReplacement } = useModuleReplacement(packageName)
198-
const { data: hasChangelog } = usePackageChangelog(packageName, requestedVersion)
198+
const { data: changelog } = usePackageChangelog(packageName, requestedVersion)
199+
200+
const hasChangelog = computed(
201+
() => changelog.value?.type == 'release' || changelog.value?.type == 'md',
202+
)
199203
200204
const {
201205
data: resolvedVersion,
@@ -883,7 +887,7 @@ const showSkeleton = shallowRef(false)
883887
{{ $t('package.links.issues') }}
884888
</LinkBase>
885889
</li>
886-
<li v-if="!!hasChangelog && resolvedVersion">
890+
<li v-if="!!changelog && resolvedVersion">
887891
<LinkBase
888892
classicon="i-carbon:warning"
889893
:to="{ name: 'changes', params: { path: [pkg.name, 'v', resolvedVersion] } }"

server/api/changelog/info/[...pkg].get.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ export default defineCachedEventHandler(
3131
})
3232
}
3333
},
34-
// {
35-
// maxAge: CACHE_MAX_AGE_ONE_DAY, // 24 hours - analysis rarely changes
36-
// swr: true,
37-
// getKey: event => {
38-
// const pkg = getRouterParam(event, 'pkg') ?? ''
39-
// return `changelog:v1:${pkg.replace(/\/+$/, '').trim()}`
40-
// },
41-
// },
34+
{
35+
maxAge: CACHE_MAX_AGE_ONE_DAY, // 24 hours
36+
swr: true,
37+
getKey: event => {
38+
const pkg = getRouterParam(event, 'pkg') ?? ''
39+
return `changelogInfo:pr1:${pkg.replace(/\/+$/, '').trim()}`
40+
},
41+
},
4242
)

server/api/changelog/releases/[provider]/[owner]/[repo].ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async function getReleasesFromGithub(owner: string, repo: string) {
5252
id: r.id,
5353
// replace single \n within <p> like with Vue's releases
5454
html: html?.replace(/(?<!>)\n/g, '<br>') ?? null,
55-
title: r.name ?? r.tag,
55+
title: r.name || r.tag,
5656
draft: r.draft,
5757
prerelease: r.prerelease,
5858
toc,

server/utils/changelog/detectChangelog.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { ChangelogReleaseInfo } from '~~/shared/types/changelog'
1+
import type { ChangelogReleaseInfo, ChangelogMarkdownInfo } from '~~/shared/types/changelog'
2+
import { ERROR_CHANGELOG_NOT_FOUND } from '~~/shared/utils/constants'
23
import { type RepoRef, parseRepoUrl } from '~~/shared/utils/git-providers'
34
import type { ExtendedPackageJson } from '~~/shared/utils/package-analysis'
45
// ChangelogInfo
@@ -22,9 +23,16 @@ export async function detectChangelog(
2223
return false
2324
}
2425

25-
const releaseInfo = await checkReleases(repoRef)
26+
const changelog = (await checkReleases(repoRef)) || (await checkChangelogFile(repoRef))
2627

27-
return releaseInfo || checkChangelogFile(repoRef)
28+
if (changelog) {
29+
return changelog
30+
}
31+
32+
throw createError({
33+
statusCode: 404,
34+
statusMessage: ERROR_CHANGELOG_NOT_FOUND,
35+
})
2836
}
2937

3038
/**
@@ -69,13 +77,23 @@ function getLatestReleaseUrl(ref: RepoRef): null | string[] {
6977
return null
7078
}
7179

72-
const CHANGELOG_FILENAMES = ['changelog', 'history', 'changes', 'news', 'releases'] as const
80+
const EXTENSIONS = ['.md', ''] as const
7381

74-
async function checkChangelogFile(ref: RepoRef) {
75-
const checkUrls = getChangelogUrls(ref)
82+
const CHANGELOG_FILENAMES = ['changelog', 'releases', 'changes', 'history', 'news']
83+
.map(fileName => {
84+
const fileNameUpperCase = fileName.toUpperCase()
85+
return EXTENSIONS.map(ext => [`${fileNameUpperCase}${ext}`, `${fileName}${ext}`])
86+
})
87+
.flat(3)
7688

77-
for (const checkUrl of checkUrls ?? []) {
78-
const exists = await fetch(checkUrl, {
89+
async function checkChangelogFile(ref: RepoRef): Promise<ChangelogMarkdownInfo | false> {
90+
const baseUrl = getBaseFileUrl(ref)
91+
if (!baseUrl) {
92+
return false
93+
}
94+
95+
for (const fileName of CHANGELOG_FILENAMES) {
96+
const exists = await fetch(`${baseUrl}/${fileName}`, {
7997
headers: {
8098
// GitHub API requires User-Agent
8199
'User-Agent': 'npmx.dev',
@@ -85,32 +103,16 @@ async function checkChangelogFile(ref: RepoRef) {
85103
.then(r => r.ok)
86104
.catch(() => false)
87105
if (exists) {
88-
console.log('exists', checkUrl)
89-
return true
106+
return {
107+
type: 'md',
108+
provider: ref.provider,
109+
path: fileName,
110+
} satisfies ChangelogMarkdownInfo
90111
}
91112
}
92113
return false
93114
}
94115

95-
function getChangelogUrls(ref: RepoRef) {
96-
const baseUrl = getBaseFileUrl(ref)
97-
if (!baseUrl) {
98-
return
99-
}
100-
101-
return CHANGELOG_FILENAMES.flatMap(fileName => {
102-
const fileNameUpCase = fileName.toUpperCase()
103-
return [
104-
`${baseUrl}/${fileNameUpCase}.md`,
105-
`${baseUrl}/${fileName}.md`,
106-
`${baseUrl}/${fileNameUpCase}`,
107-
`${baseUrl}/${fileName}`,
108-
`${baseUrl}/${fileNameUpCase}.txt`,
109-
`${baseUrl}/${fileName}.txt`,
110-
]
111-
})
112-
}
113-
114116
function getBaseFileUrl(ref: RepoRef) {
115117
switch (ref.provider) {
116118
case 'github':

shared/types/changelog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface ChangelogMarkdownInfo {
1313
/**
1414
* location within the repository
1515
*/
16-
location: string
16+
path: string
1717
}
1818

1919
export type ChangelogInfo = ChangelogReleaseInfo | ChangelogMarkdownInfo

shared/utils/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export const ERROR_GRAVATAR_FETCH_FAILED = 'Failed to fetch Gravatar profile.'
3535
export const ERROR_GRAVATAR_EMAIL_UNAVAILABLE = "User's email not accessible."
3636
export const ERROR_NEED_REAUTH = 'User needs to reauthenticate'
3737

38+
export const ERROR_CHANGELOG_NOT_FOUND =
39+
'No releases or changelogs have been found for this package'
3840
export const ERROR_CHANGELOG_RELEASES_FAILED = 'Failed to get releases'
3941
export const THROW_INCOMPLETE_PARAM = "Couldn't do request due to incomplete parameters"
4042

0 commit comments

Comments
 (0)