|
| 1 | +param( |
| 2 | + [Parameter(Mandatory = $true)] |
| 3 | + [string]$Version, |
| 4 | + |
| 5 | + [Parameter(Mandatory = $true)] |
| 6 | + [string]$ManifestSourcePath, |
| 7 | + |
| 8 | + [Parameter(Mandatory = $true)] |
| 9 | + [string]$ForkOwner, |
| 10 | + |
| 11 | + [Parameter(Mandatory = $true)] |
| 12 | + [string]$RepositoryOwner, |
| 13 | + |
| 14 | + [Parameter(Mandatory = $true)] |
| 15 | + [string]$PackageIdentifier, |
| 16 | + |
| 17 | + [Parameter(Mandatory = $true)] |
| 18 | + [string]$GithubToken |
| 19 | +) |
| 20 | + |
| 21 | +Set-StrictMode -Version Latest |
| 22 | +$ErrorActionPreference = 'Stop' |
| 23 | + |
| 24 | +function Assert-CommandAvailable { |
| 25 | + param([string]$CommandName) |
| 26 | + |
| 27 | + if (-not (Get-Command $CommandName -ErrorAction SilentlyContinue)) { |
| 28 | + throw "Required command '$CommandName' was not found in PATH." |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +Assert-CommandAvailable -CommandName 'git' |
| 33 | +Assert-CommandAvailable -CommandName 'gh' |
| 34 | + |
| 35 | +if (-not (Test-Path -LiteralPath $ManifestSourcePath)) { |
| 36 | + throw "Manifest source path not found: $ManifestSourcePath" |
| 37 | +} |
| 38 | + |
| 39 | +$manifestFiles = Get-ChildItem -LiteralPath $ManifestSourcePath -Recurse -File -Filter '*.yaml' |
| 40 | +if (-not $manifestFiles -or $manifestFiles.Count -eq 0) { |
| 41 | + throw "No winget manifest files found under $ManifestSourcePath" |
| 42 | +} |
| 43 | + |
| 44 | +$scriptRoot = Split-Path -Parent $PSCommandPath |
| 45 | +$projectRoot = Split-Path -Parent $scriptRoot |
| 46 | +$workDir = Join-Path $env:RUNNER_TEMP 'winget-pkgs-work' |
| 47 | + |
| 48 | +if (Test-Path -LiteralPath $workDir) { |
| 49 | + Remove-Item -LiteralPath $workDir -Recurse -Force |
| 50 | +} |
| 51 | + |
| 52 | +$env:GH_TOKEN = $GithubToken |
| 53 | + |
| 54 | +Write-Host 'Cloning winget-pkgs fork...' |
| 55 | +& git clone "https://x-access-token:$GithubToken@github.com/$ForkOwner/winget-pkgs.git" $workDir |
| 56 | +if ($LASTEXITCODE -ne 0) { |
| 57 | + throw 'Failed to clone winget-pkgs fork.' |
| 58 | +} |
| 59 | + |
| 60 | +Push-Location -LiteralPath $workDir |
| 61 | +try { |
| 62 | + & git config user.name 'ThreadPilot Release Bot' |
| 63 | + & git config user.email 'threadpilot-release-bot@users.noreply.github.com' |
| 64 | + |
| 65 | + $branch = "threadpilot-$Version" |
| 66 | + & git checkout -B $branch |
| 67 | + |
| 68 | + $packageParts = $PackageIdentifier.Split('.') |
| 69 | + if ($packageParts.Count -lt 2) { |
| 70 | + throw "PackageIdentifier must contain at least one dot: $PackageIdentifier" |
| 71 | + } |
| 72 | + |
| 73 | + $packagePublisher = $packageParts[0] |
| 74 | + $packageName = $packageParts[1] |
| 75 | + |
| 76 | + if ($RepositoryOwner -ne $packagePublisher) { |
| 77 | + throw "RepositoryOwner '$RepositoryOwner' does not match package publisher '$packagePublisher'." |
| 78 | + } |
| 79 | + |
| 80 | + $firstLetter = $packagePublisher.Substring(0, 1).ToLowerInvariant() |
| 81 | + $targetDir = Join-Path $workDir (Join-Path 'manifests' (Join-Path $firstLetter (Join-Path $packagePublisher (Join-Path $packageName $Version)))) |
| 82 | + |
| 83 | + if (Test-Path -LiteralPath $targetDir) { |
| 84 | + Remove-Item -LiteralPath $targetDir -Recurse -Force |
| 85 | + } |
| 86 | + |
| 87 | + New-Item -ItemType Directory -Path $targetDir -Force | Out-Null |
| 88 | + |
| 89 | + foreach ($file in $manifestFiles) { |
| 90 | + Copy-Item -LiteralPath $file.FullName -Destination (Join-Path $targetDir $file.Name) -Force |
| 91 | + } |
| 92 | + |
| 93 | + & git add $targetDir |
| 94 | + & git status --short |
| 95 | + |
| 96 | + $hasChanges = (& git diff --cached --name-only | Out-String).Trim() |
| 97 | + if ([string]::IsNullOrWhiteSpace($hasChanges)) { |
| 98 | + Write-Host "No manifest changes detected for $Version. Nothing to submit." |
| 99 | + exit 0 |
| 100 | + } |
| 101 | + |
| 102 | + & git commit -m "Add $PackageIdentifier version $Version" |
| 103 | + & git push -u origin $branch --force |
| 104 | + |
| 105 | + $existingPr = (& gh pr list --repo microsoft/winget-pkgs --state open --head "$ForkOwner`:$branch" --json number | Out-String).Trim() |
| 106 | + if (-not [string]::IsNullOrWhiteSpace($existingPr) -and $existingPr -ne '[]') { |
| 107 | + Write-Host "Winget PR already exists for branch $branch." |
| 108 | + exit 0 |
| 109 | + } |
| 110 | + |
| 111 | + $title = "New version: $PackageIdentifier version $Version" |
| 112 | + $body = @" |
| 113 | +Automated submission from ThreadPilot release workflow. |
| 114 | +
|
| 115 | +- Package: $PackageIdentifier |
| 116 | +- Version: $Version |
| 117 | +"@ |
| 118 | + |
| 119 | + & gh pr create --repo microsoft/winget-pkgs --base master --head "$ForkOwner`:$branch" --title $title --body $body |
| 120 | + if ($LASTEXITCODE -ne 0) { |
| 121 | + throw 'Failed to create winget-pkgs PR.' |
| 122 | + } |
| 123 | + |
| 124 | + Write-Host "Winget PR submitted successfully for $PackageIdentifier $Version" |
| 125 | +} |
| 126 | +finally { |
| 127 | + Pop-Location |
| 128 | +} |
0 commit comments