|
1 | 1 | $ErrorActionPreference = "Stop" |
2 | 2 | Set-StrictMode -Version 2.0 |
3 | 3 |
|
4 | | -function CleanPackage { |
| 4 | +function UnlistNightlies { |
5 | 5 | param([string] $Package) |
6 | 6 | [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13 |
7 | | - $xml = Invoke-WebRequest -Uri "https://www.powershellgallery.com/api/v2/Search()?`$orderby=Id&`$skip=0&`$top=50&searchTerm='$Package'&targetFramework=''&includePrerelease=true" |
8 | | - |
9 | | - $result = [xml] $xml |
10 | | - |
11 | | - # There are other packages not owned by us that contain this string. |
12 | | - $entries = $result.feed.entry | Where-Object{$_.properties.Id -eq "PnP.PowerShell"} |
| 7 | + |
| 8 | + $entries = GetEntries "https://www.powershellgallery.com/api/v2/FindPackagesById()?id='PnP.PowerShell'" |
13 | 9 |
|
14 | | - $sortedEntries = $entries | Sort-Object -Property @{Expression = {[System.Management.Automation.SemanticVersion]::Parse($_.properties.version)}; Descending=$false} | Where-Object {[System.Management.Automation.SemanticVersion]::Parse($_.properties.version).PreReleaseLabel -eq "nightly"} |
15 | | - $releasedEntries = $entries.Where({[System.Management.Automation.SemanticVersion]::Parse($_.properties.version).PreReleaseLabel -ne "nightly"} ); |
16 | | - # keep last 10 |
17 | | - $entriesToKeep = ($sortedEntries | Select-Object -Last 10) + $releasedEntries |
| 10 | + $sorted = $entries | Sort-Object -Property Version -Descending |
| 11 | + |
| 12 | + # Get the nightly releases |
| 13 | + $nightlies = $sorted | Where-Object { $null -ne $_.Version.PreReleaseLabel } |
18 | 14 |
|
| 15 | + # mark the latest 50 nightlies as unlisted |
| 16 | + $tounlist = $nightlies | Select-Object -First 50 -Skip 10 |
| 17 | + |
19 | 18 | $key = $("$env:POWERSHELLGALLERY_API_KEY") |
20 | | - foreach($entry in $entries) |
21 | | - { |
22 | | - if(!$entriesToKeep.Contains($entry)) |
23 | | - { |
24 | | - Write-host "Entry to be deleted - $($entry.properties.Version)" |
25 | | - nuget delete "package/$($entry.properties.Id)" $entry.properties.Version -ApiKey $key -Source https://www.powershellgallery.com/api/v2 -NonInteractive |
| 19 | + |
| 20 | + foreach ($entry in $tounlist) { |
| 21 | + Write-host "Entry to be deleted - $($entry.Version.ToString())" |
| 22 | + nuget delete "package/$($entry.Id)" $entry.version.ToString() -ApiKey $key -Source https://www.powershellgallery.com/api/v2 -NonInteractive |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function GetEntries([string] $url) { |
| 27 | + $entries = New-Object System.Collections.Generic.List[System.Object] |
| 28 | + |
| 29 | + $result = Invoke-WebRequest -Uri $url |
| 30 | + $xml = [xml]$result |
| 31 | + |
| 32 | + foreach ($entry in $xml.feed.entry) { |
| 33 | + $newEntry = [PSCustomObject]@{ |
| 34 | + Id = $entry.id |
| 35 | + Version = [System.Management.Automation.SemanticVersion]$entry.properties.version |
26 | 36 | } |
| 37 | + $entries.Add($newEntry) |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + $nextLink = $xml.feed.link | Where-Object { $_.rel -eq "next" } |
| 42 | + |
| 43 | + if ($null -ne $nextLink) { |
| 44 | + $extraEntries = GetEntries $nextLink.href |
| 45 | + $entries.AddRange($extraEntries) |
27 | 46 | } |
| 47 | + return $entries |
28 | 48 | } |
29 | 49 |
|
30 | 50 | Write-host "Starting cleanup old nightlies job" |
31 | | -CleanPackage "PnP.PowerShell" |
| 51 | +UnlistNightlies "PnP.PowerShell" |
0 commit comments