From 1f925ff754010240b440757e03e6d30a450bc1af Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 9 Aug 2021 08:52:30 +0200 Subject: [PATCH] fix(changelog): remove irrelevant versions and details (#939) excludes prerelease versions removes lines marked as hidden by starting with . fixes #925 --- .../latest-changes/latest-changes.module.ts | 4 ++ .../latest-changes.service.spec.ts | 59 +++++++++++++++++++ .../latest-changes/latest-changes.service.ts | 33 ++++++++--- 3 files changed, 87 insertions(+), 9 deletions(-) diff --git a/src/app/core/latest-changes/latest-changes.module.ts b/src/app/core/latest-changes/latest-changes.module.ts index ff42cbaaff..bc1d5c3bd8 100644 --- a/src/app/core/latest-changes/latest-changes.module.ts +++ b/src/app/core/latest-changes/latest-changes.module.ts @@ -38,6 +38,10 @@ import { LatestChangesService } from "./latest-changes.service"; * Displaying app version and changelog information to the user * through components that can be used in other templates * as well as automatic popups on updates (see {@link UpdateManagerService}, {@link LatestChangesService}). + * + * Changelogs are dynamically loaded from GitHub Releases through the GitHub API. + * pre-releases are excluded and individual lines in the body can be hidden by starting + * text (after markdown characters) with a ".". */ @NgModule({ imports: [ diff --git a/src/app/core/latest-changes/latest-changes.service.spec.ts b/src/app/core/latest-changes/latest-changes.service.spec.ts index c730323c29..47e6e404ec 100644 --- a/src/app/core/latest-changes/latest-changes.service.spec.ts +++ b/src/app/core/latest-changes/latest-changes.service.spec.ts @@ -47,6 +47,13 @@ describe("LatestChangesService", () => { body: "A", published_at: "2018-01-01", }, + { + name: "prerelease 1", + tag_name: "1.0-rc.1", + prerelease: true, + body: "A", + published_at: "2018-01-01", + }, ]; beforeEach(() => { @@ -122,4 +129,56 @@ describe("LatestChangesService", () => { } ); }); + + it("should not include prereleases", (done) => { + spyOn(http, "get").and.returnValue(of(testReleases)); + + service.getChangelogsBeforeVersion("1.1", 10).subscribe((result) => { + expect(result).toEqual([testReleases[2]]); + expect(result[0]["prerelease"]).toBeFalsy(); + expect(result).not.toContain(testReleases[3]); + done(); + }); + }); + + it("should remove lines from release changelog body that are explicitly hidden by starting with a '.'", (done) => { + const testRelease = { + name: "test with notes", + tag_name: "3.0", + body: `changelog +### Bugs +* relevant fix +* .hidden fix +### .Hidden +`, + }; + + spyOn(http, "get").and.returnValue(of([testRelease])); + + service.getChangelogsBetweenVersions("3.0", "2.9").subscribe((result) => { + expect(result[0].tag_name).toBe(testRelease.tag_name); + expect(result[0].body).toBe(`changelog +# Bugs +* relevant fix +`); + done(); + }); + }); + + it("should remove irrelevant details from release changelog body", (done) => { + const testRelease = { + name: "test with notes", + tag_name: "3.0", + body: + "* fix ([e03dcca](https://github.com/Aam-Digital/ndb-core/commit/e03dcca7d89e584b8f08cc7fe30621c1ad428dba))", + }; + + spyOn(http, "get").and.returnValue(of([testRelease])); + + service.getChangelogsBetweenVersions("3.0", "2.9").subscribe((result) => { + expect(result[0].tag_name).toBe(testRelease.tag_name); + expect(result[0].body).toBe("* fix"); + done(); + }); + }); }); diff --git a/src/app/core/latest-changes/latest-changes.service.ts b/src/app/core/latest-changes/latest-changes.service.ts index 5bd1eb54cf..786c1f005a 100644 --- a/src/app/core/latest-changes/latest-changes.service.ts +++ b/src/app/core/latest-changes/latest-changes.service.ts @@ -119,6 +119,7 @@ export class LatestChangesService { LatestChangesService.GITHUB_API + environment.repositoryId + "/releases" ) .pipe( + map(excludePrereleases), map(releaseFilter), map((relevantReleases) => relevantReleases.map((r) => this.parseGithubApiRelease(r)) @@ -130,23 +131,37 @@ export class LatestChangesService { return throwError("Could not load latest changes."); }) ); + + function excludePrereleases(releases: any[]): Changelog[] { + return releases.filter( + (release) => !release.prerelease && !release.draft + ); + } } private parseGithubApiRelease(githubResponse: any): Changelog { - const releaseNotesWithoutHeading = githubResponse.body.replace( - /#{1,2}[^###]*/, - "" - ); - const releaseNotesWithoutCommitRefs = releaseNotesWithoutHeading.replace( - / \(\[\w{7}\]\([^\)]*\)\)/g, - "" - ); + const cleanedReleaseNotes = githubResponse.body + .replace( + // remove heading + /#{1,2}[^###]*/, + "" + ) + .replace( + // remove commit refs + / \(\[\w{7}\]\([^\)]*\)\)/g, + "" + ) + .replace( + // remove lines starting with "." after markdown characters + /^(\*|\#)* *\.(.*)(\n|\r\n)/gm, + "" + ); return { tag_name: githubResponse.tag_name, name: githubResponse.name, published_at: githubResponse.published_at, - body: releaseNotesWithoutCommitRefs, + body: cleanedReleaseNotes, }; } }