DanGough / Nevergreen

This module is an alternative to Evergreen, and allows you to find the latest version and download URL for various Windows apps. Evergreen uses API queries to obtain its data whereas this module is more focussed on web scraping. This is more prone to breaking when websites are changed, hence the name.
The Unlicense
71 stars 16 forks source link

[New App Request] OpenSSL #71

Open AScott-WWF opened 3 weeks ago

AScott-WWF commented 3 weeks ago

I have written a new NeverGreen script to return each of the latest versions of OpenSSL (3.0.x, 3.1.x, 3.2.x & 3.3.x), (this is predominantly to be used for reporting purposes to give me an idea of new releases to fix security vulnerabilities). The detail is obtained using a webscrape against their downloads page: https://www.openssl.org/source/

N.B. As the downloads are .tar.gz format, I have added this as a valid file type to the $Type attribute in New-NevergreenApp.ps1 Also as I introduced the new ReleaseDate attribute in the https://github.com/DanGough/Nevergreen/issues/65 New issue, I have made use of it in here too.

Here is my script:

# Get-OpenSSL.ps1

$AppName = "OpenSSL"
$ReleaseURL = 'https://www.openssl.org/source/'

$Releases = @(
    @{Type = 'Gz'; Ring ='3.0'; Pattern = '(3\.0\.\d+)'}
    @{Type = 'Gz'; Ring ='3.1'; Pattern = '(3\.1\.\d+)'}
    @{Type = 'Gz'; Ring ='3.2'; Pattern = '(3\.2\.\d+)'}
    @{Type = 'Gz'; Ring ='3.3'; Pattern = '(3\.3\.\d+)'}
)

foreach ($Release in $Releases) {
    $URL = Set-UriPrefix -Uri (Get-Link -Uri $ReleaseURL -MatchProperty href -Pattern $Release.Pattern) -Prefix $ReleaseURL
    [version]$Version = Get-Version -String $URL -Pattern '(\d+\.\d+\.\d+)'

    #Obtain Release Date and convert to "yyyy-MM-dd" format
    try {
        $Content = (Invoke-WebRequest -Uri $ReleaseURL).Content

        $ReleaseDateRegEx = "(?s)(\d{4}-\w{3}-\d{2}).*?$($Release.Pattern)\."

        # Extract the captured groups and display them
        foreach ($match in [regex]::Matches($Content, $ReleaseDateRegEx)) {
            $ReleasedOn = $match.Groups[1].Value  # Date group
        }
    }
    catch {
        Throw "Failed to connect to ($($ReleaseUrl)) with error $_."
        Break
    }
    finally {
        # Define a regular expression pattern to match a date in the format "Year-Month-Day"
        $pattern = "\d{4}[-](?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|January|February|March|April|May|June|July|August|September|October|November|December)[-]\d{1,2}"

        # Use Select-String to find the date in the text
        $matchesfound = $ReleasedOn | Select-String -Pattern $pattern

        # Extract the matched date
        if ($matchesfound) {
            # Parse the date and reformat it to "YYYY-MM-DD"
            $ReleaseDate = (Get-Date $ReleasedOn).ToString("yyyy-MM-dd")
        } else {
            Write-Verbose "Date not found in the text."
            $ReleaseDate = ""
        }
        # Success
        New-NevergreenApp -Name $AppName -Architecture 'multi' -Version $Version -ReleaseDate $ReleaseDate -Uri $URL -Type $Release.Type -Ring $Release.Ring
    }
}