Skip to content

Commit

Permalink
Update MongoDB.Server workflow and script
Browse files Browse the repository at this point in the history
  • Loading branch information
Utesgui authored Apr 30, 2024
1 parent 2953771 commit c9f2fc4
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/MongoDB.Server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: MongoDB.Server 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.Server

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


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

Write-Host "Try to update $wingetPackage"

# Follow redirect of https://app.gitbutler.com/downloads/release/windows/x86_64/msi
$url = "https://www.mongodb.com/try/download/community"
# 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?://[^\s]*\.msi' -AllMatches | ForEach-Object { $_.Matches } | ForEach-Object { $_.Value }

# Extract versions from the links
$versions = $links | ForEach-Object { $_ -match '(\d+\.\d+\.\d+(-rc\d*)?)' | Out-Null; $matches[1] }

# Exclude release candidates
$stableVersions = $versions | Where-Object { $_ -notmatch '-rc' }

# Sort the versions and get the latest one
$latestVersion = $stableVersions | Sort-Object {[Version]$_} | Select-Object -Last 1
$latestVersionUrl = $links | Where-Object { $_ -match $latestVersion }


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 c9f2fc4

Please sign in to comment.