- add ability to provide an empty string in case the regex does not match

- FIX https://github.com/mikepenz/release-changelog-builder-action/issues/576
This commit is contained in:
Mike Penz
2021-11-12 11:36:29 +01:00
parent 94e94dd7a9
commit 1fa3f709a9
3 changed files with 39 additions and 2 deletions
+29
View File
@@ -18,6 +18,10 @@ configuration.categories = [
{ {
title: '## 🧪 Tests', title: '## 🧪 Tests',
labels: ['[Test]'] labels: ['[Test]']
},
{
title: '## 🧪 Others',
labels: ['[Other]']
} }
] ]
@@ -192,6 +196,31 @@ it('Extract label from title, match multiple', async () => {
) )
}) })
it('Extract label from title, match multiple, custon non matching label', async () => {
configuration.label_extractor = [
{
pattern: '\\[Feature\\]|\\[Issue\\]',
on_property: 'title',
method: 'match',
on_empty: '[Other]'
}
]
const resultChangelog = buildChangelog(mergedPullRequests, {
owner: 'mikepenz',
repo: 'test-repo',
fromTag: '1.0.0',
toTag: '2.0.0',
failOnError: false,
commitMode: false,
configuration
})
expect(resultChangelog).toStrictEqual(
`## 🚀 Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## 🐛 Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## 🧪 Others\n\n- [AB-404] - not found label\n - PR: #4\n\n`
)
})
// test set of PRs with lables predefined // test set of PRs with lables predefined
const pullRequestsWithLabels: PullRequestInfo[] = [] const pullRequestsWithLabels: PullRequestInfo[] = []
pullRequestsWithLabels.push( pullRequestsWithLabels.push(
+1
View File
@@ -34,6 +34,7 @@ export interface Transformer extends Regex {
export interface Extractor extends Transformer { export interface Extractor extends Transformer {
on_property?: 'title' | 'author' | 'milestone' | 'body' | undefined // retrieve the property to extract the value from on_property?: 'title' | 'author' | 'milestone' | 'body' | undefined // retrieve the property to extract the value from
method?: 'replace' | 'match' | undefined // the method to use to extract the value, `match` will not use the `target` property method?: 'replace' | 'match' | undefined // the method to use to extract the value, `match` will not use the `target` property
on_empty?: string | undefined // in case the regex results in an empty string, this value is gonna be used instead (only for label_extractor currently)
} }
export interface TagResolver { export interface TagResolver {
+9 -2
View File
@@ -302,9 +302,11 @@ export function validateTransformer(
try { try {
let onProperty = undefined let onProperty = undefined
let method = undefined let method = undefined
let onEmpty = undefined
if (transformer.hasOwnProperty('on_property')) { if (transformer.hasOwnProperty('on_property')) {
onProperty = (transformer as Extractor).on_property onProperty = (transformer as Extractor).on_property
method = (transformer as Extractor).method method = (transformer as Extractor).method
onEmpty = (transformer as Extractor).on_empty
} }
return { return {
@@ -314,7 +316,8 @@ export function validateTransformer(
), ),
target: transformer.target || '', target: transformer.target || '',
onProperty, onProperty,
method method,
onEmpty
} }
} catch (e) { } catch (e) {
core.warning(`⚠️ Bad replacer regex: ${transformer.pattern}`) core.warning(`⚠️ Bad replacer regex: ${transformer.pattern}`)
@@ -347,7 +350,7 @@ function extractValues(
if (extractor.method === 'match') { if (extractor.method === 'match') {
const lables = onValue.match(extractor.pattern) const lables = onValue.match(extractor.pattern)
if (lables !== null) { if (lables !== null && lables.length > 0) {
return lables.map(label => label.toLocaleLowerCase('en')) return lables.map(label => label.toLocaleLowerCase('en'))
} }
} else { } else {
@@ -356,6 +359,9 @@ function extractValues(
return [label.toLocaleLowerCase('en')] return [label.toLocaleLowerCase('en')]
} }
} }
if (extractor.onEmpty !== undefined) {
return [extractor.onEmpty.toLocaleLowerCase('en')]
}
return null return null
} }
@@ -364,4 +370,5 @@ export interface RegexTransformer {
target: string target: string
onProperty?: 'title' | 'author' | 'milestone' | 'body' | undefined onProperty?: 'title' | 'author' | 'milestone' | 'body' | undefined
method?: 'replace' | 'match' | undefined method?: 'replace' | 'match' | undefined
onEmpty?: string | undefined
} }