From e37d2cf97ad9012140e43668f27379616180818b Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Wed, 18 Oct 2023 09:33:55 +0200 Subject: [PATCH] feat: parse multiple releases in changelogs (#3351) --- app.ts | 4 +- src/App.vue | 165 +++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 125 insertions(+), 44 deletions(-) diff --git a/app.ts b/app.ts index dc0a3818ec..18dd022622 100644 --- a/app.ts +++ b/app.ts @@ -1,5 +1,5 @@ import express, { Request, RequestHandler, Response, Router } from 'express' - +import { version } from './package.json' import history from 'connect-history-api-fallback' import cors from 'cors' import csrf from 'csurf' @@ -1155,7 +1155,7 @@ app.post( // update versions to actual ones settings.gateway.versions = { - app: utils.getVersion(), + app: version, // don't use getVersion here as it may include commit sha driver: libVersion, server: serverVersion, } diff --git a/src/App.vue b/src/App.vue index 1adf2d8de9..7f5e4070f1 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1092,6 +1092,44 @@ export default { log.error(error) } }, + async getChangelogs(project, prevTag, nextTag, parseChangelog) { + const changelogs = [] + + try { + const response = await fetch( + `https://api.github.com/repos/zwave-js/${project}/releases` + ) + const data = await response.json() + + let start = false + let maxParse = 10 + + for (const release of data) { + if (release.tag_name === prevTag) { + break + } + + if (release.tag_name === nextTag) { + start = true + } + + if (!start) continue + + if (release.draft || release.prerelease) continue + + changelogs.push(parseChangelog(release, changelogs.length)) + + if (--maxParse === 0) break + + // if last version is not defined just print the last + if (!prevTag) break + } + } catch (error) { + log.error(error) + } + + return changelogs + }, async checkChangelog() { const settings = useBaseStore().gateway @@ -1117,61 +1155,104 @@ export default { if (settings?.disableChangelog) return - if (versions?.app !== this.appInfo.appVersion) { - const current = await this.getRelease( + const { default: md } = await import('markdown-it') + + if (versions?.app !== currentVersion) { + const appChangelogs = await this.getChangelogs( 'zwave-js-ui', - 'v' + currentVersion - ) - const { default: md } = await import('markdown-it') - - current.body = current.body.replace( - new RegExp( - `#+ \\[${currentVersion}\\]\\([^\\)]+\\)`, - 'g' - ), - `## Z-Wave JS UI [${current.tag_name}](https://github.com/zwave-js/zwave-js-ui/releases/tag/${current.tag_name})` + versions?.app ? 'v' + versions?.app : null, + 'v' + currentVersion, + (release, i) => { + release.body = release.body.replace( + new RegExp( + `#+ \\[${release.tag_name.replace( + 'v', + '' + )}\\]\\([^\\)]+\\)`, + 'g' + ), + `${i === 0 ? '# UI\n---\n' : ''}## [${ + release.tag_name + }](https://github.com/zwave-js/zwave-js-ui/releases/tag/${ + release.tag_name + })` + ) + + let changelog = md() + .render(release.body) + .replace('', '
') + + if (i === 0) { + changelog = changelog.replace( + '

', + '

' + ) + } + + return changelog + } ) - let changelog = md() - .render(current.body) - .replace('

', '
') + let changelog = appChangelogs.join('
') - if (this.appInfo.zwaveVersion !== versions?.zwave) { - // get changelog from github latest release - const zwaveLatest = await this.getRelease( + if (this.appInfo.zwaveVersion !== versions?.driver) { + const driverChangelogs = await this.getChangelogs( 'node-zwave-js', - 'v' + this.appInfo.zwaveVersion + versions?.driver ? 'v' + versions?.driver : null, + 'v' + this.appInfo.zwaveVersion, + (release, i) => { + const changelog = md() + .render(release.body) + .replace( + /#(\d+)/g, + '#$1' + ) + + return `${ + i === 0 + ? '

Driver



' + : '' + }

${ + release.tag_name + }


${changelog}
` + } ) - const zwaveChangelog = md() - .render(zwaveLatest.body) - .replace( - /#(\d+)/g, - '#$1' - ) - - changelog += `

Driver ${zwaveLatest.tag_name}


${zwaveChangelog}` + changelog += driverChangelogs.join('') } if (this.appInfo.serverVersion !== versions?.server) { - // get changelog from github latest release - const serverLatest = await this.getRelease( + const serverChangelogs = await this.getChangelogs( 'zwave-js-server', - this.appInfo.serverVersion + versions?.server || null, + this.appInfo.serverVersion, + (release, i) => { + const changelog = md() + .render(release.body) + .replace( + "

What's Changed

", + '

Changes

' + ) + .replace( + /#(\d+)/g, + '#$1' + ) + + return `${ + i === 0 + ? '

Server



' + : '' + }

v${ + release.tag_name + }


${changelog}
` + } ) - const serverChangelog = md() - .render(serverLatest.body) - .replace( - "

What's Changed

", - '

Changes

' - ) - .replace( - /#(\d+)/g, - '#$1' - ) - - changelog += `

Server v${serverLatest.tag_name}


${serverChangelog}` + changelog += serverChangelogs.join('') } // means we never saw the changelog for this version