Repository: release-drafter/release-drafter
Pull request #1255 (2022-10-24 14:23:47)

fix(release): strip prefix before comparing version
In documentation, it says 
> A known prefix used to filter release tags. For matching tags, this prefix is stripped before attempting to parse the version. Default: ""

However, while sorting version to retrieve the latest release, the prefix is taking into account and leads to sort regarding `created_at`, not semver

DuMaM: Thanks for this PR,
Do you think it can resolve this issue?
https://github.com/release-drafter/release-drafter/issues/1269
neilime: @DuMaM Yes I think so
neilime: @jetersen need your approval to run the workflow, and your review. Thanks
neilime: @jetersen  :up:  thanks

Patch:
diff --git a/dist/index.js b/dist/index.js
index f0b23c0..9e8840d 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -142893,11 +142893,17 @@ const { getVersionInfo } = __nccwpck_require__(49914)
 const { template } = __nccwpck_require__(47282)
 const { log } = __nccwpck_require__(71911)
 
-const sortReleases = (releases) => {
+const sortReleases = (releases, tagPrefix) => {
   // For semver, we find the greatest release number
   // For non-semver, we use the most recently merged
   try {
-    return releases.sort((r1, r2) => compareVersions(r1.tag_name, r2.tag_name))
+    const tagPrefixRexExp = new RegExp(`^${regexEscape(tagPrefix)}`)
+    return releases.sort((r1, r2) =>
+      compareVersions(
+        r1.tag_name.replace(tagPrefixRexExp, ''),
+        r2.tag_name.replace(tagPrefixRexExp, '')
+      )
+    )
   } catch {
     return releases.sort(
       (r1, r2) => new Date(r1.created_at) - new Date(r2.created_at)
@@ -142948,7 +142954,8 @@ const findReleases = async ({
   const sortedSelectedReleases = sortReleases(
     filteredReleases.filter(
       (r) => !r.draft && (!r.prerelease || includePreReleases)
-    )
+    ),
+    tagPrefix
   )
   const draftRelease = filteredReleases.find((r) => r.draft)
   const lastRelease = sortedSelectedReleases[sortedSelectedReleases.length - 1]
diff --git a/lib/releases.js b/lib/releases.js
index 1f99289..f168637 100644
--- a/lib/releases.js
+++ b/lib/releases.js
@@ -5,11 +5,17 @@ const { getVersionInfo } = require('./versions')
 const { template } = require('./template')
 const { log } = require('./log')
 
-const sortReleases = (releases) => {
+const sortReleases = (releases, tagPrefix) => {
   // For semver, we find the greatest release number
   // For non-semver, we use the most recently merged
   try {
-    return releases.sort((r1, r2) => compareVersions(r1.tag_name, r2.tag_name))
+    const tagPrefixRexExp = new RegExp(`^${regexEscape(tagPrefix)}`)
+    return releases.sort((r1, r2) =>
+      compareVersions(
+        r1.tag_name.replace(tagPrefixRexExp, ''),
+        r2.tag_name.replace(tagPrefixRexExp, '')
+      )
+    )
   } catch {
     return releases.sort(
       (r1, r2) => new Date(r1.created_at) - new Date(r2.created_at)
@@ -60,7 +66,8 @@ const findReleases = async ({
   const sortedSelectedReleases = sortReleases(
     filteredReleases.filter(
       (r) => !r.draft && (!r.prerelease || includePreReleases)
-    )
+    ),
+    tagPrefix
   )
   const draftRelease = filteredReleases.find((r) => r.draft)
   const lastRelease = sortedSelectedReleases[sortedSelectedReleases.length - 1]


Test patch:
diff --git a/test/releases.test.js b/test/releases.test.js
index 8dd8316..3d8e80f 100644
--- a/test/releases.test.js
+++ b/test/releases.test.js
@@ -1,4 +1,4 @@
-const { generateChangeLog } = require('../lib/releases')
+const { generateChangeLog, findReleases } = require('../lib/releases')
 const { DEFAULT_CONFIG } = require('../lib/default-config')
 
 const pullRequests = [
@@ -234,4 +234,46 @@ describe('releases', () => {
       `)
     })
   })
+
+  describe('findReleases', () => {
+    it('should retrieve last release respecting semver, stripped prefix', async () => {
+      const paginate = jest.fn().mockResolvedValue([
+        {
+          tag_name: 'test-1.0.1',
+          target_commitish: 'master',
+          created_at: '2021-06-29T05:45:15Z',
+        },
+        {
+          tag_name: 'test-1.0.0',
+          target_commitish: 'master',
+          created_at: '2022-06-29T05:45:15Z',
+        },
+      ])
+
+      const context = {
+        log: {
+          info: jest.fn(),
+        },
+        repo: jest.fn(),
+        payload: {
+          repository: 'test',
+        },
+        octokit: {
+          paginate,
+          repos: { listReleases: { endpoint: { merge: jest.fn() } } },
+        },
+      }
+      const targetCommitish = 'refs/heads/master'
+      const filterByCommitish = ''
+      const tagPrefix = 'test-'
+
+      const { lastRelease } = await findReleases({
+        context,
+        targetCommitish,
+        filterByCommitish,
+        tagPrefix,
+      })
+      expect(lastRelease.tag_name).toEqual('test-1.0.1')
+    })
+  })
 })