Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

yulolimum
Copy link

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 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
Author

Choose a reason for hiding this comment

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

Documents option usage.

```

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

Copy link
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 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
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.

const [tag, date] = string.split(DIVIDER)
return {
tag,
date,
title: tag,
version: inferSemver(tag.replace(tagPrefix, ''))
Copy link
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.

const inferSemver = (tag, { tagPrefix, tagParserPattern, tagParserReplacement }) => {
if (!!tagParserPattern && !!tagParserReplacement) {
return tag.replace(new RegExp(tagParserPattern), tagParserReplacement)
}
Copy link
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.


if (!!tagPrefix && tag.startsWith(tagPrefix)) {
tag = tag.replace(new RegExp(`^${tagPrefix}`), '')
}
Copy link
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.

'2.1.0-build.2355',
'0.1.0-build.2345'
])
})
Copy link
Author

Choose a reason for hiding this comment

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

Adds test.

@cookpete
Copy link
Owner

cookpete commented Feb 3, 2022

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

@yulolimum
Copy link
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