Skip to content

Commit

Permalink
- add ability to trim values prior to inserting them in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
mikepenz authored Dec 9, 2022
1 parent 27d0a0a commit e0b335b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 34 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 🧪

Expand Down
33 changes: 17 additions & 16 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Configuration {
tag_resolver: TagResolver
base_branches: string[]
custom_placeholders?: Placeholder[]
trim_values: boolean
}

export interface Category {
Expand Down Expand Up @@ -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
}
39 changes: 23 additions & 16 deletions src/transform.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
)
)
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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 || [])
Expand All @@ -321,26 +322,27 @@ 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(
template: string,
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
Expand All @@ -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) {
Expand All @@ -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})`)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e0b335b

Please sign in to comment.