|
| 1 | +function Get-CodebergRepoRelease { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Calls the Codeberg Releases API passed via $Uri, validates the response and returns a formatted object |
| 5 | + Docs: https://codeberg.org/api/swagger |
| 6 | + #> |
| 7 | + [OutputType([System.Management.Automation.PSObject])] |
| 8 | + [CmdletBinding(SupportsShouldProcess = $false)] |
| 9 | + param ( |
| 10 | + [Parameter(Mandatory = $true, Position = 0)] |
| 11 | + [ValidateScript( { |
| 12 | + if ($_ -match "^(https://codeberg.org/api/v1/repos/)([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)(/tags|/releases)") { |
| 13 | + $true |
| 14 | + } |
| 15 | + else { |
| 16 | + throw "'$_' must be in the format 'https://codeberg.org/api/v1/repos/user/repository/releases'. Replace 'user' with the user or organisation and 'repository' with the target repository name." |
| 17 | + } |
| 18 | + })] |
| 19 | + [System.String] $Uri, |
| 20 | + |
| 21 | + [Parameter(Mandatory = $false, Position = 1)] |
| 22 | + [ValidateNotNullOrEmpty()] |
| 23 | + [System.String] $MatchVersion = "(\d+(\.\d+){1,4}).*", |
| 24 | + |
| 25 | + [Parameter(Mandatory = $false, Position = 2)] |
| 26 | + [ValidateNotNullOrEmpty()] |
| 27 | + [System.String] $VersionTag = "tag_name", |
| 28 | + |
| 29 | + [Parameter(Mandatory = $false, Position = 3)] |
| 30 | + [ValidateNotNullOrEmpty()] |
| 31 | + [System.String] $Filter = "\.exe$|\.msi$|\.msp$|\.zip$", |
| 32 | + |
| 33 | + [Parameter(Mandatory = $false, Position = 4)] |
| 34 | + [System.Array] $VersionReplace, |
| 35 | + |
| 36 | + [Parameter()] |
| 37 | + [System.Management.Automation.SwitchParameter] $ReturnVersionOnly |
| 38 | + ) |
| 39 | + |
| 40 | + begin { |
| 41 | + } |
| 42 | + |
| 43 | + process { |
| 44 | + try { |
| 45 | + # Retrieve the releases from the Codeberg API |
| 46 | + # Use TLS for connections |
| 47 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Set TLS to 1.2." |
| 48 | + [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072 |
| 49 | + |
| 50 | + # Invoke the Codeberg releases REST API |
| 51 | + # Note that the API performs rate limiting. |
| 52 | + # https://docs.Codeberg.com/en/free-pro-team@latest/rest/reference/repos#get-the-latest-release |
| 53 | + $params = @{ |
| 54 | + ContentType = "application/json" |
| 55 | + ErrorAction = "Stop" |
| 56 | + MaximumRedirection = 0 |
| 57 | + DisableKeepAlive = $true |
| 58 | + UseBasicParsing = $true |
| 59 | + UserAgent = "codeberg-aaronparker-evergreen" |
| 60 | + Uri = $Uri |
| 61 | + } |
| 62 | + if (Test-ProxyEnv) { |
| 63 | + $params.Proxy = $script:EvergreenProxy |
| 64 | + } |
| 65 | + if (Test-ProxyEnv -Creds) { |
| 66 | + $params.ProxyCredential = $script:EvergreenProxyCreds |
| 67 | + } |
| 68 | + # # If Codeberg_TOKEN or GH_TOKEN exists, let's add that to the API request |
| 69 | + # if (Test-Path -Path "env:Codeberg_TOKEN") { |
| 70 | + # $params.Headers = @{ Authorization = "token $env:Codeberg_TOKEN" } |
| 71 | + # } |
| 72 | + # elseif (Test-Path -Path "env:GH_TOKEN") { |
| 73 | + # $params.Headers = @{ Authorization = "token $env:GH_TOKEN" } |
| 74 | + # } |
| 75 | + |
| 76 | + # Output the parameters when using -Verbose |
| 77 | + foreach ($item in $params.GetEnumerator()) { |
| 78 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Invoke-WebRequest parameter: $($item.name): $($item.value)." |
| 79 | + } |
| 80 | + |
| 81 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Get Codeberg release from: $Uri." |
| 82 | + $release = Invoke-RestMethod @params |
| 83 | + } |
| 84 | + catch { |
| 85 | + throw $_ |
| 86 | + } |
| 87 | + |
| 88 | + if ($null -eq $script:resourceStrings.Properties.Codeberg) { |
| 89 | + Write-Warning -Message "$($MyInvocation.MyCommand): Unable to validate release against Codeberg releases property object because we can't find the module resource." |
| 90 | + } |
| 91 | + else { |
| 92 | + # Validate that $release has the expected properties |
| 93 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Validating Codeberg release object." |
| 94 | + foreach ($item in $release) { |
| 95 | + |
| 96 | + # Compare the Codeberg release object with properties that we expect |
| 97 | + $params = @{ |
| 98 | + ReferenceObject = $script:resourceStrings.Properties.Codeberg |
| 99 | + DifferenceObject = (Get-Member -InputObject $item -MemberType NoteProperty) |
| 100 | + PassThru = $true |
| 101 | + ErrorAction = "Continue" |
| 102 | + } |
| 103 | + $missingProperties = Compare-Object @params |
| 104 | + |
| 105 | + # Throw an error for missing properties |
| 106 | + if ($null -ne $missingProperties) { |
| 107 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Validated successfully." |
| 108 | + } |
| 109 | + else { |
| 110 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Validation failed." |
| 111 | + $missingProperties | ForEach-Object { |
| 112 | + throw [System.Management.Automation.ValidationMetadataException]::New("$($MyInvocation.MyCommand): Property: '$_' missing") |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + # Build and array of the latest release and download URLs |
| 119 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Found $($release.count) release/s." |
| 120 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Found $($release.assets.count) asset/s." |
| 121 | + |
| 122 | + if ($PSBoundParameters.ContainsKey("ReturnVersionOnly")) { |
| 123 | + if ($Uri -match "^*tags$") { |
| 124 | + try { |
| 125 | + # Uri matches tags fo the repo; find the latest tag |
| 126 | + $Version = [RegEx]::Match($release[0].name, $MatchVersion).Captures.Groups[1].Value |
| 127 | + } |
| 128 | + catch { |
| 129 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Failed to match version number as-is, returning: $($release[0].$VersionTag)." |
| 130 | + $Version = $release[0].name |
| 131 | + } |
| 132 | + } |
| 133 | + else { |
| 134 | + try { |
| 135 | + # Uri matches releases for the repo; return just the version string |
| 136 | + $Version = [RegEx]::Match($release[0].$VersionTag, $MatchVersion).Captures.Groups[1].Value |
| 137 | + } |
| 138 | + catch { |
| 139 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Failed to match version number as-is, returning: $($release[0].$VersionTag)." |
| 140 | + $Version = $item.$VersionTag |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if ($PSBoundParameters.ContainsKey("VersionReplace")) { |
| 145 | + # Replace string in version |
| 146 | + $Version = $Version -replace $res.Get.VersionReplace[0], $res.Get.VersionReplace[1] |
| 147 | + } |
| 148 | + |
| 149 | + # Build the output object |
| 150 | + $PSObject = [PSCustomObject] @{ |
| 151 | + Version = $Version |
| 152 | + } |
| 153 | + Write-Output -InputObject $PSObject |
| 154 | + } |
| 155 | + else { |
| 156 | + foreach ($item in $release) { |
| 157 | + foreach ($asset in $item.assets) { |
| 158 | + |
| 159 | + # Filter downloads by matching the RegEx in the manifest. The the RegEx may perform includes and excludes |
| 160 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Match $Filter to $($asset.browser_download_url)." |
| 161 | + if ($asset.browser_download_url -match $Filter) { |
| 162 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Building Windows release output object with: $($asset.browser_download_url)." |
| 163 | + |
| 164 | + # Capture the version string from the specified release tag |
| 165 | + try { |
| 166 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Matching version number against: $($item.$VersionTag)." |
| 167 | + $Version = [RegEx]::Match($item.$VersionTag, $MatchVersion).Captures.Groups[1].Value |
| 168 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Found version number: $Version." |
| 169 | + } |
| 170 | + catch { |
| 171 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Failed to match version number, returning: $($item.$VersionTag)." |
| 172 | + $Version = $item.$VersionTag |
| 173 | + } |
| 174 | + |
| 175 | + if ($PSBoundParameters.ContainsKey("VersionReplace")) { |
| 176 | + # Replace string in version |
| 177 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Replace $($res.Get.VersionReplace[0])." |
| 178 | + $Version = $Version -replace $res.Get.VersionReplace[0], $res.Get.VersionReplace[1] |
| 179 | + } |
| 180 | + |
| 181 | + # Build the output object |
| 182 | + if ((Get-Platform -String $asset.browser_download_url) -eq "Windows") { |
| 183 | + $PSObject = [PSCustomObject] @{ |
| 184 | + Version = $Version |
| 185 | + Date = ConvertTo-DateTime -DateTime $item.created_at -Pattern "MM/dd/yyyy HH:mm:ss" |
| 186 | + Size = $asset.size |
| 187 | + Architecture = Get-Architecture -String $(Split-Path -Path $asset.browser_download_url -Leaf) |
| 188 | + InstallerType = Get-InstallerType -String $asset.browser_download_url |
| 189 | + Type = [System.IO.Path]::GetExtension($asset.browser_download_url).Split(".")[-1] |
| 190 | + URI = $asset.browser_download_url |
| 191 | + } |
| 192 | + Write-Output -InputObject $PSObject |
| 193 | + } |
| 194 | + } |
| 195 | + else { |
| 196 | + Write-Verbose -Message "$($MyInvocation.MyCommand): Skip: $($asset.browser_download_url)." |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments