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 4495eb7 commit c200c59
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 11 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/MongoDB.Compass.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: MongoDB.Compass 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.Compass
WebsiteURL: https://www.mongodb.com/try/download/tools

jobs:
winget:
name: Publish winget package
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
path: Utesgui
- name: Install winget
uses: Cyberboss/install-winget@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Update package
id: get_version
working-directory: Utesgui
env:
GITHUB_TOKEN: ${{ secrets.WINGET_PAT }}
WINGET_PKGS_FORK_REPO: ${{ vars.WINGET_PKGS_FORK_REPO }}
run: .\Scripts\Update-${{ env.PackageName }}.ps1


33 changes: 33 additions & 0 deletions .github/workflows/MongoDB.DatabaseTools.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: MongoDB.Compass 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.Compass
WebsiteURL: https://www.mongodb.com/try/download/tools

jobs:
winget:
name: Publish winget package
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
path: Utesgui
- name: Install winget
uses: Cyberboss/install-winget@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Update package
id: get_version
working-directory: Utesgui
env:
GITHUB_TOKEN: ${{ secrets.WINGET_PAT }}
WINGET_PKGS_FORK_REPO: ${{ vars.WINGET_PKGS_FORK_REPO }}
run: .\Scripts\Update-${{ env.PackageName }}.ps1


34 changes: 34 additions & 0 deletions .github/workflows/MongoDB.MongoDBAtlasCLI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: MongoDB.MongoDBAtlasCLI 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.MongoDBAtlasCLI
WebsiteURL: https://www.mongodb.com/try/download/tools
PackageFilter: mongodb-atlas-cli

jobs:
winget:
name: Publish winget package
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
path: Utesgui
- name: Install winget
uses: Cyberboss/install-winget@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Update package
id: get_version
working-directory: Utesgui
env:
GITHUB_TOKEN: ${{ secrets.WINGET_PAT }}
WINGET_PKGS_FORK_REPO: ${{ vars.WINGET_PKGS_FORK_REPO }}
run: .\Scripts\Update-${{ env.PackageName }}.ps1


34 changes: 34 additions & 0 deletions .github/workflows/MongoDB.MongoDBCLI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: MongoDB.MongoDBCLI 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.MongoDBCLI
WebsiteURL: https://www.mongodb.com/try/download/tools
PackageFilter: mongocli

jobs:
winget:
name: Publish winget package
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
path: Utesgui
- name: Install winget
uses: Cyberboss/install-winget@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Update package
id: get_version
working-directory: Utesgui
env:
GITHUB_TOKEN: ${{ secrets.WINGET_PAT }}
WINGET_PKGS_FORK_REPO: ${{ vars.WINGET_PKGS_FORK_REPO }}
run: .\Scripts\Update-${{ env.PackageName }}.ps1


2 changes: 1 addition & 1 deletion .github/workflows/MongoDB.Server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- cron: 3 0/4 * * *
env:
PackageName: MongoDB.Server
WebsiteURL: https://www.mongodb.com/try/download/shell
WebsiteURL: https://www.mongodb.com/try/download/community

jobs:
winget:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: MongoDB-Tools PR
name: MongoDB.Shell 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-Tools
PackageName: MongoDB.Shell
WebsiteURL: https://www.mongodb.com/try/download/tools

jobs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ else {
$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"
Expand All @@ -45,8 +40,14 @@ $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: $PackageFilter $latestVersion. URL: $latestVersionUrl"

# Bring $latestVersion in correct format x.x.x.x
# Check if $latestVersion is in the x.x.x format
if ($latestVersion -match '^\d+\.\d+\.\d+$') {
# Append .0 to $latestVersion
$latestVersion = "$latestVersion.0"
}

Write-Host "Version found: $latestVersion"

Expand Down
93 changes: 93 additions & 0 deletions Scripts/Update-MongoDB.DatabaseTools.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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}

# Placeholder for the release notes URL
$releaseNotesUrl = "https://www.mongodb.com/docs/database-tools/release-notes/database-tools-changelog/#{version}-changelog"

$PackageMapping = @{
"MongoDB.DatabaseTools" = "mongodb-database-tools"
}

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"
$actualReleaseNotesUrl = $releaseNotesUrl -replace "{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."
}
}
}
87 changes: 87 additions & 0 deletions Scripts/Update-MongoDB.MongoDBAtlasCLI.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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}
$PackageFilter = ${Env:PackageFilter}


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."
}
}
}
Loading

0 comments on commit c200c59

Please sign in to comment.