- add ability to trim values prior to inserting them in templates
This commit is contained in:
@@ -253,6 +253,7 @@ This configuration is a `.json` file in the following format. (The below shocase
|
||||
"target": "- $4\n - $6"
|
||||
}
|
||||
],
|
||||
"trim_values": false,
|
||||
"max_tags_to_fetch": 200,
|
||||
"max_pull_requests": 200,
|
||||
"max_back_track_time_days": 365,
|
||||
@@ -409,6 +410,7 @@ Table of descriptions for the `configuration.json` options to configure the resu
|
||||
| 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 |
|
||||
| trim_values | Defines if all values inserted in templates are `trimmed`. Default: false |
|
||||
|
||||
## Experimental 🧪
|
||||
|
||||
|
||||
+17
-16
@@ -218,7 +218,8 @@ exports.DefaultConfiguration = {
|
||||
transformer: undefined // transforms the tag name using the regex, run after the filter
|
||||
},
|
||||
base_branches: [],
|
||||
custom_placeholders: []
|
||||
custom_placeholders: [],
|
||||
trim_values: false // defines if values are being trimmed prior to inserting
|
||||
};
|
||||
|
||||
|
||||
@@ -1627,7 +1628,7 @@ function buildChangelog(diffInfo, prs, options) {
|
||||
const transformedMap = new Map();
|
||||
// convert PRs to their text representation
|
||||
for (const pr of prs) {
|
||||
transformedMap.set(pr, transform(fillPrTemplate(pr, config.pr_template || configuration_1.DefaultConfiguration.pr_template, placeholders, placeholderPrMap), validatedTransformers));
|
||||
transformedMap.set(pr, transform(fillPrTemplate(pr, config.pr_template || configuration_1.DefaultConfiguration.pr_template, placeholders, placeholderPrMap, config), validatedTransformers));
|
||||
}
|
||||
core.info(`ℹ️ Used ${validatedTransformers.length} transformers to adjust message`);
|
||||
core.info(`✒️ Wrote messages for ${prs.length} pull requests`);
|
||||
@@ -1768,8 +1769,8 @@ function buildChangelog(diffInfo, prs, options) {
|
||||
placeholderMap.set('COMMITS', diffInfo.commits.toString());
|
||||
fillAdditionalPlaceholders(options, placeholderMap);
|
||||
let transformedChangelog = config.template || configuration_1.DefaultConfiguration.template;
|
||||
transformedChangelog = replacePlaceholders(transformedChangelog, EMPTY_MAP, placeholderMap, placeholders, placeholderPrMap);
|
||||
transformedChangelog = replacePrPlaceholders(transformedChangelog, placeholderPrMap);
|
||||
transformedChangelog = replacePlaceholders(transformedChangelog, EMPTY_MAP, placeholderMap, placeholders, placeholderPrMap, config);
|
||||
transformedChangelog = replacePrPlaceholders(transformedChangelog, placeholderPrMap, config);
|
||||
transformedChangelog = cleanupPrPlaceholders(transformedChangelog, placeholders);
|
||||
core.info(`ℹ️ Filled template`);
|
||||
return transformedChangelog;
|
||||
@@ -1782,7 +1783,7 @@ function replaceEmptyTemplate(template, options) {
|
||||
}
|
||||
const placeholderMap = new Map();
|
||||
fillAdditionalPlaceholders(options, placeholderMap);
|
||||
return replacePlaceholders(template, new Map(), placeholderMap, placeholders);
|
||||
return replacePlaceholders(template, new Map(), placeholderMap, placeholders, undefined, options.configuration);
|
||||
}
|
||||
exports.replaceEmptyTemplate = replaceEmptyTemplate;
|
||||
function fillAdditionalPlaceholders(options, placeholderMap /* placeholderKey and original value */) {
|
||||
@@ -1803,7 +1804,7 @@ function fillAdditionalPlaceholders(options, placeholderMap /* placeholderKey an
|
||||
}
|
||||
placeholderMap.set('RELEASE_DIFF', `https://github.com/${options.owner}/${options.repo}/compare/${options.fromTag.name}...${options.toTag.name}`);
|
||||
}
|
||||
function fillPrTemplate(pr, template, placeholders /* placeholders to apply */, placeholderPrMap /* map to keep replaced placeholder values with their key */) {
|
||||
function fillPrTemplate(pr, template, placeholders /* placeholders to apply */, placeholderPrMap /* map to keep replaced placeholder values with their key */, configuration) {
|
||||
var _a, _b, _c, _d, _e, _f;
|
||||
const arrayPlaceholderMap = new Map();
|
||||
fillReviewPlaceholders(arrayPlaceholderMap, 'REVIEWS', pr.reviews || []);
|
||||
@@ -1827,22 +1828,22 @@ function fillPrTemplate(pr, template, placeholders /* placeholders to apply */,
|
||||
placeholderMap.set('APPROVERS', ((_f = pr.approvedReviewers) === null || _f === void 0 ? void 0 : _f.join(', ')) || '');
|
||||
placeholderMap.set('BRANCH', pr.branch || '');
|
||||
placeholderMap.set('BASE_BRANCH', pr.baseBranch);
|
||||
return replacePlaceholders(template, arrayPlaceholderMap, placeholderMap, placeholders, placeholderPrMap);
|
||||
return replacePlaceholders(template, arrayPlaceholderMap, placeholderMap, placeholders, placeholderPrMap, configuration);
|
||||
}
|
||||
function replacePlaceholders(template, arrayPlaceholderMap /* arrayPlaceholderKey and original value */, placeholderMap /* placeholderKey and original value */, placeholders /* placeholders to apply */, placeholderPrMap /* map to keep replaced placeholder values with their key */) {
|
||||
function replacePlaceholders(template, arrayPlaceholderMap /* arrayPlaceholderKey and original value */, placeholderMap /* placeholderKey and original value */, placeholders /* placeholders to apply */, placeholderPrMap /* map to keep replaced placeholder values with their key */, configuration) {
|
||||
let transformed = template;
|
||||
// replace array placeholders first
|
||||
for (const [key, value] of arrayPlaceholderMap) {
|
||||
transformed = handlePlaceholder(transformed, key, value, placeholders, placeholderPrMap);
|
||||
transformed = handlePlaceholder(transformed, key, value, placeholders, placeholderPrMap, configuration);
|
||||
}
|
||||
// replace traditional placeholders
|
||||
for (const [key, value] of placeholderMap) {
|
||||
transformed = handlePlaceholder(transformed, key, value, placeholders, placeholderPrMap);
|
||||
transformed = handlePlaceholder(transformed, key, value, placeholders, placeholderPrMap, configuration);
|
||||
}
|
||||
return transformed;
|
||||
}
|
||||
function handlePlaceholder(template, key, value, placeholders /* placeholders to apply */, placeholderPrMap /* map to keep replaced placeholder values with their key */) {
|
||||
let transformed = template.replaceAll(`\${{${key}}}`, value);
|
||||
function handlePlaceholder(template, key, value, placeholders /* placeholders to apply */, placeholderPrMap /* map to keep replaced placeholder values with their key */, configuration) {
|
||||
let transformed = template.replaceAll(`\${{${key}}}`, configuration.trim_values ? value.trim() : value);
|
||||
// replace custom placeholders
|
||||
const phs = placeholders.get(key);
|
||||
if (phs) {
|
||||
@@ -1855,7 +1856,7 @@ function handlePlaceholder(template, key, value, placeholders /* placeholders to
|
||||
if (placeholderPrMap) {
|
||||
(0, utils_1.createOrSet)(placeholderPrMap, placeholder.name, extractedValue);
|
||||
}
|
||||
transformed = transformed.replaceAll(`\${{${placeholder.name}}}`, extractedValue);
|
||||
transformed = transformed.replaceAll(`\${{${placeholder.name}}}`, configuration.trim_values ? extractedValue.trim() : extractedValue);
|
||||
if (core.isDebug()) {
|
||||
core.debug(` Custom Placeholder successfully matched data - ${extractValues} (${placeholder.name})`);
|
||||
}
|
||||
@@ -1884,17 +1885,17 @@ function fillReviewPlaceholders(placeholderMap /* placeholderKey and original va
|
||||
placeholderMap.set(`\${{${parentKey}[*].${childKey}}}`, values.map(value => { var _a; return ((_a = value[childKey]) === null || _a === void 0 ? void 0 : _a.toLocaleString('en')) || ''; }).join(', '));
|
||||
}
|
||||
}
|
||||
function replacePrPlaceholders(template, placeholderPrMap /* map with all pr related custom placeholder values */) {
|
||||
function replacePrPlaceholders(template, placeholderPrMap /* map with all pr related custom placeholder values */, configuration) {
|
||||
let transformed = template;
|
||||
for (const [key, values] of placeholderPrMap) {
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
transformed = transformed.replaceAll(`\${{${key}[${i}]}}`, values[i]);
|
||||
transformed = transformed.replaceAll(`\${{${key}[${i}]}}`, configuration.trim_values ? values[i].trim() : values[i]);
|
||||
}
|
||||
transformed = transformed.replaceAll(`\${{${key}[*]}}`, values.join(''));
|
||||
}
|
||||
return transformed;
|
||||
}
|
||||
function cleanupPrPlaceholders(template, placeholders /* placeholders to apply */) {
|
||||
function cleanupPrPlaceholders(template, placeholders) {
|
||||
let transformed = template;
|
||||
for (const [, phs] of placeholders) {
|
||||
for (const ph of phs) {
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
@@ -15,6 +15,7 @@ export interface Configuration {
|
||||
tag_resolver: TagResolver
|
||||
base_branches: string[]
|
||||
custom_placeholders?: Placeholder[]
|
||||
trim_values: boolean
|
||||
}
|
||||
|
||||
export interface Category {
|
||||
@@ -95,5 +96,6 @@ export const DefaultConfiguration: Configuration = {
|
||||
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
|
||||
custom_placeholders: []
|
||||
custom_placeholders: [],
|
||||
trim_values: false // defines if values are being trimmed prior to inserting
|
||||
}
|
||||
|
||||
+23
-16
@@ -1,5 +1,5 @@
|
||||
import * as core from '@actions/core'
|
||||
import {Category, DefaultConfiguration, Extractor, Placeholder, Transformer} from './configuration'
|
||||
import {Category, Configuration, DefaultConfiguration, Extractor, Placeholder, Transformer} from './configuration'
|
||||
import {CommentInfo, EMPTY_COMMENT_INFO, PullRequestInfo, sortPullRequests} from './pullRequests'
|
||||
import {ReleaseNotesOptions} from './releaseNotes'
|
||||
import {DiffInfo} from './commits'
|
||||
@@ -76,7 +76,7 @@ export function buildChangelog(diffInfo: DiffInfo, prs: PullRequestInfo[], optio
|
||||
transformedMap.set(
|
||||
pr,
|
||||
transform(
|
||||
fillPrTemplate(pr, config.pr_template || DefaultConfiguration.pr_template, placeholders, placeholderPrMap),
|
||||
fillPrTemplate(pr, config.pr_template || DefaultConfiguration.pr_template, placeholders, placeholderPrMap, config),
|
||||
validatedTransformers
|
||||
)
|
||||
)
|
||||
@@ -253,8 +253,8 @@ export function buildChangelog(diffInfo: DiffInfo, prs: PullRequestInfo[], optio
|
||||
fillAdditionalPlaceholders(options, placeholderMap)
|
||||
|
||||
let transformedChangelog = config.template || DefaultConfiguration.template
|
||||
transformedChangelog = replacePlaceholders(transformedChangelog, EMPTY_MAP, placeholderMap, placeholders, placeholderPrMap)
|
||||
transformedChangelog = replacePrPlaceholders(transformedChangelog, placeholderPrMap)
|
||||
transformedChangelog = replacePlaceholders(transformedChangelog, EMPTY_MAP, placeholderMap, placeholders, placeholderPrMap, config)
|
||||
transformedChangelog = replacePrPlaceholders(transformedChangelog, placeholderPrMap, config)
|
||||
transformedChangelog = cleanupPrPlaceholders(transformedChangelog, placeholders)
|
||||
core.info(`ℹ️ Filled template`)
|
||||
return transformedChangelog
|
||||
@@ -267,7 +267,7 @@ export function replaceEmptyTemplate(template: string, options: ReleaseNotesOpti
|
||||
}
|
||||
const placeholderMap = new Map<string, string>()
|
||||
fillAdditionalPlaceholders(options, placeholderMap)
|
||||
return replacePlaceholders(template, new Map<string, string>(), placeholderMap, placeholders)
|
||||
return replacePlaceholders(template, new Map<string, string>(), placeholderMap, placeholders, undefined, options.configuration)
|
||||
}
|
||||
|
||||
function fillAdditionalPlaceholders(
|
||||
@@ -297,7 +297,8 @@ function fillPrTemplate(
|
||||
pr: PullRequestInfo,
|
||||
template: string,
|
||||
placeholders: Map<string, Placeholder[]> /* placeholders to apply */,
|
||||
placeholderPrMap: Map<string, string[]> /* map to keep replaced placeholder values with their key */
|
||||
placeholderPrMap: Map<string, string[]> /* map to keep replaced placeholder values with their key */,
|
||||
configuration: Configuration
|
||||
): string {
|
||||
const arrayPlaceholderMap = new Map<string, string>()
|
||||
fillReviewPlaceholders(arrayPlaceholderMap, 'REVIEWS', pr.reviews || [])
|
||||
@@ -321,7 +322,7 @@ function fillPrTemplate(
|
||||
placeholderMap.set('APPROVERS', pr.approvedReviewers?.join(', ') || '')
|
||||
placeholderMap.set('BRANCH', pr.branch || '')
|
||||
placeholderMap.set('BASE_BRANCH', pr.baseBranch)
|
||||
return replacePlaceholders(template, arrayPlaceholderMap, placeholderMap, placeholders, placeholderPrMap)
|
||||
return replacePlaceholders(template, arrayPlaceholderMap, placeholderMap, placeholders, placeholderPrMap, configuration)
|
||||
}
|
||||
|
||||
function replacePlaceholders(
|
||||
@@ -329,18 +330,19 @@ function replacePlaceholders(
|
||||
arrayPlaceholderMap: Map<string, string> /* arrayPlaceholderKey and original value */,
|
||||
placeholderMap: Map<string, string> /* placeholderKey and original value */,
|
||||
placeholders: Map<string, Placeholder[]> /* placeholders to apply */,
|
||||
placeholderPrMap?: Map<string, string[]> /* map to keep replaced placeholder values with their key */
|
||||
placeholderPrMap: Map<string, string[]> | undefined /* map to keep replaced placeholder values with their key */,
|
||||
configuration: Configuration
|
||||
): string {
|
||||
let transformed = template
|
||||
|
||||
// replace array placeholders first
|
||||
for (const [key, value] of arrayPlaceholderMap) {
|
||||
transformed = handlePlaceholder(transformed, key, value, placeholders, placeholderPrMap)
|
||||
transformed = handlePlaceholder(transformed, key, value, placeholders, placeholderPrMap, configuration)
|
||||
}
|
||||
|
||||
// replace traditional placeholders
|
||||
for (const [key, value] of placeholderMap) {
|
||||
transformed = handlePlaceholder(transformed, key, value, placeholders, placeholderPrMap)
|
||||
transformed = handlePlaceholder(transformed, key, value, placeholders, placeholderPrMap, configuration)
|
||||
}
|
||||
|
||||
return transformed
|
||||
@@ -351,9 +353,10 @@ function handlePlaceholder(
|
||||
key: string,
|
||||
value: string,
|
||||
placeholders: Map<string, Placeholder[]> /* placeholders to apply */,
|
||||
placeholderPrMap?: Map<string, string[]> /* map to keep replaced placeholder values with their key */
|
||||
placeholderPrMap: Map<string, string[]> | undefined /* map to keep replaced placeholder values with their key */,
|
||||
configuration: Configuration
|
||||
): string {
|
||||
let transformed = template.replaceAll(`\${{${key}}}`, value)
|
||||
let transformed = template.replaceAll(`\${{${key}}}`, configuration.trim_values ? value.trim() : value)
|
||||
// replace custom placeholders
|
||||
const phs = placeholders.get(key)
|
||||
if (phs) {
|
||||
@@ -366,7 +369,10 @@ function handlePlaceholder(
|
||||
if (placeholderPrMap) {
|
||||
createOrSet(placeholderPrMap, placeholder.name, extractedValue)
|
||||
}
|
||||
transformed = transformed.replaceAll(`\${{${placeholder.name}}}`, extractedValue)
|
||||
transformed = transformed.replaceAll(
|
||||
`\${{${placeholder.name}}}`,
|
||||
configuration.trim_values ? extractedValue.trim() : extractedValue
|
||||
)
|
||||
|
||||
if (core.isDebug()) {
|
||||
core.debug(` Custom Placeholder successfully matched data - ${extractValues} (${placeholder.name})`)
|
||||
@@ -410,19 +416,20 @@ function fillReviewPlaceholders(
|
||||
|
||||
function replacePrPlaceholders(
|
||||
template: string,
|
||||
placeholderPrMap: Map<string, string[]> /* map with all pr related custom placeholder values */
|
||||
placeholderPrMap: Map<string, string[]> /* map with all pr related custom placeholder values */,
|
||||
configuration: Configuration
|
||||
): string {
|
||||
let transformed = template
|
||||
for (const [key, values] of placeholderPrMap) {
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
transformed = transformed.replaceAll(`\${{${key}[${i}]}}`, values[i])
|
||||
transformed = transformed.replaceAll(`\${{${key}[${i}]}}`, configuration.trim_values ? values[i].trim() : values[i])
|
||||
}
|
||||
transformed = transformed.replaceAll(`\${{${key}[*]}}`, values.join(''))
|
||||
}
|
||||
return transformed
|
||||
}
|
||||
|
||||
function cleanupPrPlaceholders(template: string, placeholders: Map<string, Placeholder[]> /* placeholders to apply */): string {
|
||||
function cleanupPrPlaceholders(template: string, placeholders: Map<string, Placeholder[]>): string {
|
||||
let transformed = template
|
||||
for (const [, phs] of placeholders) {
|
||||
for (const ph of phs) {
|
||||
|
||||
Reference in New Issue
Block a user