Repository: release-drafter/release-drafter
Pull request #918 (2021-08-10 14:50:51)

Fix prerelease string getting removed and add $COMPLETE version variable
Currently if the version is a prerelease the prerelease string is discarded because `semver.coerce` discards it.

With this PR we now use `semver.parse` instead and fall back to coerce.

It also updates `splitSemVer` to always return a string instead of sometimes a `SemVer` instance (which currently becomes a string in `template` due to it having a `toString`) and sometimes string.

Finally it adds a `$COMPELTE` variable that can be used to access the full version string, otherwise it's still not possible to get the prerelease info in the template.

jetersen: @tjenkinson A PR merged resulted in some conflict 😅 Could you please take a look?
tjenkinson: @jetersen done!

Patch:
diff --git a/README.md b/README.md
index 6728df7..3c4b654 100644
--- a/README.md
+++ b/README.md
@@ -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
 
diff --git a/dist/index.js b/dist/index.js
index b748a8e..95a8d86 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -1319,7 +1319,7 @@ const splitSemVer = (input, versionKey = 'version') => {
 
   const version = input.inc
     ? semver.inc(input[versionKey], input.inc, true)
-    : semver.parse(input[versionKey])
+    : input[versionKey].version
 
   return {
     ...input,
@@ -1327,6 +1327,7 @@ const splitSemVer = (input, versionKey = 'version') => {
     $MAJOR: semver.major(version),
     $MINOR: semver.minor(version),
     $PATCH: semver.patch(version),
+    $COMPLETE: version,
   }
 }
 
@@ -1393,14 +1394,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 = (
diff --git a/lib/versions.js b/lib/versions.js
index 7346d70..54dd7d0 100644
--- a/lib/versions.js
+++ b/lib/versions.js
@@ -7,7 +7,7 @@ const splitSemVer = (input, versionKey = 'version') => {
 
   const version = input.inc
     ? semver.inc(input[versionKey], input.inc, true)
-    : semver.parse(input[versionKey])
+    : input[versionKey].version
 
   return {
     ...input,
@@ -15,6 +15,7 @@ const splitSemVer = (input, versionKey = 'version') => {
     $MAJOR: semver.major(version),
     $MINOR: semver.minor(version),
     $PATCH: semver.patch(version),
+    $COMPLETE: version,
   }
 }
 
@@ -81,14 +82,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 = (


Test patch:
diff --git a/test/versions.test.js b/test/versions.test.js
index 1261f10..16a86e8 100644
--- a/test/versions.test.js
+++ b/test/versions.test.js
@@ -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')
@@ -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')
+    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'
     )
@@ -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')
   })