import { catchError , map } from "rxjs/operators" ; import { Injectable } from "@angular/core" ; import { Observable , throwError } from "rxjs" ; import { Changelog } from "./changelog" ; import { AlertService } from "../alerts/alert.service" ; import { HttpClient } from "@angular/common/http" ; import { environment } from "../../../environments/environment" ; /**
 * Manage the changelog information and display it to the user
 * on request or automatically on the first visit of a new version after update.
 */ @ Injectable ( ) export class LatestChangesService { private static GITHUB_API = "https://api.github.com/repos/" ; constructor ( private http , private alertService ) { } param currentVersion param previousVersion getChangelogsBetweenVersions ( currentVersion , previousVersion ? ) { return this . getChangelogs ( ( releases ) => this . filterReleasesBetween ( releases , currentVersion , previousVersion ) ) ; } private filterReleasesBetween ( releases , currentVersion , previousVersion ? ) { let relevantReleases ; const releasesUpToCurrentVersion = releases . filter ( ( r ) => r . tag_name <= currentVersion ) ; if ( releasesUpToCurrentVersion . length < 1 ) { return [ ] ; } if ( previousVersion ) { const releasesBackToPreviousVersion = releasesUpToCurrentVersion . filter ( ( r ) => r . tag_name > previousVersion ) ; relevantReleases = releasesBackToPreviousVersion . sort ( ( a , b ) => ( b . tag_name as string ) . localeCompare ( a . tag_name , "en" ) ) ; } else { relevantReleases = [ releasesUpToCurrentVersion [ 0 ] ] ; } return relevantReleases ; } param version param count getChangelogsBeforeVersion ( version , count ) { return this . getChangelogs ( ( releases ) => this . filterReleasesBefore ( releases , version , count ) ) ; } private filterReleasesBefore ( releases , version , count ) { let relevantReleases ; const releasesUpToCurrentVersion = releases . filter ( ( r ) => r . tag_name < version ) ; if ( releasesUpToCurrentVersion . length < 1 ) { return [ ] ; } relevantReleases = releasesUpToCurrentVersion . sort ( ( a , b ) => ( b . tag_name as string ) . localeCompare ( a . tag_name , "en" ) ) ; return relevantReleases . slice ( 0 , count ) ; } param releaseFilter private getChangelogs ( releaseFilter ) { return this . http . get < any [ ] > ( LatestChangesService . GITHUB_API + environment . repositoryId + "/releases" ) . pipe ( map ( excludePrereleases ) , map ( releaseFilter ) , map ( ( relevantReleases ) => relevantReleases . map ( ( r ) => this . parseGithubApiRelease ( r ) ) ) , catchError ( ( error ) => { this . alertService . addWarning ( $localize `Could not load latest changes: ${ error }` ) ; return throwError ( "Could not load latest changes." ) ; } ) ) ; function excludePrereleases ( releases ) { return releases . filter ( ( release ) => ! release . prerelease && ! release . draft ) ; } } private parseGithubApiRelease ( githubResponse ) { const cleanedReleaseNotes = githubResponse . body . replace ( /#{1,2}[^###]*/ , "" ) . replace ( / \(\[\w{7}\]\([^\)]*\)\)/g , "" ) . replace ( /^(\*|\#)* *\.(.*)(\n|\r\n)/gm , "" ) ; return { tag_name : githubResponse . tag_name , name : githubResponse . name , published_at : githubResponse . published_at , body : cleanedReleaseNotes , } ; } }