Skip to content

Commit

Permalink
Update MongoDB-Tools workflow and script
Browse files Browse the repository at this point in the history
  • Loading branch information
Utesgui authored Apr 30, 2024
1 parent 5eb26eb commit 4495eb7
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: MongoDB.Shell PR
name: MongoDB-Tools PR
# based off of https://github.com/nushell/nushell/blob/main/.github/workflows/winget-submission.yml

on:
workflow_dispatch:
schedule:
- cron: 3 0/4 * * *
env:
PackageName: MongoDB.Shell
WebsiteURL: https://www.mongodb.com/try/download/shell
PackageName: MongoDB-Tools
WebsiteURL: https://www.mongodb.com/try/download/tools

jobs:
winget:
Expand Down
96 changes: 96 additions & 0 deletions Scripts/Update-MongoDB-Tools.ps1
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."
}
}
}

0 comments on commit 4495eb7

Please sign in to comment.