- introduce new match mechanism for the label_extractor. Allowed replace (default) vs match

- introduce new test case to verify match behavior
This commit is contained in:
Mike Penz
2021-08-26 16:41:45 +02:00
parent a753722e84
commit d8f61a957d
6 changed files with 83 additions and 24 deletions
+2 -1
View File
@@ -296,8 +296,9 @@ Table of descriptions for the `configuration.json` options to configure the resu
| empty_template | Template to pick if no changes are detected. See [Template placeholders](#template-placeholders) for possible values |
| label_extractor | An array of `transform` specifications, offering a flexible API to extract additinal labels from a PR (Default: `body`, Default in commit mode: `commit message`). |
| label_extractor.pattern | A `regex` pattern, extracting values of the change message. |
| label_extractor.target | The result pattern. The result text will be used as label. If empty, no label is created. |
| label_extractor.target | The result pattern. The result text will be used as label. If empty, no label is created. (Unused for `match` method) |
| 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` |
| 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. |
+30
View File
@@ -134,3 +134,33 @@ it('Extract label from title, split regex', async () => {
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`)
})
it('Extract label from title, match', async () => {
configuration.label_extractor = [
{
"pattern": "\\[Feature\\]",
"on_property": "title",
"method": "match"
},
{
"pattern": "\\[Issue\\]",
"on_property": "title",
"method": "match"
}
]
const resultChangelog = buildChangelog(
mergedPullRequests,
{
owner: "mikepenz",
repo: "test-repo",
fromTag: "1.0.0",
toTag: "2.0.0",
failOnError: false,
commitMode: false,
configuration: 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`)
})
Generated Vendored
+22 -9
View File
@@ -1134,20 +1134,31 @@ function buildChangelog(prs, options) {
for (const extractor of labelExtractors) {
if (extractor.pattern != null) {
for (const pr of prs) {
let label;
let onValue;
if (extractor.onProperty !== undefined) {
let value = pr[extractor.onProperty];
if (value === undefined) {
core.warning(`⚠️ the provided property '${extractor.onProperty}' for \`label_extractor\` is not valid`);
value = pr['body'];
}
label = value.replace(extractor.pattern, extractor.target);
onValue = value;
}
else {
label = pr.body.replace(extractor.pattern, extractor.target);
onValue = pr.body;
}
if (label !== '') {
pr.labels.add(label.toLocaleLowerCase());
if (extractor.method === 'match') {
const lables = onValue.match(extractor.pattern);
if (lables !== null) {
for (const label of lables) {
pr.labels.add(label.toLocaleLowerCase());
}
}
}
else {
const label = onValue.replace(extractor.pattern, extractor.target);
if (label !== '') {
pr.labels.add(label.toLocaleLowerCase());
}
}
}
}
@@ -1281,21 +1292,23 @@ function validateTransformers(specifiedTransformers) {
var _a;
try {
let onProperty = undefined;
let method = undefined;
if (transformer.hasOwnProperty('on_property')) {
onProperty = transformer.on_property;
method = transformer.method;
}
return {
pattern: new RegExp(transformer.pattern.replace('\\\\', '\\'), (_a = transformer.flags) !== null && _a !== void 0 ? _a : 'gu'),
target: transformer.target,
onProperty
target: transformer.target || '',
onProperty,
method
};
}
catch (e) {
core.warning(`⚠️ Bad replacer regex: ${transformer.pattern}`);
return {
pattern: null,
target: '',
onProperty: undefined
target: ''
};
}
})
Generated Vendored
+1 -1
View File
File diff suppressed because one or more lines are too long
+4 -3
View File
@@ -22,12 +22,13 @@ export interface Category {
export interface Transformer {
pattern: string
target: string
flags?: string
target?: string
flags?: string // the regex flag to use for RegExp
}
export interface Extractor extends Transformer {
on_property: 'title' | 'author' | 'milestone' | 'body' | undefined
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
}
export interface TagResolver {
+24 -10
View File
@@ -24,7 +24,7 @@ export function buildChangelog(
for (const extractor of labelExtractors) {
if (extractor.pattern != null) {
for (const pr of prs) {
let label
let onValue
if (extractor.onProperty !== undefined) {
let value: string = pr[extractor.onProperty]
if (value === undefined) {
@@ -33,12 +33,23 @@ export function buildChangelog(
)
value = pr['body']
}
label = value.replace(extractor.pattern, extractor.target)
onValue = value
} else {
label = pr.body.replace(extractor.pattern, extractor.target)
onValue = pr.body
}
if (label !== '') {
pr.labels.add(label.toLocaleLowerCase())
if (extractor.method === 'match') {
const lables = onValue.match(extractor.pattern)
if (lables !== null) {
for (const label of lables) {
pr.labels.add(label.toLocaleLowerCase())
}
}
} else {
const label = onValue.replace(extractor.pattern, extractor.target)
if (label !== '') {
pr.labels.add(label.toLocaleLowerCase())
}
}
}
}
@@ -251,8 +262,10 @@ function validateTransformers(
.map(transformer => {
try {
let onProperty = undefined
let method = undefined
if (transformer.hasOwnProperty('on_property')) {
onProperty = (transformer as Extractor).on_property
method = (transformer as Extractor).method
}
return {
@@ -260,15 +273,15 @@ function validateTransformers(
transformer.pattern.replace('\\\\', '\\'),
transformer.flags ?? 'gu'
),
target: transformer.target,
onProperty
target: transformer.target || '',
onProperty,
method
}
} catch (e) {
core.warning(`⚠️ Bad replacer regex: ${transformer.pattern}`)
return {
pattern: null,
target: '',
onProperty: undefined
target: ''
}
}
})
@@ -278,5 +291,6 @@ function validateTransformers(
interface RegexTransformer {
pattern: RegExp | null
target: string
onProperty: 'title' | 'author' | 'milestone' | 'body' | undefined
onProperty?: 'title' | 'author' | 'milestone' | 'body' | undefined
method?: 'replace' | 'match' | undefined
}