Skip to content

Add --tag-parser-pattern & --tag-parser-replacement for unconventional tag coercion into valid semver. - #222

Open
yulolimum wants to merge 8 commits into
cookpete:masterfrom
yulolimum:feature/tag-parsing
Open

Add --tag-parser-pattern & --tag-parser-replacement for unconventional tag coercion into valid semver.#222
yulolimum wants to merge 8 commits into
cookpete:masterfrom
yulolimum:feature/tag-parsing

Conversation

@yulolimum

Copy link
Copy Markdown

Description

This PR adds the ability to pass in a regex capture pattern and a replacement string in order for auto-changelog to properly include, validate, and sort custom tagging approaches.

Stories

  • auto-changelog has the --tag-prefix option to remove a non-semver string from tags but it cannot remove multiple invalid strings.
  • auto-changelog, when including invalid semver tags via --tag-pattern, cannot properly sort said tags in their semver order (e.g. v4.10.1@next appears before v4.2.0@next)

Changes

  • adds --tag-parser-pattern to allow user to supply a regex for capturing semver MAJOR.MINOR.PATCH[-pre-release.BUILD].
  • adds --tag-parser-replacement to allow user to replace the captured values and coerce the tag to valid semver.
  • refactors related code.

Proofs

yulolimum-capture-10-14-2021-tB3jqmkZ

Comment thread README.md
Comment on lines +43 to +44
--tag-parser-pattern [regex] # pattern used to capture values in tags for replacement
--tag-parser-replacement [string] # replaces captures supplied in the --tag-parser-pattern option to create valid semver tags

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documents option usage.

Comment thread README.md
```

Note, both options need to be supplied for this to work. Also note, `$` signs in the replacement string must be escaped.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documents parsing strategies and their differences.

Comment thread src/run.js
Comment on lines +47 to +48
.option('--tag-parser-pattern <regex>', 'pattern used to capture values in tags for replacement')
.option('--tag-parser-replacement <string>', 'replaces captures supplied in the --tag-parser-pattern option to create valid semver tags')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds options to commander.

Comment thread src/tags.js Outdated
tag,
date,
title: tag,
version: inferSemver(tag.replace(tagPrefix, ''))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moves the tagPrefix replacement logic into the inferSemver.

Comment thread src/tags.js
const inferSemver = (tag, { tagPrefix, tagParserPattern, tagParserReplacement }) => {
if (!!tagParserPattern && !!tagParserReplacement) {
return tag.replace(new RegExp(tagParserPattern), tagParserReplacement)
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds the logic for the pattern/replacement options.

Comment thread src/tags.js

if (!!tagPrefix && tag.startsWith(tagPrefix)) {
tag = tag.replace(new RegExp(`^${tagPrefix}`), '')
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are removing the "prefix" via the tagPrefix option, actually check if the tag "starts with" the supply string and only remove it from the beginning.

Comment thread test/tags.js
'2.1.0-build.2355',
'0.1.0-build.2345'
])
})

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds test.

@cookpete

cookpete commented Feb 3, 2022

Copy link
Copy Markdown
Owner

This looks interesting but couldn’t the same be achieved with the existing --tag-pattern option and capture groups?

@yulolimum

Copy link
Copy Markdown
Author

This looks interesting but couldn’t the same be achieved with the existing --tag-pattern option and capture groups?

@cookpete Good question!

From what I remember (it's been a while since I looked at the code), tagPattern is only used to validate whether the tag should be kept or rejected in the filter. What these options do, is they prepare the tag strings before the validation even runs. One could argue, "well why does that matter?" - the answer is semver sort order. If a tagPattern is used to pass the initial filtering for "invalid" semver tags, it doesn't mean that the tags will be sorted correctly using semver. Parsing the tags into the correct semver format will allow the sort to work as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants