- add new ability to transform tags (tag name) prior to sorting, allowing to transform tags into semver format can be achieved

- tags will keep the original name post sorting
  - FIX https://github.com/mikepenz/release-changelog-builder-action/issues/571
This commit is contained in:
Mike Penz
2021-11-06 10:13:46 +01:00
parent 0a1db911b6
commit e398a20e36
3 changed files with 60 additions and 6 deletions
+4 -2
View File
@@ -38,7 +38,8 @@ export interface Extractor extends Transformer {
export interface TagResolver { export interface TagResolver {
method: string // semver, sort method: string // semver, sort
filter?: Regex // the regex to filter the tags filter?: Regex // the regex to filter the tags, prior to sorting
transformer?: Transformer // transforms the tag name using the regex, run after the filter
} }
export const DefaultConfiguration: Configuration = { export const DefaultConfiguration: Configuration = {
@@ -71,7 +72,8 @@ export const DefaultConfiguration: Configuration = {
tag_resolver: { tag_resolver: {
// defines the logic on how to resolve the previous tag, only relevant if `fromTag` is not specified // defines the logic on how to resolve the previous tag, only relevant if `fromTag` is not specified
method: 'semver', // defines which method to use, by default it will use `semver` (dropping all non matching tags). Alternative `sort` is also available. method: 'semver', // defines which method to use, by default it will use `semver` (dropping all non matching tags). Alternative `sort` is also available.
filter: undefined // filter out all tags not matching the regex filter: undefined, // filter out all tags not matching the regex
transformer: undefined // transforms the tag name using the regex, run after the filter
}, },
base_branches: [] // target branches for the merged PR ignoring PRs with different target branch, by default it will get all PRs base_branches: [] // target branches for the merged PR ignoring PRs with different target branch, by default it will get all PRs
} }
+54 -2
View File
@@ -5,6 +5,7 @@ import {Octokit, RestEndpointMethodTypes} from '@octokit/rest'
import {SemVer} from 'semver' import {SemVer} from 'semver'
import {TagResolver} from './configuration' import {TagResolver} from './configuration'
import {createCommandManager} from './gitHelper' import {createCommandManager} from './gitHelper'
import {RegexTransformer, validateTransformer} from './transform'
export interface TagResult { export interface TagResult {
from: TagInfo | null from: TagInfo | null
@@ -16,6 +17,10 @@ export interface TagInfo {
commit: string commit: string
} }
export interface SortableTagInfo extends TagInfo {
tmp: string
}
export class Tags { export class Tags {
constructor(private octokit: Octokit) {} constructor(private octokit: Octokit) {}
@@ -114,8 +119,35 @@ export class Tags {
maxTagsToFetch: number, maxTagsToFetch: number,
tagResolver: TagResolver tagResolver: TagResolver
): Promise<TagResult> { ): Promise<TagResult> {
const filteredTags = filterTags(await this.getTags(owner, repo, maxTagsToFetch), tagResolver) // filter out tags not matching the specified filter
const tags = sortTags(filteredTags, tagResolver) const filteredTags = filterTags(
// retrieve the tags from the API
await this.getTags(owner, repo, maxTagsToFetch),
tagResolver
)
// check if a transformer was defined
const tagTransformer = validateTransformer(tagResolver.transformer)
let transformedTags: TagInfo[]
if (tagTransformer != null) {
transformedTags = transformTags(filteredTags, tagTransformer)
} else {
transformedTags = filteredTags
}
let tags = sortTags(transformedTags, tagResolver)
if (tagTransformer != null) {
// restore the original name, after sorting
tags = filteredTags.map(function (tag) {
if (tag.hasOwnProperty('tmp')) {
return {name: (tag as SortableTagInfo).tmp, commit: tag.commit}
} else {
return tag
}
})
}
let resultToTag: TagInfo | null let resultToTag: TagInfo | null
let resultFromTag: TagInfo | null let resultFromTag: TagInfo | null
@@ -209,6 +241,26 @@ export function filterTags(
} }
} }
/**
* Helper function to transform the tag name given the transformer
*/
function transformTags(
tags: TagInfo[],
transformer: RegexTransformer
): TagInfo[] {
return tags.map(function (tag) {
if (transformer.pattern) {
return {
tmp: tag.name, // remember the original name
name: tag.name.replace(transformer.pattern, transformer.target),
commit: tag.commit
}
} else {
return tag
}
})
}
/* /*
Sorts an array of tags as shown below: Sorts an array of tags as shown below:
+2 -2
View File
@@ -293,7 +293,7 @@ function validateTransformers(
}) })
} }
function validateTransformer( export function validateTransformer(
transformer?: Transformer transformer?: Transformer
): RegexTransformer | null { ): RegexTransformer | null {
if (transformer === undefined) { if (transformer === undefined) {
@@ -359,7 +359,7 @@ function extractValues(
return null return null
} }
interface RegexTransformer { export interface RegexTransformer {
pattern: RegExp | null pattern: RegExp | null
target: string target: string
onProperty?: 'title' | 'author' | 'milestone' | 'body' | undefined onProperty?: 'title' | 'author' | 'milestone' | 'body' | undefined