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

Fix prerelease string getting removed and add $COMPLETE version variable #918

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Expand Up @@ -162,11 +162,12 @@ You can use any of the following variables in your `template`, `name-template` a

You can use any of the following variables in `version-template` to format the `$NEXT_{PATCH,MINOR,MAJOR}_VERSION` variables:

| Variable | Description |
| -------- | ------------------------- |
| `$PATCH` | The patch version number. |
| `$MINOR` | The minor version number. |
| `$MAJOR` | The major version number. |
| Variable | Description |
| ----------- | ------------------------------------------------------------ |
| `$PATCH` | The patch version number. |
| `$MINOR` | The minor version number. |
| `$MAJOR` | The major version number. |
| `$COMPLETE` | The complete version string (including any prerelease info). |

## Version Resolver

Expand Down
24 changes: 19 additions & 5 deletions lib/versions.js
Expand Up @@ -5,16 +5,20 @@ const splitSemVer = (input, versionKey = 'version') => {
return null
}

const version = input.inc
? semver.inc(input[versionKey], input.inc, true)
: semver.parse(input[versionKey])
let version
if (input.inc) {
version = semver.inc(input[versionKey], input.inc, true)
} else {
version = input[versionKey].version
}
tjenkinson marked this conversation as resolved.
Show resolved Hide resolved

return {
...input,
version,
$MAJOR: semver.major(version),
$MINOR: semver.minor(version),
$PATCH: semver.patch(version),
$COMPLETE: version,
}
}

Expand Down Expand Up @@ -81,14 +85,24 @@ const getTemplatableVersion = (input) => {
return templatableVersion
}

const toSemver = (version) => {
const result = semver.parse(version)
if (result) {
return result
}

// doesn't handle prerelease
return semver.coerce(version)
}

const coerceVersion = (input) => {
if (!input) {
return null
}

return typeof input === 'object'
? semver.coerce(input.tag_name) || semver.coerce(input.name)
: semver.coerce(input)
? toSemver(input.tag_name) || toSemver(input.name)
: toSemver(input)
}

module.exports.getVersionInfo = (
Expand Down
15 changes: 10 additions & 5 deletions test/versions.test.js
Expand Up @@ -131,7 +131,8 @@ describe('versions', () => {
tag_name: 'v10.0.3-alpha',
name: 'Some release',
},
'$MAJOR.$MINOR.$PATCH'
'$MAJOR.$MINOR.$PATCH',
'v10.0.3-alpha'
)

expect(versionInfo.$NEXT_MAJOR_VERSION.version).toEqual('11.0.0')
Expand All @@ -145,6 +146,10 @@ describe('versions', () => {
expect(versionInfo.$NEXT_MAJOR_VERSION_PATCH.version).toEqual('11.0.0')
expect(versionInfo.$NEXT_MAJOR_VERSION_PATCH.template).toEqual('$PATCH')
expect(versionInfo.$NEXT_MINOR_VERSION.version).toEqual('10.1.0')
expect(versionInfo.$NEXT_PATCH_VERSION.version).toEqual('10.0.3')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was previously wrong. The next patch version after v10.0.3-alpha is actually 10.0.3 not 10.0.4, but it was becoming 10.0.4 before because 10.0.3-alpha was incorrectly becoming 10.0.3 internally

expect(versionInfo.$INPUT_VERSION.version).toEqual('10.0.3-alpha')
expect(versionInfo.$RESOLVED_VERSION.version).toEqual('10.0.3-alpha')

expect(versionInfo.$NEXT_MINOR_VERSION.template).toEqual(
'$MAJOR.$MINOR.$PATCH'
)
Expand All @@ -154,15 +159,15 @@ describe('versions', () => {
expect(versionInfo.$NEXT_MINOR_VERSION_MINOR.template).toEqual('$MINOR')
expect(versionInfo.$NEXT_MINOR_VERSION_PATCH.version).toEqual('10.1.0')
expect(versionInfo.$NEXT_MINOR_VERSION_PATCH.template).toEqual('$PATCH')
expect(versionInfo.$NEXT_PATCH_VERSION.version).toEqual('10.0.4')
expect(versionInfo.$NEXT_PATCH_VERSION.version).toEqual('10.0.3')
expect(versionInfo.$NEXT_PATCH_VERSION.template).toEqual(
'$MAJOR.$MINOR.$PATCH'
)
expect(versionInfo.$NEXT_PATCH_VERSION_MAJOR.version).toEqual('10.0.4')
expect(versionInfo.$NEXT_PATCH_VERSION_MAJOR.version).toEqual('10.0.3')
expect(versionInfo.$NEXT_PATCH_VERSION_MAJOR.template).toEqual('$MAJOR')
expect(versionInfo.$NEXT_PATCH_VERSION_MINOR.version).toEqual('10.0.4')
expect(versionInfo.$NEXT_PATCH_VERSION_MINOR.version).toEqual('10.0.3')
expect(versionInfo.$NEXT_PATCH_VERSION_MINOR.template).toEqual('$MINOR')
expect(versionInfo.$NEXT_PATCH_VERSION_PATCH.version).toEqual('10.0.4')
expect(versionInfo.$NEXT_PATCH_VERSION_PATCH.version).toEqual('10.0.3')
expect(versionInfo.$NEXT_PATCH_VERSION_PATCH.template).toEqual('$PATCH')
})

Expand Down