-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update MongoDB-Tools workflow and script
- Loading branch information
Showing
2 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
.github/workflows/MongoDB.Shell.yml → .github/workflows/MongoDB-Tools.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
if ($Env:GITHUB_TOKEN) { | ||
Write-Host 'GITHUB_TOKEN detected' | ||
$gitToken = ${Env:GITHUB_TOKEN} | ||
} | ||
else { | ||
Write-Host 'GITHUB_TOKEN not detected' | ||
exit 1 | ||
} | ||
|
||
#$wingetPackage = ${Env:PackageName} | ||
$url = ${Env:WebsiteURL} | ||
|
||
$PackageMapping = @{ | ||
"MongoDB.Shell" = "mongosh" | ||
"MongoDB.Compass.Community" = "mongodb-compass" | ||
"MongoDB.Compass.Full" = "mongodb-compass" | ||
"MongoDB.DatabaseTools" = "mongodb-database-tools" | ||
"MongoDB.Compass.Isolated" = "mongodb-compass-isolated" | ||
"MongoDB.Compass.Readonly" = "mongodb-compass-readonly" | ||
"MongoDB.MongoDBCLI" = "mongocli" | ||
"MongoDB.MongoDBAtlasCLI" = "mongodb-atlas-cli" | ||
} | ||
|
||
Write-Host "Try to update MongoDB Tools" | ||
|
||
# Download the webpage | ||
$website = Invoke-WebRequest -Uri $url | ||
|
||
# Extract the content of the webpage | ||
$content = $website.Content | ||
|
||
# Find all strings that look like links and end with .msi | ||
$links = $content | Select-String -Pattern 'https?://[^"]+' -AllMatches | % { $_.Matches } | % { $_.Value } | ||
$msilinks = $links | Select-String -Pattern 'https?://[^\s]*\.msi' -AllMatches | ForEach-Object { $_.Matches } | ForEach-Object { $_.Value } | ||
|
||
foreach ($wingetPackage in $PackageMapping.Keys){ | ||
$PackageFilter = $PackageMapping[$wingetPackage] | ||
$Packagelinks = $msilinks | Select-String -Pattern "https?://[^\s]*$PackageFilter[^\s]*\.msi" -AllMatches | ForEach-Object { $_.Matches } | ForEach-Object { $_.Value }| Where-Object { $_ -notmatch "$PackageFilter-isolated|$PackageFilter-readonly" } | ||
# Extract versions from the links | ||
$versions = $Packagelinks | ForEach-Object { $_ -match '(\d+\.\d+\.\d+(-rc\d*|-beta\d*)?)' | Out-Null; $matches[1] } | ||
|
||
# Exclude release candidates | ||
$stableVersions = $versions | Where-Object { $_ -notmatch '(-rc|beta)' } | ||
|
||
# Sort the versions and get the latest one | ||
$latestVersion = $stableVersions | Sort-Object {[Version]$_} | Select-Object -Last 1 | ||
$latestVersionUrl = $Packagelinks | Where-Object { $_ -match $latestVersion } | ||
Write-Host "Version found: $PackageFilter $latestVersion. URL: $latestVersionUrl" | ||
|
||
|
||
Write-Host "Version found: $latestVersion" | ||
|
||
$prMessage = "Update version: $wingetPackage version $latestVersion" | ||
|
||
$foundMessage, $textVersion, $separator, $wingetVersions = winget search --id $wingetPackage --source winget --versions | ||
|
||
# Check for existing versions in winget | ||
if ($wingetVersions -contains $latestVersion) { | ||
Write-Output "Latest version of $wingetPackage $latestVersion is already present in winget." | ||
} | ||
else { | ||
# Check for existing PRs | ||
Write-Host "Fetching existing PRs" | ||
#gh pr list --search "$($wingetPackage) $($latestVersion) in:title draft:false" --state 'open' --json 'title,url' --repo 'microsoft/winget-pkgs' | ConvertFrom-Json | ||
$ExistingOpenPRs = gh pr list --search "$($wingetPackage) $($latestVersion) in:title draft:false" --state 'open' --json 'title,url' --repo 'microsoft/winget-pkgs' | ConvertFrom-Json | ||
#gh pr list --search "$($wingetPackage) $($latestVersion) in:title draft:false" --state 'merged' --json 'title,url' --repo 'microsoft/winget-pkgs' | ConvertFrom-Json | ||
$ExistingMergedPRs = gh pr list --search "$($wingetPackage) $($latestVersion) in:title draft:false" --state 'merged' --json 'title,url' --repo 'microsoft/winget-pkgs' | ConvertFrom-Json | ||
|
||
$ExistingPRs = @($ExistingOpenPRs) + @($ExistingMergedPRs) | ||
|
||
# TODO Check if PR is already merged, if so exit | ||
|
||
# TODO if PR from us is already open, update PR with new version | ||
|
||
# TODO if PR is closed, not from us and no PR got merged, create new PR | ||
|
||
# TODO if PR is closed, from us and no PR got merged, throw error | ||
|
||
if ($ExistingPRs.Count -gt 0) { | ||
Write-Output "$foundMessage" | ||
$ExistingPRs | ForEach-Object { | ||
Write-Output "Found existing PR: $($_.title)" | ||
Write-Output "-> $($_.url)" | ||
} | ||
} | ||
elseif ($wingetVersions -and ($wingetVersions -notmatch $latestVersion)) { | ||
Write-Host "Open PR for update" | ||
Invoke-WebRequest https://aka.ms/wingetcreate/latest -OutFile wingetcreate.exe | ||
.\wingetcreate.exe update $wingetPackage -s -v $latestVersion -u "$latestVersionUrl" --prtitle $prMessage -t $gitToken | ||
} | ||
else { | ||
Write-Output "$foundMessage" | ||
Write-Output "No existing PRs found. Check why wingetcreate has not run." | ||
} | ||
} | ||
} |