Skip to content

Commit

Permalink
Merge pull request #581 from mikepenz/feature/576
Browse files Browse the repository at this point in the history
New API to provide `on_empty` placeholder on non matching label_extractor regex
  • Loading branch information
mikepenz authored Nov 12, 2021
2 parents 94e94dd + a9444bd commit 080a856
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ Table of descriptions for the `configuration.json` options to configure the resu
| label_extractor.on_property | The property to retrieve the text from. This is optional. Defaults to: `body`. Alternative values: `title`, `author`, `milestone`. |
| label_extractor.method | The extraction method used. Defaults to: `replace`. Alternative value: `match`. The method specified references the JavaScript String method. |
| label_extractor.flags | Defines the regex flags specified for the pattern. Default: `gu` |
| label_extractor.on_empty | Defines the placeholder to be filled in, if the regex does not lead to a result. |
| duplicate_filter | Defines the `Extractor` to use for retrieving the identifier for a PR. In case of duplicates will keep the last matching pull request (depends on `sort`). See `label_extractor` for details on `Extractor` properties. |
| transformers | An array of `transform` specifications, offering a flexible API to modify the text per pull request. This is applied on the change text created with `pr_template`. `transformers` are executed per change, in the order specified |
| transformer.pattern | A `regex` pattern, extracting values of the change message. |
Expand All @@ -329,6 +330,7 @@ Table of descriptions for the `configuration.json` options to configure the resu
| tag_resolver | Section to provide configuration for the tag resolving logic. Used if no `fromTag` is provided |
| tag_resolver.method | Defines the method to use. Current options are: `semver`, `sort`. Default: `semver` |
| tag_resolver.filter | Defines a regex which is used to filter out tags not matching. |
| tag_resolver.transformer | Defines a regex transformer used to optionally transform the tag after the filter was applied. Allows to adjust the format to e.g. semver. |
| base_branches | The target branches for the merged PR, ingnores PRs with different target branch. Values can be a `regex`. Default: allow all base branches |

## Contribute 🧬
Expand Down
29 changes: 29 additions & 0 deletions __tests__/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ configuration.categories = [
{
title: '## 🧪 Tests',
labels: ['[Test]']
},
{
title: '## 🧪 Others',
labels: ['[Other]']
}
]

Expand Down Expand Up @@ -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
const pullRequestsWithLabels: PullRequestInfo[] = []
pullRequestsWithLabels.push(
Expand Down
1 change: 1 addition & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface Transformer extends Regex {
export interface Extractor extends Transformer {
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
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 {
Expand Down
11 changes: 9 additions & 2 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,11 @@ export function validateTransformer(
try {
let onProperty = undefined
let method = undefined
let onEmpty = undefined
if (transformer.hasOwnProperty('on_property')) {
onProperty = (transformer as Extractor).on_property
method = (transformer as Extractor).method
onEmpty = (transformer as Extractor).on_empty
}

return {
Expand All @@ -314,7 +316,8 @@ export function validateTransformer(
),
target: transformer.target || '',
onProperty,
method
method,
onEmpty
}
} catch (e) {
core.warning(`⚠️ Bad replacer regex: ${transformer.pattern}`)
Expand Down Expand Up @@ -347,7 +350,7 @@ function extractValues(

if (extractor.method === 'match') {
const lables = onValue.match(extractor.pattern)
if (lables !== null) {
if (lables !== null && lables.length > 0) {
return lables.map(label => label.toLocaleLowerCase('en'))
}
} else {
Expand All @@ -356,6 +359,9 @@ function extractValues(
return [label.toLocaleLowerCase('en')]
}
}
if (extractor.onEmpty !== undefined) {
return [extractor.onEmpty.toLocaleLowerCase('en')]
}
return null
}

Expand All @@ -364,4 +370,5 @@ export interface RegexTransformer {
target: string
onProperty?: 'title' | 'author' | 'milestone' | 'body' | undefined
method?: 'replace' | 'match' | undefined
onEmpty?: string | undefined
}

0 comments on commit 080a856

Please sign in to comment.