- update replacement logic for array and comment types (fix cleanup if no match)

- adjust style definition - 140 line length
This commit is contained in:
Mike Penz
2022-12-09 14:09:40 +00:00
committed by GitHub
parent 3e744d00f1
commit 27d0a0a53c
9 changed files with 145 additions and 155 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
{ {
"printWidth": 120, "printWidth": 140,
"tabWidth": 2, "tabWidth": 2,
"useTabs": false, "useTabs": false,
"semi": false, "semi": false,
-2
View File
@@ -60,8 +60,6 @@ Specify the action as part of your GitHub actions workflow:
- name: "Build Changelog" - name: "Build Changelog"
id: build_changelog id: build_changelog
uses: mikepenz/release-changelog-builder-action@{latest-release} uses: mikepenz/release-changelog-builder-action@{latest-release}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
``` ```
### Action outputs ### Action outputs
Generated Vendored
+56 -50
View File
@@ -1571,6 +1571,7 @@ const core = __importStar(__nccwpck_require__(2186));
const configuration_1 = __nccwpck_require__(5527); const configuration_1 = __nccwpck_require__(5527);
const pullRequests_1 = __nccwpck_require__(4217); const pullRequests_1 = __nccwpck_require__(4217);
const utils_1 = __nccwpck_require__(918); const utils_1 = __nccwpck_require__(918);
const EMPTY_MAP = new Map();
function buildChangelog(diffInfo, prs, options) { function buildChangelog(diffInfo, prs, options) {
// sort to target order // sort to target order
const config = options.configuration; const config = options.configuration;
@@ -1767,9 +1768,9 @@ function buildChangelog(diffInfo, prs, options) {
placeholderMap.set('COMMITS', diffInfo.commits.toString()); placeholderMap.set('COMMITS', diffInfo.commits.toString());
fillAdditionalPlaceholders(options, placeholderMap); fillAdditionalPlaceholders(options, placeholderMap);
let transformedChangelog = config.template || configuration_1.DefaultConfiguration.template; let transformedChangelog = config.template || configuration_1.DefaultConfiguration.template;
transformedChangelog = replacePlaceholders(transformedChangelog, placeholderMap, placeholders, placeholderPrMap); transformedChangelog = replacePlaceholders(transformedChangelog, EMPTY_MAP, placeholderMap, placeholders, placeholderPrMap);
transformedChangelog = replacePrPlaceholders(transformedChangelog, placeholderPrMap); transformedChangelog = replacePrPlaceholders(transformedChangelog, placeholderPrMap);
transformedChangelog = cleanupPrPlaceHolders(transformedChangelog, placeholders); transformedChangelog = cleanupPrPlaceholders(transformedChangelog, placeholders);
core.info(`️ Filled template`); core.info(`️ Filled template`);
return transformedChangelog; return transformedChangelog;
} }
@@ -1781,7 +1782,7 @@ function replaceEmptyTemplate(template, options) {
} }
const placeholderMap = new Map(); const placeholderMap = new Map();
fillAdditionalPlaceholders(options, placeholderMap); fillAdditionalPlaceholders(options, placeholderMap);
return replacePlaceholders(template, placeholderMap, placeholders); return replacePlaceholders(template, new Map(), placeholderMap, placeholders);
} }
exports.replaceEmptyTemplate = replaceEmptyTemplate; exports.replaceEmptyTemplate = replaceEmptyTemplate;
function fillAdditionalPlaceholders(options, placeholderMap /* placeholderKey and original value */) { function fillAdditionalPlaceholders(options, placeholderMap /* placeholderKey and original value */) {
@@ -1804,10 +1805,8 @@ function fillAdditionalPlaceholders(options, placeholderMap /* placeholderKey an
} }
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 */) {
var _a, _b, _c, _d, _e, _f; var _a, _b, _c, _d, _e, _f;
let transformed = replaceArrayPlaceholders(template, 'ASSIGNEES', pr.assignees || []); const arrayPlaceholderMap = new Map();
transformed = replaceArrayPlaceholders(transformed, 'REVIEWERS', pr.requestedReviewers || []); fillReviewPlaceholders(arrayPlaceholderMap, 'REVIEWS', pr.reviews || []);
transformed = replaceArrayPlaceholders(transformed, 'APPROVERS', pr.approvedReviewers || []);
transformed = replaceReviewPlaceholders(transformed, 'REVIEWS', pr.reviews || []);
const placeholderMap = new Map(); const placeholderMap = new Map();
placeholderMap.set('NUMBER', pr.number.toString()); placeholderMap.set('NUMBER', pr.number.toString());
placeholderMap.set('TITLE', pr.title); placeholderMap.set('TITLE', pr.title);
@@ -1820,44 +1819,71 @@ function fillPrTemplate(pr, template, placeholders /* placeholders to apply */,
placeholderMap.set('LABELS', ((_c = (_b = [...pr.labels]) === null || _b === void 0 ? void 0 : _b.filter(l => !l.startsWith('--rcba-'))) === null || _c === void 0 ? void 0 : _c.join(', ')) || ''); placeholderMap.set('LABELS', ((_c = (_b = [...pr.labels]) === null || _b === void 0 ? void 0 : _b.filter(l => !l.startsWith('--rcba-'))) === null || _c === void 0 ? void 0 : _c.join(', ')) || '');
placeholderMap.set('MILESTONE', pr.milestone || ''); placeholderMap.set('MILESTONE', pr.milestone || '');
placeholderMap.set('BODY', pr.body); placeholderMap.set('BODY', pr.body);
fillArrayPlaceholders(arrayPlaceholderMap, 'ASSIGNEES', pr.assignees || []);
placeholderMap.set('ASSIGNEES', ((_d = pr.assignees) === null || _d === void 0 ? void 0 : _d.join(', ')) || ''); placeholderMap.set('ASSIGNEES', ((_d = pr.assignees) === null || _d === void 0 ? void 0 : _d.join(', ')) || '');
fillArrayPlaceholders(arrayPlaceholderMap, 'REVIEWERS', pr.requestedReviewers || []);
placeholderMap.set('REVIEWERS', ((_e = pr.requestedReviewers) === null || _e === void 0 ? void 0 : _e.join(', ')) || ''); placeholderMap.set('REVIEWERS', ((_e = pr.requestedReviewers) === null || _e === void 0 ? void 0 : _e.join(', ')) || '');
fillArrayPlaceholders(arrayPlaceholderMap, 'APPROVERS', pr.approvedReviewers || []);
placeholderMap.set('APPROVERS', ((_f = pr.approvedReviewers) === null || _f === void 0 ? void 0 : _f.join(', ')) || ''); placeholderMap.set('APPROVERS', ((_f = pr.approvedReviewers) === null || _f === void 0 ? void 0 : _f.join(', ')) || '');
placeholderMap.set('BRANCH', pr.branch || ''); placeholderMap.set('BRANCH', pr.branch || '');
placeholderMap.set('BASE_BRANCH', pr.baseBranch); placeholderMap.set('BASE_BRANCH', pr.baseBranch);
return replacePlaceholders(transformed, placeholderMap, placeholders, placeholderPrMap); return replacePlaceholders(template, arrayPlaceholderMap, placeholderMap, placeholders, placeholderPrMap);
} }
function replacePlaceholders(template, 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 */) {
let transformed = template; let transformed = template;
// replace array placeholders first
for (const [key, value] of arrayPlaceholderMap) {
transformed = handlePlaceholder(transformed, key, value, placeholders, placeholderPrMap);
}
// replace traditional placeholders
for (const [key, value] of placeholderMap) { for (const [key, value] of placeholderMap) {
transformed = transformed.replaceAll(`\${{${key}}}`, value); transformed = handlePlaceholder(transformed, key, value, placeholders, placeholderPrMap);
// replace custom placeholders }
const phs = placeholders.get(key); return transformed;
if (phs) { }
for (const placeholder of phs) { function handlePlaceholder(template, key, value, placeholders /* placeholders to apply */, placeholderPrMap /* map to keep replaced placeholder values with their key */) {
const transformer = validateTransformer(placeholder.transformer); let transformed = template.replaceAll(`\${{${key}}}`, value);
if (transformer === null || transformer === void 0 ? void 0 : transformer.pattern) { // replace custom placeholders
const extractedValue = value.replace(transformer.pattern, transformer.target); const phs = placeholders.get(key);
// note: `.replace` will return the full string again if there was no match if (phs) {
if (extractedValue && for (const placeholder of phs) {
(extractedValue !== value || (extractedValue === value && value.match(transformer.pattern)))) { const transformer = validateTransformer(placeholder.transformer);
if (placeholderPrMap) { if (transformer === null || transformer === void 0 ? void 0 : transformer.pattern) {
(0, utils_1.createOrSet)(placeholderPrMap, placeholder.name, extractedValue); const extractedValue = value.replace(transformer.pattern, transformer.target);
} // note: `.replace` will return the full string again if there was no match
transformed = transformed.replaceAll(`\${{${placeholder.name}}}`, extractedValue); if (extractedValue && (extractedValue !== value || (extractedValue === value && value.match(transformer.pattern)))) {
if (core.isDebug()) { if (placeholderPrMap) {
core.debug(` Custom Placeholder successfully matched data - ${extractValues} (${placeholder.name})`); (0, utils_1.createOrSet)(placeholderPrMap, placeholder.name, extractedValue);
}
} }
else if (core.isDebug() && extractedValue === value) { transformed = transformed.replaceAll(`\${{${placeholder.name}}}`, extractedValue);
core.debug(` Custom Placeholder did result in the full original value returned. Skipping. (${placeholder.name})`); if (core.isDebug()) {
core.debug(` Custom Placeholder successfully matched data - ${extractValues} (${placeholder.name})`);
} }
} }
else if (core.isDebug() && extractedValue === value) {
core.debug(` Custom Placeholder did result in the full original value returned. Skipping. (${placeholder.name})`);
}
} }
} }
} }
return transformed; return transformed;
} }
function fillArrayPlaceholders(placeholderMap /* placeholderKey and original value */, key, values) {
for (let i = 0; i < values.length; i++) {
placeholderMap.set(`\${{${key}[${i}]}}`, values[i]);
}
placeholderMap.set(`\${{${key}[*]}}`, values.join(', '));
}
function fillReviewPlaceholders(placeholderMap /* placeholderKey and original value */, parentKey, values) {
var _a;
// retrieve the keys from the CommentInfo object
for (const childKey of Object.keys(pullRequests_1.EMPTY_COMMENT_INFO)) {
for (let i = 0; i < values.length; i++) {
placeholderMap.set(`\${{${parentKey}[${i}].${childKey}}}`, ((_a = values[i][childKey]) === null || _a === void 0 ? void 0 : _a.toLocaleString('en')) || '');
}
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 */) {
let transformed = template; let transformed = template;
for (const [key, values] of placeholderPrMap) { for (const [key, values] of placeholderPrMap) {
@@ -1868,27 +1894,7 @@ function replacePrPlaceholders(template, placeholderPrMap /* map with all pr rel
} }
return transformed; return transformed;
} }
function replaceArrayPlaceholders(template, key, values) { function cleanupPrPlaceholders(template, placeholders /* placeholders to apply */) {
let transformed = template;
for (let i = 0; i < values.length; i++) {
transformed = transformed.replaceAll(`\${{${key}[${i}]}}`, values[i]);
}
transformed = transformed.replaceAll(`\${{${key}[*]}}`, values.join(', '));
return transformed;
}
function replaceReviewPlaceholders(template, parentKey, values) {
var _a;
let transformed = template;
// retrieve the keys from the CommentInfo object
for (const childKey of Object.keys(pullRequests_1.EMPTY_COMMENT_INFO)) {
for (let i = 0; i < values.length; i++) {
transformed = transformed.replaceAll(`\${{${parentKey}[${i}].${childKey}}}`, ((_a = values[i][childKey]) === null || _a === void 0 ? void 0 : _a.toLocaleString('en')) || '');
}
transformed = transformed.replaceAll(`\${{${parentKey}[*].${childKey}}}`, values.map(value => { var _a; return ((_a = value[childKey]) === null || _a === void 0 ? void 0 : _a.toLocaleString('en')) || ''; }).join(', '));
}
return transformed;
}
function cleanupPrPlaceHolders(template, placeholders /* placeholders to apply */) {
let transformed = template; let transformed = template;
for (const [, phs] of placeholders) { for (const [, phs] of placeholders) {
for (const ph of phs) { for (const ph of phs) {
Generated Vendored
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -8
View File
@@ -40,14 +40,7 @@ export interface Transformer extends Regex {
} }
export interface Extractor extends Transformer { export interface Extractor extends Transformer {
on_property?: on_property?: ('title' | 'author' | 'milestone' | 'body' | 'branch')[] | 'title' | 'author' | 'milestone' | 'body' | 'branch' | undefined // retrieve the property to extract the value from
| ('title' | 'author' | 'milestone' | 'body' | 'branch')[]
| 'title'
| 'author'
| 'milestone'
| 'body'
| 'branch'
| 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) 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)
} }
+1 -4
View File
@@ -232,10 +232,7 @@ function attachSpeciaLabels(status: 'open' | 'merged', labels: Set<string>): Set
return labels return labels
} }
const mapPullRequest = ( const mapPullRequest = (pr: PullData | Unpacked<PullsListData>, status: 'open' | 'merged' = 'open'): PullRequestInfo => ({
pr: PullData | Unpacked<PullsListData>,
status: 'open' | 'merged' = 'open'
): PullRequestInfo => ({
number: pr.number, number: pr.number,
title: pr.title, title: pr.title,
htmlURL: pr.html_url, htmlURL: pr.html_url,
+4 -15
View File
@@ -62,10 +62,7 @@ export class ReleaseNotes {
if (mergedPullRequests.length === 0) { if (mergedPullRequests.length === 0) {
core.warning(`⚠️ No pull requests found`) core.warning(`⚠️ No pull requests found`)
return replaceEmptyTemplate( return replaceEmptyTemplate(this.options.configuration.empty_template || DefaultConfiguration.empty_template, this.options)
this.options.configuration.empty_template || DefaultConfiguration.empty_template,
this.options
)
} }
core.startGroup('📦 Build changelog') core.startGroup('📦 Build changelog')
@@ -128,10 +125,7 @@ export class ReleaseNotes {
core.info(`️ Retrieved ${pullRequests.length} PRs for ${owner}/${repo} in date range from API`) core.info(`️ Retrieved ${pullRequests.length} PRs for ${owner}/${repo} in date range from API`)
const prCommits = filterCommits( const prCommits = filterCommits(commits, configuration.exclude_merge_branches || DefaultConfiguration.exclude_merge_branches)
commits,
configuration.exclude_merge_branches || DefaultConfiguration.exclude_merge_branches
)
core.info(`️ Retrieved ${prCommits.length} release commits for ${owner}/${repo}`) core.info(`️ Retrieved ${prCommits.length} release commits for ${owner}/${repo}`)
@@ -181,9 +175,7 @@ export class ReleaseNotes {
}) })
if (baseBranches.length !== 0) { if (baseBranches.length !== 0) {
core.info( core.info(`️ Retrieved ${mergedPullRequests.length} PRs for ${owner}/${repo} filtered by the 'base_branches' configuration.`)
`️ Retrieved ${mergedPullRequests.length} PRs for ${owner}/${repo} filtered by the 'base_branches' configuration.`
)
} }
if (fetchReviewers) { if (fetchReviewers) {
@@ -224,10 +216,7 @@ export class ReleaseNotes {
return [diffInfo, []] return [diffInfo, []]
} }
const prCommits = filterCommits( const prCommits = filterCommits(commits, configuration.exclude_merge_branches || DefaultConfiguration.exclude_merge_branches)
commits,
configuration.exclude_merge_branches || DefaultConfiguration.exclude_merge_branches
)
core.info(`️ Retrieved ${prCommits.length} commits for ${owner}/${repo}`) core.info(`️ Retrieved ${prCommits.length} commits for ${owner}/${repo}`)
+2 -6
View File
@@ -52,9 +52,7 @@ export class Tags {
} }
} }
core.info( core.info(`️ Found ${tagsInfo.length} (fetching max: ${maxTagsToFetch}) tags from the GitHub API for ${owner}/${repo}`)
`️ Found ${tagsInfo.length} (fetching max: ${maxTagsToFetch}) tags from the GitHub API for ${owner}/${repo}`
)
return tagsInfo return tagsInfo
} }
@@ -73,9 +71,7 @@ export class Tags {
tagInfo.date = moment(release.created_at) tagInfo.date = moment(release.created_at)
core.info(`️ Retrieved information about the release associated with ${tagInfo.name} from the GitHub API`) core.info(`️ Retrieved information about the release associated with ${tagInfo.name} from the GitHub API`)
} catch (error) { } catch (error) {
core.info( core.info(`⚠️ No release information found for ${tagInfo.name}, trying to retrieve tag creation time as fallback.`)
`⚠️ No release information found for ${tagInfo.name}, trying to retrieve tag creation time as fallback.`
)
const gitHelper = await createCommandManager(repositoryPath) const gitHelper = await createCommandManager(repositoryPath)
const creationTimeString = await gitHelper.tagCreation(tagInfo.name) const creationTimeString = await gitHelper.tagCreation(tagInfo.name)
const creationTime = moment(creationTimeString) const creationTime = moment(creationTimeString)
+79 -68
View File
@@ -13,6 +13,8 @@ export interface RegexTransformer {
onEmpty?: string | undefined onEmpty?: string | undefined
} }
const EMPTY_MAP = new Map<string, string>()
export function buildChangelog(diffInfo: DiffInfo, prs: PullRequestInfo[], options: ReleaseNotesOptions): string { export function buildChangelog(diffInfo: DiffInfo, prs: PullRequestInfo[], options: ReleaseNotesOptions): string {
// sort to target order // sort to target order
const config = options.configuration const config = options.configuration
@@ -126,9 +128,7 @@ export function buildChangelog(diffInfo: DiffInfo, prs: PullRequestInfo[], optio
const prNum = pr.number const prNum = pr.number
const prLabels = pr.labels const prLabels = pr.labels
const excludeLabels = JSON.stringify(category.exclude_labels) const excludeLabels = JSON.stringify(category.exclude_labels)
core.debug( core.debug(`PR ${prNum} with labels: ${prLabels} excluded from category via exclude label: ${excludeLabels}`)
`PR ${prNum} with labels: ${prLabels} excluded from category via exclude label: ${excludeLabels}`
)
} }
continue // one of the exclude labels matched, skip the PR for this category continue // one of the exclude labels matched, skip the PR for this category
} }
@@ -253,9 +253,9 @@ export function buildChangelog(diffInfo: DiffInfo, prs: PullRequestInfo[], optio
fillAdditionalPlaceholders(options, placeholderMap) fillAdditionalPlaceholders(options, placeholderMap)
let transformedChangelog = config.template || DefaultConfiguration.template let transformedChangelog = config.template || DefaultConfiguration.template
transformedChangelog = replacePlaceholders(transformedChangelog, placeholderMap, placeholders, placeholderPrMap) transformedChangelog = replacePlaceholders(transformedChangelog, EMPTY_MAP, placeholderMap, placeholders, placeholderPrMap)
transformedChangelog = replacePrPlaceholders(transformedChangelog, placeholderPrMap) transformedChangelog = replacePrPlaceholders(transformedChangelog, placeholderPrMap)
transformedChangelog = cleanupPrPlaceHolders(transformedChangelog, placeholders) transformedChangelog = cleanupPrPlaceholders(transformedChangelog, placeholders)
core.info(`️ Filled template`) core.info(`️ Filled template`)
return transformedChangelog return transformedChangelog
} }
@@ -267,7 +267,7 @@ export function replaceEmptyTemplate(template: string, options: ReleaseNotesOpti
} }
const placeholderMap = new Map<string, string>() const placeholderMap = new Map<string, string>()
fillAdditionalPlaceholders(options, placeholderMap) fillAdditionalPlaceholders(options, placeholderMap)
return replacePlaceholders(template, placeholderMap, placeholders) return replacePlaceholders(template, new Map<string, string>(), placeholderMap, placeholders)
} }
function fillAdditionalPlaceholders( function fillAdditionalPlaceholders(
@@ -299,11 +299,8 @@ function fillPrTemplate(
placeholders: Map<string, Placeholder[]> /* placeholders to apply */, 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 */
): string { ): string {
let transformed = replaceArrayPlaceholders(template, 'ASSIGNEES', pr.assignees || []) const arrayPlaceholderMap = new Map<string, string>()
transformed = replaceArrayPlaceholders(transformed, 'REVIEWERS', pr.requestedReviewers || []) fillReviewPlaceholders(arrayPlaceholderMap, 'REVIEWS', pr.reviews || [])
transformed = replaceArrayPlaceholders(transformed, 'APPROVERS', pr.approvedReviewers || [])
transformed = replaceReviewPlaceholders(transformed, 'REVIEWS', pr.reviews || [])
const placeholderMap = new Map<string, string>() const placeholderMap = new Map<string, string>()
placeholderMap.set('NUMBER', pr.number.toString()) placeholderMap.set('NUMBER', pr.number.toString())
placeholderMap.set('TITLE', pr.title) placeholderMap.set('TITLE', pr.title)
@@ -316,49 +313,66 @@ function fillPrTemplate(
placeholderMap.set('LABELS', [...pr.labels]?.filter(l => !l.startsWith('--rcba-'))?.join(', ') || '') placeholderMap.set('LABELS', [...pr.labels]?.filter(l => !l.startsWith('--rcba-'))?.join(', ') || '')
placeholderMap.set('MILESTONE', pr.milestone || '') placeholderMap.set('MILESTONE', pr.milestone || '')
placeholderMap.set('BODY', pr.body) placeholderMap.set('BODY', pr.body)
fillArrayPlaceholders(arrayPlaceholderMap, 'ASSIGNEES', pr.assignees || [])
placeholderMap.set('ASSIGNEES', pr.assignees?.join(', ') || '') placeholderMap.set('ASSIGNEES', pr.assignees?.join(', ') || '')
fillArrayPlaceholders(arrayPlaceholderMap, 'REVIEWERS', pr.requestedReviewers || [])
placeholderMap.set('REVIEWERS', pr.requestedReviewers?.join(', ') || '') placeholderMap.set('REVIEWERS', pr.requestedReviewers?.join(', ') || '')
fillArrayPlaceholders(arrayPlaceholderMap, 'APPROVERS', pr.approvedReviewers || [])
placeholderMap.set('APPROVERS', pr.approvedReviewers?.join(', ') || '') placeholderMap.set('APPROVERS', pr.approvedReviewers?.join(', ') || '')
placeholderMap.set('BRANCH', pr.branch || '') placeholderMap.set('BRANCH', pr.branch || '')
placeholderMap.set('BASE_BRANCH', pr.baseBranch) placeholderMap.set('BASE_BRANCH', pr.baseBranch)
return replacePlaceholders(transformed, placeholderMap, placeholders, placeholderPrMap) return replacePlaceholders(template, arrayPlaceholderMap, placeholderMap, placeholders, placeholderPrMap)
} }
function replacePlaceholders( function replacePlaceholders(
template: string, template: string,
arrayPlaceholderMap: Map<string, string> /* arrayPlaceholderKey and original value */,
placeholderMap: Map<string, string> /* placeholderKey and original value */, placeholderMap: Map<string, string> /* placeholderKey and original value */,
placeholders: Map<string, Placeholder[]> /* placeholders to apply */, 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 */
): string { ): string {
let transformed = template let transformed = template
// replace array placeholders first
for (const [key, value] of arrayPlaceholderMap) {
transformed = handlePlaceholder(transformed, key, value, placeholders, placeholderPrMap)
}
// replace traditional placeholders
for (const [key, value] of placeholderMap) { for (const [key, value] of placeholderMap) {
transformed = transformed.replaceAll(`\${{${key}}}`, value) transformed = handlePlaceholder(transformed, key, value, placeholders, placeholderPrMap)
}
// replace custom placeholders return transformed
const phs = placeholders.get(key) }
if (phs) {
for (const placeholder of phs) {
const transformer = validateTransformer(placeholder.transformer)
if (transformer?.pattern) {
const extractedValue = value.replace(transformer.pattern, transformer.target)
// note: `.replace` will return the full string again if there was no match
if (
extractedValue &&
(extractedValue !== value || (extractedValue === value && value.match(transformer.pattern)))
) {
if (placeholderPrMap) {
createOrSet(placeholderPrMap, placeholder.name, extractedValue)
}
transformed = transformed.replaceAll(`\${{${placeholder.name}}}`, extractedValue)
if (core.isDebug()) { function handlePlaceholder(
core.debug(` Custom Placeholder successfully matched data - ${extractValues} (${placeholder.name})`) template: string,
} key: string,
} else if (core.isDebug() && extractedValue === value) { value: string,
core.debug( placeholders: Map<string, Placeholder[]> /* placeholders to apply */,
` Custom Placeholder did result in the full original value returned. Skipping. (${placeholder.name})` placeholderPrMap?: Map<string, string[]> /* map to keep replaced placeholder values with their key */
) ): string {
let transformed = template.replaceAll(`\${{${key}}}`, value)
// replace custom placeholders
const phs = placeholders.get(key)
if (phs) {
for (const placeholder of phs) {
const transformer = validateTransformer(placeholder.transformer)
if (transformer?.pattern) {
const extractedValue = value.replace(transformer.pattern, transformer.target)
// note: `.replace` will return the full string again if there was no match
if (extractedValue && (extractedValue !== value || (extractedValue === value && value.match(transformer.pattern)))) {
if (placeholderPrMap) {
createOrSet(placeholderPrMap, placeholder.name, extractedValue)
} }
transformed = transformed.replaceAll(`\${{${placeholder.name}}}`, extractedValue)
if (core.isDebug()) {
core.debug(` Custom Placeholder successfully matched data - ${extractValues} (${placeholder.name})`)
}
} else if (core.isDebug() && extractedValue === value) {
core.debug(` Custom Placeholder did result in the full original value returned. Skipping. (${placeholder.name})`)
} }
} }
} }
@@ -366,6 +380,34 @@ function replacePlaceholders(
return transformed return transformed
} }
function fillArrayPlaceholders(
placeholderMap: Map<string, string> /* placeholderKey and original value */,
key: string,
values: string[]
): void {
for (let i = 0; i < values.length; i++) {
placeholderMap.set(`\${{${key}[${i}]}}`, values[i])
}
placeholderMap.set(`\${{${key}[*]}}`, values.join(', '))
}
function fillReviewPlaceholders(
placeholderMap: Map<string, string> /* placeholderKey and original value */,
parentKey: string,
values: CommentInfo[]
): void {
// retrieve the keys from the CommentInfo object
for (const childKey of Object.keys(EMPTY_COMMENT_INFO)) {
for (let i = 0; i < values.length; i++) {
placeholderMap.set(`\${{${parentKey}[${i}].${childKey}}}`, values[i][childKey as keyof CommentInfo]?.toLocaleString('en') || '')
}
placeholderMap.set(
`\${{${parentKey}[*].${childKey}}}`,
values.map(value => value[childKey as keyof CommentInfo]?.toLocaleString('en') || '').join(', ')
)
}
}
function replacePrPlaceholders( function replacePrPlaceholders(
template: string, 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 */
@@ -380,38 +422,7 @@ function replacePrPlaceholders(
return transformed return transformed
} }
function replaceArrayPlaceholders(template: string, key: string, values: string[]): string { function cleanupPrPlaceholders(template: string, placeholders: Map<string, Placeholder[]> /* placeholders to apply */): string {
let transformed = template
for (let i = 0; i < values.length; i++) {
transformed = transformed.replaceAll(`\${{${key}[${i}]}}`, values[i])
}
transformed = transformed.replaceAll(`\${{${key}[*]}}`, values.join(', '))
return transformed
}
function replaceReviewPlaceholders(template: string, parentKey: string, values: CommentInfo[]): string {
let transformed = template
// retrieve the keys from the CommentInfo object
for (const childKey of Object.keys(EMPTY_COMMENT_INFO)) {
for (let i = 0; i < values.length; i++) {
transformed = transformed.replaceAll(
`\${{${parentKey}[${i}].${childKey}}}`,
values[i][childKey as keyof CommentInfo]?.toLocaleString('en') || ''
)
}
transformed = transformed.replaceAll(
`\${{${parentKey}[*].${childKey}}}`,
values.map(value => value[childKey as keyof CommentInfo]?.toLocaleString('en') || '').join(', ')
)
}
return transformed
}
function cleanupPrPlaceHolders(
template: string,
placeholders: Map<string, Placeholder[]> /* placeholders to apply */
): string {
let transformed = template let transformed = template
for (const [, phs] of placeholders) { for (const [, phs] of placeholders) {
for (const ph of phs) { for (const ph of phs) {