From 15c15f41dd17dba42d2784568db6d618d02ff34c Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 11 Jan 2023 21:15:37 +0100 Subject: [PATCH] fix: changelog works again (#1651) --- src/app/core/latest-changes/changelog.ts | 6 +++ .../changelog/changelog.component.ts | 4 +- .../latest-changes/latest-changes.service.ts | 50 ++++++++----------- .../markdown-page/markdown-page.module.ts | 3 +- 4 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/app/core/latest-changes/changelog.ts b/src/app/core/latest-changes/changelog.ts index 5274fcf3b1..9e10a0ed88 100644 --- a/src/app/core/latest-changes/changelog.ts +++ b/src/app/core/latest-changes/changelog.ts @@ -30,4 +30,10 @@ export class Changelog { /** release date */ published_at: string; + + /** whether it is a pre-release */ + prerelease?: boolean; + + /** whether it is a draft */ + draft?: boolean; } diff --git a/src/app/core/latest-changes/changelog/changelog.component.ts b/src/app/core/latest-changes/changelog/changelog.component.ts index d3e13ed882..51fd887f2f 100644 --- a/src/app/core/latest-changes/changelog/changelog.component.ts +++ b/src/app/core/latest-changes/changelog/changelog.component.ts @@ -29,6 +29,7 @@ import { LatestChangesService } from "../latest-changes.service"; import { UntilDestroy, untilDestroyed } from "@ngneat/until-destroy"; import { DatePipe, NgForOf, NgIf } from "@angular/common"; import { FontAwesomeModule } from "@fortawesome/angular-fontawesome"; +import { MarkdownPageModule } from "../../markdown-page/markdown-page.module"; import { MarkdownModule } from "ngx-markdown"; import { MatButtonModule } from "@angular/material/button"; @@ -48,6 +49,7 @@ import { MatButtonModule } from "@angular/material/button"; DatePipe, MarkdownModule, NgIf, + MarkdownPageModule, MatButtonModule, ], standalone: true, @@ -96,7 +98,7 @@ export class ChangelogComponent implements OnInit { } const lastDisplayedVersion = - this.changelogs[this.changelogs.length - 1].tag_name; + this.changelogs[this.changelogs.length - 1]?.tag_name; this.latestChangesService .getChangelogsBeforeVersion(lastDisplayedVersion, 1) .pipe(untilDestroyed(this)) diff --git a/src/app/core/latest-changes/latest-changes.service.ts b/src/app/core/latest-changes/latest-changes.service.ts index 0fc43d691f..7b0a2ea8b7 100644 --- a/src/app/core/latest-changes/latest-changes.service.ts +++ b/src/app/core/latest-changes/latest-changes.service.ts @@ -43,37 +43,30 @@ export class LatestChangesService { currentVersion: string, previousVersion?: string ): Observable { - return this.getChangelogs((releases: any[]) => + return this.getChangelogs((releases) => this.filterReleasesBetween(releases, currentVersion, previousVersion) ); } private filterReleasesBetween( - releases: any[], + releases: Changelog[], currentVersion: string, previousVersion?: string ) { - let relevantReleases; - const releasesUpToCurrentVersion = releases.filter( - (r) => r.tag_name <= currentVersion + (r) => this.compareVersion(r.tag_name, currentVersion) <= 0 ); if (releasesUpToCurrentVersion.length < 1) { return []; } if (previousVersion) { - relevantReleases = releasesUpToCurrentVersion.filter( - (r) => r.tag_name > previousVersion - ); - relevantReleases.sort((a, b) => - (b.tag_name as string).localeCompare(a.tag_name, "en") - ); + return releasesUpToCurrentVersion + .filter((r) => this.compareVersion(r.tag_name, previousVersion) > 0) + .sort((a, b) => this.compareVersion(b.tag_name, a.tag_name)); } else { - relevantReleases = [releasesUpToCurrentVersion[0]]; + return [releasesUpToCurrentVersion[0]]; } - - return relevantReleases; } /** @@ -85,27 +78,24 @@ export class LatestChangesService { version: string, count: number ): Observable { - return this.getChangelogs((releases: any) => + return this.getChangelogs((releases: Changelog[]) => this.filterReleasesBefore(releases, version, count) ); } private filterReleasesBefore( - releases: any[], + releases: Changelog[], version: string, count: number ) { - const releasesUpToCurrentVersion = releases.filter( - (r) => r.tag_name < version - ); - if (releasesUpToCurrentVersion.length < 1) { - return []; - } + return releases + .filter((r) => (version ? r.tag_name < version : true)) + .sort((a, b) => this.compareVersion(b.tag_name, a.tag_name)) + .slice(0, count); + } - releasesUpToCurrentVersion.sort((a, b) => - (b.tag_name as string).localeCompare(a.tag_name, "en") - ); - return releasesUpToCurrentVersion.slice(0, count); + private compareVersion(a: string, b: string) { + return a.localeCompare(b, "en", { numeric: true }); } /** @@ -113,10 +103,10 @@ export class LatestChangesService { * @param releaseFilter Filter function that is selecting relevant objects from the array of GitHub releases */ private getChangelogs( - releaseFilter: (releases: any[]) => any[] + releaseFilter: (releases: Changelog[]) => Changelog[] ): Observable { return this.http - .get( + .get( `${LatestChangesService.GITHUB_API}${environment.repositoryId}/releases`, { context: new HttpContext().set(AUTH_ENABLED, false) } ) @@ -134,14 +124,14 @@ export class LatestChangesService { }) ); - function excludePrereleases(releases: any[]): Changelog[] { + function excludePrereleases(releases: Changelog[]): Changelog[] { return releases.filter( (release) => !release.prerelease && !release.draft ); } } - private parseGithubApiRelease(githubResponse: any): Changelog { + private parseGithubApiRelease(githubResponse: Changelog): Changelog { const cleanedReleaseNotes = githubResponse.body .replace( // remove heading diff --git a/src/app/core/markdown-page/markdown-page.module.ts b/src/app/core/markdown-page/markdown-page.module.ts index af905173b0..c0479a47e3 100644 --- a/src/app/core/markdown-page/markdown-page.module.ts +++ b/src/app/core/markdown-page/markdown-page.module.ts @@ -16,7 +16,7 @@ */ import { NgModule } from "@angular/core"; -import { HttpClient } from "@angular/common/http"; +import { HttpClient, HttpClientModule } from "@angular/common/http"; import { MarkdownModule, MarkedOptions } from "ngx-markdown"; import { MarkedRendererCustom } from "./MarkedRendererCustom"; @@ -38,6 +38,7 @@ function markedOptionsFactory(): MarkedOptions { */ @NgModule({ imports: [ + HttpClientModule, MarkdownModule.forRoot({ loader: HttpClient, markedOptions: {