@@ -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. |
|
||||
@@ -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 🧬
|
||||
|
||||
@@ -18,6 +18,10 @@ configuration.categories = [
|
||||
{
|
||||
title: '## 🧪 Tests',
|
||||
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
|
||||
const pullRequestsWithLabels: PullRequestInfo[] = []
|
||||
pullRequestsWithLabels.push(
|
||||
|
||||
+61
-6
@@ -174,7 +174,8 @@ exports.DefaultConfiguration = {
|
||||
tag_resolver: {
|
||||
// defines the logic on how to resolve the previous tag, only relevant if `fromTag` is not specified
|
||||
method: 'semver',
|
||||
filter: undefined // filter out all tags not matching the regex
|
||||
filter: undefined,
|
||||
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
|
||||
};
|
||||
@@ -891,6 +892,7 @@ const github = __importStar(__nccwpck_require__(5438));
|
||||
const semver = __importStar(__nccwpck_require__(1383));
|
||||
const semver_1 = __nccwpck_require__(1383);
|
||||
const gitHelper_1 = __nccwpck_require__(353);
|
||||
const transform_1 = __nccwpck_require__(1644);
|
||||
class Tags {
|
||||
constructor(octokit) {
|
||||
this.octokit = octokit;
|
||||
@@ -973,7 +975,32 @@ class Tags {
|
||||
retrieveRange(repositoryPath, owner, repo, fromTag, toTag, ignorePreReleases, maxTagsToFetch, tagResolver) {
|
||||
var _a;
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const tags = sortTags(yield this.getTags(owner, repo, maxTagsToFetch), tagResolver);
|
||||
// filter out tags not matching the specified filter
|
||||
const filteredTags = filterTags(
|
||||
// retrieve the tags from the API
|
||||
yield this.getTags(owner, repo, maxTagsToFetch), tagResolver);
|
||||
// check if a transformer was defined
|
||||
const tagTransformer = (0, transform_1.validateTransformer)(tagResolver.transformer);
|
||||
let transformedTags;
|
||||
if (tagTransformer != null) {
|
||||
core.debug(`ℹ️ Using configured tagTransformer`);
|
||||
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.tmp, commit: tag.commit };
|
||||
}
|
||||
else {
|
||||
return tag;
|
||||
}
|
||||
});
|
||||
}
|
||||
let resultToTag;
|
||||
let resultFromTag;
|
||||
// ensure to resolve the toTag if it was not provided
|
||||
@@ -1041,13 +1068,34 @@ function filterTags(tags, tagResolver) {
|
||||
const filter = tagResolver.filter;
|
||||
if (filter !== undefined) {
|
||||
const regex = new RegExp(filter.pattern.replace('\\\\', '\\'), (_a = filter.flags) !== null && _a !== void 0 ? _a : 'gu');
|
||||
return tags.filter(tag => tag.name.match(regex) !== null);
|
||||
const filteredTags = tags.filter(tag => tag.name.match(regex) !== null);
|
||||
core.debug(`ℹ️ Filtered tags count: ${filteredTags.length}, original count: ${tags.length}`);
|
||||
return filteredTags;
|
||||
}
|
||||
else {
|
||||
return tags;
|
||||
}
|
||||
}
|
||||
exports.filterTags = filterTags;
|
||||
/**
|
||||
* Helper function to transform the tag name given the transformer
|
||||
*/
|
||||
function transformTags(tags, transformer) {
|
||||
return tags.map(function (tag) {
|
||||
if (transformer.pattern) {
|
||||
const transformedName = tag.name.replace(transformer.pattern, transformer.target);
|
||||
core.debug(`ℹ️ Transformed ${tag.name} to ${transformedName}`);
|
||||
return {
|
||||
tmp: tag.name,
|
||||
name: transformedName,
|
||||
commit: tag.commit
|
||||
};
|
||||
}
|
||||
else {
|
||||
return tag;
|
||||
}
|
||||
});
|
||||
}
|
||||
/*
|
||||
Sorts an array of tags as shown below:
|
||||
|
||||
@@ -1142,7 +1190,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.fillAdditionalPlaceholders = exports.buildChangelog = void 0;
|
||||
exports.validateTransformer = exports.fillAdditionalPlaceholders = exports.buildChangelog = void 0;
|
||||
const core = __importStar(__nccwpck_require__(2186));
|
||||
const configuration_1 = __nccwpck_require__(5527);
|
||||
const pullRequests_1 = __nccwpck_require__(4217);
|
||||
@@ -1345,15 +1393,18 @@ function validateTransformer(transformer) {
|
||||
try {
|
||||
let onProperty = undefined;
|
||||
let method = undefined;
|
||||
let onEmpty = undefined;
|
||||
if (transformer.hasOwnProperty('on_property')) {
|
||||
onProperty = transformer.on_property;
|
||||
method = transformer.method;
|
||||
onEmpty = transformer.on_empty;
|
||||
}
|
||||
return {
|
||||
pattern: new RegExp(transformer.pattern.replace('\\\\', '\\'), (_a = transformer.flags) !== null && _a !== void 0 ? _a : 'gu'),
|
||||
target: transformer.target || '',
|
||||
onProperty,
|
||||
method
|
||||
method,
|
||||
onEmpty
|
||||
};
|
||||
}
|
||||
catch (e) {
|
||||
@@ -1361,6 +1412,7 @@ function validateTransformer(transformer) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
exports.validateTransformer = validateTransformer;
|
||||
function extractValues(pr, extractor, extractor_usecase) {
|
||||
if (extractor.pattern == null) {
|
||||
return null;
|
||||
@@ -1379,7 +1431,7 @@ function extractValues(pr, extractor, extractor_usecase) {
|
||||
}
|
||||
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'));
|
||||
}
|
||||
}
|
||||
@@ -1389,6 +1441,9 @@ function extractValues(pr, extractor, extractor_usecase) {
|
||||
return [label.toLocaleLowerCase('en')];
|
||||
}
|
||||
}
|
||||
if (extractor.onEmpty !== undefined) {
|
||||
return [extractor.onEmpty.toLocaleLowerCase('en')];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
@@ -34,11 +34,13 @@ 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 {
|
||||
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 = {
|
||||
@@ -71,7 +73,8 @@ export const DefaultConfiguration: Configuration = {
|
||||
tag_resolver: {
|
||||
// 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.
|
||||
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
|
||||
}
|
||||
|
||||
+62
-2
@@ -5,6 +5,7 @@ import {Octokit, RestEndpointMethodTypes} from '@octokit/rest'
|
||||
import {SemVer} from 'semver'
|
||||
import {TagResolver} from './configuration'
|
||||
import {createCommandManager} from './gitHelper'
|
||||
import {RegexTransformer, validateTransformer} from './transform'
|
||||
|
||||
export interface TagResult {
|
||||
from: TagInfo | null
|
||||
@@ -16,6 +17,10 @@ export interface TagInfo {
|
||||
commit: string
|
||||
}
|
||||
|
||||
export interface SortableTagInfo extends TagInfo {
|
||||
tmp: string
|
||||
}
|
||||
|
||||
export class Tags {
|
||||
constructor(private octokit: Octokit) {}
|
||||
|
||||
@@ -114,11 +119,37 @@ export class Tags {
|
||||
maxTagsToFetch: number,
|
||||
tagResolver: TagResolver
|
||||
): Promise<TagResult> {
|
||||
const tags = sortTags(
|
||||
// filter out tags not matching the specified filter
|
||||
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) {
|
||||
core.debug(`ℹ️ Using configured tagTransformer`)
|
||||
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 resultFromTag: TagInfo | null
|
||||
|
||||
@@ -205,12 +236,41 @@ export function filterTags(
|
||||
filter.pattern.replace('\\\\', '\\'),
|
||||
filter.flags ?? 'gu'
|
||||
)
|
||||
return tags.filter(tag => tag.name.match(regex) !== null)
|
||||
const filteredTags = tags.filter(tag => tag.name.match(regex) !== null)
|
||||
core.debug(
|
||||
`ℹ️ Filtered tags count: ${filteredTags.length}, original count: ${tags.length}`
|
||||
)
|
||||
return filteredTags
|
||||
} else {
|
||||
return tags
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
const transformedName = tag.name.replace(
|
||||
transformer.pattern,
|
||||
transformer.target
|
||||
)
|
||||
core.debug(`ℹ️ Transformed ${tag.name} to ${transformedName}`)
|
||||
return {
|
||||
tmp: tag.name, // remember the original name
|
||||
name: transformedName,
|
||||
commit: tag.commit
|
||||
}
|
||||
} else {
|
||||
return tag
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/*
|
||||
Sorts an array of tags as shown below:
|
||||
|
||||
|
||||
+11
-4
@@ -293,7 +293,7 @@ function validateTransformers(
|
||||
})
|
||||
}
|
||||
|
||||
function validateTransformer(
|
||||
export function validateTransformer(
|
||||
transformer?: Transformer
|
||||
): RegexTransformer | null {
|
||||
if (transformer === undefined) {
|
||||
@@ -302,9 +302,11 @@ 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 {
|
||||
@@ -314,7 +316,8 @@ function validateTransformer(
|
||||
),
|
||||
target: transformer.target || '',
|
||||
onProperty,
|
||||
method
|
||||
method,
|
||||
onEmpty
|
||||
}
|
||||
} catch (e) {
|
||||
core.warning(`⚠️ Bad replacer regex: ${transformer.pattern}`)
|
||||
@@ -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 {
|
||||
@@ -356,12 +359,16 @@ function extractValues(
|
||||
return [label.toLocaleLowerCase('en')]
|
||||
}
|
||||
}
|
||||
if (extractor.onEmpty !== undefined) {
|
||||
return [extractor.onEmpty.toLocaleLowerCase('en')]
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
interface RegexTransformer {
|
||||
export interface RegexTransformer {
|
||||
pattern: RegExp | null
|
||||
target: string
|
||||
onProperty?: 'title' | 'author' | 'milestone' | 'body' | undefined
|
||||
method?: 'replace' | 'match' | undefined
|
||||
onEmpty?: string | undefined
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user