|
| 1 | +function Merge-DevPullRequest { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Merges pull requests in the azure-powershell repository. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Helps merge pull requests in the azure-powershell repo. When using -AllArchivePR, for safety, |
| 8 | + it only supports merging PRs with the "[skip ci]" prefix in the title and created by the |
| 9 | + "azure-powershell-bot" user, which are the archive PRs for generated modules. |
| 10 | + When using -Number, any PR can be merged. |
| 11 | +
|
| 12 | + .PARAMETER Number |
| 13 | + The pull request number(s) to merge. Can be a single number or an array of numbers. |
| 14 | +
|
| 15 | + .PARAMETER AllArchivePR |
| 16 | + Lists all matching PRs (ordered by CreatedAt ascending) and prompts for confirmation before merging. |
| 17 | +
|
| 18 | + .PARAMETER Approve |
| 19 | + Approve the pull request before merging. |
| 20 | +
|
| 21 | + .PARAMETER Force |
| 22 | + Skip confirmation prompts. |
| 23 | +
|
| 24 | + .EXAMPLE |
| 25 | + Merge-DevPullRequest -Approve -Number 28690 |
| 26 | +
|
| 27 | + .EXAMPLE |
| 28 | + Merge-DevPullRequest -Approve -Number 28690, 28691, 28692 |
| 29 | +
|
| 30 | + .EXAMPLE |
| 31 | + Merge-DevPullRequest -Approve -AllArchivePR -Force |
| 32 | +
|
| 33 | + .NOTES |
| 34 | + Requires GitHub CLI to be installed and authenticated (gh auth login). |
| 35 | + #> |
| 36 | + [CmdletBinding(DefaultParameterSetName = 'Single')] |
| 37 | + [Alias('Merge-DevPR')] |
| 38 | + param( |
| 39 | + [Parameter(Mandatory = $true, ParameterSetName = 'Single')] |
| 40 | + [int[]]$Number, |
| 41 | + |
| 42 | + [Parameter(Mandatory = $true, ParameterSetName = 'All')] |
| 43 | + [switch]$AllArchivePR, |
| 44 | + |
| 45 | + [switch]$Approve, |
| 46 | + |
| 47 | + [switch]$Force |
| 48 | + ) |
| 49 | + |
| 50 | + # Check if GitHub CLI is available |
| 51 | + try { |
| 52 | + gh --version | Out-Null |
| 53 | + } |
| 54 | + catch { |
| 55 | + throw "GitHub CLI is not installed or not in PATH. Please install GitHub CLI and authenticate with 'gh auth login'." |
| 56 | + } |
| 57 | + |
| 58 | + # Check if authenticated |
| 59 | + try { |
| 60 | + gh auth status | Out-Null |
| 61 | + } |
| 62 | + catch { |
| 63 | + throw "GitHub CLI is not authenticated. Please run 'gh auth login' first." |
| 64 | + } |
| 65 | + |
| 66 | + $mergedPRs = @() |
| 67 | + $failedPRs = @() |
| 68 | + |
| 69 | + if ($PSCmdlet.ParameterSetName -eq 'Single') { |
| 70 | + # Get PR details for each number |
| 71 | + $prsToMerge = @() |
| 72 | + foreach ($prNumber in $Number) { |
| 73 | + try { |
| 74 | + $prJson = gh pr view $prNumber --json number,title,author,createdAt,url 2>$null |
| 75 | + if ($LASTEXITCODE -ne 0) { |
| 76 | + throw "Pull request #$prNumber not found." |
| 77 | + } |
| 78 | + $pr = $prJson | ConvertFrom-Json |
| 79 | + $prsToMerge += $pr |
| 80 | + } |
| 81 | + catch { |
| 82 | + throw "Failed to get pull request #$prNumber`: $_" |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + else { |
| 87 | + # Get all archive PRs |
| 88 | + try { |
| 89 | + $allPRsJson = gh pr list --state open --author azure-powershell-bot --json number,title,author,createdAt,url 2>$null |
| 90 | + if ($LASTEXITCODE -ne 0) { |
| 91 | + throw "Failed to list pull requests." |
| 92 | + } |
| 93 | + $allPRs = $allPRsJson | ConvertFrom-Json |
| 94 | + } |
| 95 | + catch { |
| 96 | + throw "Failed to list pull requests: $_" |
| 97 | + } |
| 98 | + |
| 99 | + # Filter for archive PRs (with [skip ci] prefix) |
| 100 | + $archivePRs = $allPRs | Where-Object { $_.title.StartsWith('[skip ci]') } | Sort-Object createdAt |
| 101 | + |
| 102 | + if ($archivePRs.Count -eq 0) { |
| 103 | + Write-Host "No archive PRs found matching criteria." |
| 104 | + return @() |
| 105 | + } |
| 106 | + |
| 107 | + $prsToMerge = $archivePRs |
| 108 | + } |
| 109 | + |
| 110 | + # Display PRs to be merged |
| 111 | + if ($prsToMerge.Count -gt 0) { |
| 112 | + if (-not $Force) { |
| 113 | + $confirmMessage = "Do you want to" |
| 114 | + if ($Approve) { $confirmMessage += " approve and" } |
| 115 | + $confirmMessage += " merge the following pull request$(if($prsToMerge.Count -gt 1){'s'})?" |
| 116 | + Write-Host $confirmMessage |
| 117 | + } |
| 118 | + |
| 119 | + # Format and display PRs |
| 120 | + $prTable = $prsToMerge | ForEach-Object { |
| 121 | + [PSCustomObject]@{ |
| 122 | + 'No.' = $_.number |
| 123 | + 'Title' = $_.title |
| 124 | + 'CreatedBy' = $_.author.login |
| 125 | + 'CreatedAt' = [DateTime]::Parse($_.createdAt).ToString('M/d/yyyy h:mm:ss tt') |
| 126 | + 'Url' = $_.url |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + $prTable | Format-Table -AutoSize | Out-String | Write-Host |
| 131 | + |
| 132 | + # Confirmation prompt (for both parameter sets unless Force is used) |
| 133 | + if (-not $Force) { |
| 134 | + do { |
| 135 | + $response = Read-Host "Type Y to$(if($Approve){' approve and'}) merge, N to cancel" |
| 136 | + } while ($response -notin @('Y', 'y', 'N', 'n')) |
| 137 | + |
| 138 | + if ($response -in @('N', 'n')) { |
| 139 | + Write-Host "Operation cancelled." |
| 140 | + return @() |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + # Merge PRs |
| 146 | + foreach ($pr in $prsToMerge) { |
| 147 | + try { |
| 148 | + Write-Host "Processing PR #$($pr.number)..." -ForegroundColor Yellow |
| 149 | + |
| 150 | + # Approve if requested |
| 151 | + if ($Approve) { |
| 152 | + Write-Host " Approving PR #$($pr.number)..." -ForegroundColor Cyan |
| 153 | + gh pr review $pr.number --approve 2>$null |
| 154 | + if ($LASTEXITCODE -ne 0) { |
| 155 | + throw "Failed to approve PR #$($pr.number)" |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + # Merge PR |
| 160 | + Write-Host " Merging PR #$($pr.number)..." -ForegroundColor Cyan |
| 161 | + gh pr merge $pr.number --squash 2>$null |
| 162 | + if ($LASTEXITCODE -ne 0) { |
| 163 | + throw "Failed to merge PR #$($pr.number)" |
| 164 | + } |
| 165 | + |
| 166 | + Write-Host " Successfully merged PR #$($pr.number)" -ForegroundColor Green |
| 167 | + $mergedPRs += $pr |
| 168 | + } |
| 169 | + catch { |
| 170 | + Write-Error "Failed to merge PR #$($pr.number): $_" |
| 171 | + $failedPRs += $pr |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + # Report results |
| 176 | + if ($mergedPRs.Count -gt 0) { |
| 177 | + Write-Host "`nSuccessfully merged $($mergedPRs.Count) pull request(s)." -ForegroundColor Green |
| 178 | + } |
| 179 | + |
| 180 | + if ($failedPRs.Count -gt 0) { |
| 181 | + $errorMessage = "Failed to merge $($failedPRs.Count) pull request(s): $($failedPRs.number -join ', ')" |
| 182 | + Write-Error $errorMessage |
| 183 | + throw $errorMessage |
| 184 | + } |
| 185 | + |
| 186 | + # Return merged PRs in the format shown in README |
| 187 | + return $mergedPRs | ForEach-Object { |
| 188 | + [PSCustomObject]@{ |
| 189 | + 'No.' = $_.number |
| 190 | + 'Title' = $_.title |
| 191 | + 'CreatedBy' = $_.author.login |
| 192 | + 'CreatedAt' = [DateTime]::Parse($_.createdAt).ToString('M/d/yyyy h:mm:ss tt') |
| 193 | + 'Url' = $_.url |
| 194 | + } |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +Export-ModuleMember -Function Merge-DevPullRequest -Alias Merge-DevPR |
0 commit comments