Skip to content

Commit

Permalink
feat: support tag filter (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
Innei authored Oct 5, 2024
1 parent 3b37a71 commit 6c07e87
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export async function resolveConfig(options: ChangelogOptions) {
config.baseUrl = config.baseUrl ?? 'github.com'
config.baseUrlApi = config.baseUrlApi ?? 'api.github.com'
config.to = config.to || await getCurrentGitBranch()
config.from = config.from || await getLastMatchingTag(config.to) || await getFirstGitCommit()
config.tagFilter = config.tagFilter ?? (() => true)
config.from = config.from || await getLastMatchingTag(config.to, config.tagFilter) || await getFirstGitCommit()
// @ts-expect-error backward compatibility
config.repo = config.repo || config.github || await getGitHubRepo(config.baseUrl)
config.prerelease = config.prerelease ?? isPrerelease(config.to)
Expand Down
7 changes: 4 additions & 3 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ function getTagWithoutPrefix(tag: string) {
return tag.replace(/^v/, '')
}

export async function getLastMatchingTag(inputTag: string) {
export async function getLastMatchingTag(inputTag: string, tagFilter: (tag: string) => boolean) {
const inputTagWithoutPrefix = getTagWithoutPrefix(inputTag)
const isVersion = semver.valid(inputTagWithoutPrefix) !== null
const isPrerelease = semver.prerelease(inputTag) !== null
const tags = await getGitTags()
const filteredTags = tags.filter(tagFilter)

let tag: string | undefined
// Doing a stable release, find the last stable release to compare with
if (!isPrerelease && isVersion) {
tag = tags.find((tag) => {
tag = filteredTags.find((tag) => {
const tagWithoutPrefix = getTagWithoutPrefix(tag)

return tagWithoutPrefix !== inputTagWithoutPrefix
Expand All @@ -46,7 +47,7 @@ export async function getLastMatchingTag(inputTag: string) {
}

// Fallback to the last tag, that are not the input tag
tag ||= tags.find(tag => tag !== inputTag)
tag ||= filteredTags.find(tag => tag !== inputTag)

return tag
}
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ export interface ChangelogOptions extends Partial<ChangelogenOptions> {
* @default api.github.com
*/
baseUrlApi?: string

/**
* Filter tags
*/
tagFilter?: (tag: string) => boolean
}

export type ResolvedChangelogOptions = Required<ChangelogOptions>
Expand Down

0 comments on commit 6c07e87

Please sign in to comment.