Skip to content

Commit

Permalink
fix: changelog works again (#1651)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSlimvReal authored Jan 11, 2023
1 parent d4344db commit 15c15f4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
6 changes: 6 additions & 0 deletions src/app/core/latest-changes/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 3 additions & 1 deletion src/app/core/latest-changes/changelog/changelog.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -48,6 +49,7 @@ import { MatButtonModule } from "@angular/material/button";
DatePipe,
MarkdownModule,
NgIf,
MarkdownPageModule,
MatButtonModule,
],
standalone: true,
Expand Down Expand Up @@ -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))
Expand Down
50 changes: 20 additions & 30 deletions src/app/core/latest-changes/latest-changes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,30 @@ export class LatestChangesService {
currentVersion: string,
previousVersion?: string
): Observable<Changelog[]> {
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;
}

/**
Expand All @@ -85,38 +78,35 @@ export class LatestChangesService {
version: string,
count: number
): Observable<Changelog[]> {
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 });
}

/**
* Load release information from GitHub based on a given filter to select relevant releases.
* @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<Changelog[]> {
return this.http
.get<any[]>(
.get<Changelog[]>(
`${LatestChangesService.GITHUB_API}${environment.repositoryId}/releases`,
{ context: new HttpContext().set(AUTH_ENABLED, false) }
)
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/app/core/markdown-page/markdown-page.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -38,6 +38,7 @@ function markedOptionsFactory(): MarkedOptions {
*/
@NgModule({
imports: [
HttpClientModule,
MarkdownModule.forRoot({
loader: HttpClient,
markedOptions: {
Expand Down

0 comments on commit 15c15f4

Please sign in to comment.