| |
| |
| |
|
|
| class CommitNode { |
| [string] $Hash |
| [string[]] $Parents |
| [string] $AuthorName |
| [string] $AuthorGitHubLogin |
| [string] $AuthorEmail |
| [string] $Subject |
| [string] $Body |
| [string] $PullRequest |
| [string] $ChangeLogMessage |
| [string] $ThankYouMessage |
| [bool] $IsBreakingChange |
|
|
| CommitNode($hash, $parents, $name, $email, $subject, $body) { |
| $this.Hash = $hash |
| $this.Parents = $parents |
| $this.AuthorName = $name |
| $this.AuthorEmail = $email |
| $this.Subject = $subject |
| $this.Body = $body |
| $this.IsBreakingChange = $body -match "\[breaking change\]" |
|
|
| if ($subject -match "\(#(\d+)\)$") { |
| $this.PullRequest = $Matches[1] |
| } |
| } |
| } |
|
|
| |
| |
| $Script:powershell_team = @( |
| "Travis Plunk" |
| "dependabot-preview[bot]" |
| "dependabot[bot]" |
| "github-actions[bot]" |
| "Copilot" |
| "Anam Navied" |
| "Andrew Schwartzmeyer" |
| "Jason Helmick" |
| "Patrick Meinecke" |
| "Steven Bucher" |
| "PowerShell Team Bot" |
| "Justin Chung" |
| ) |
|
|
| |
| $script:psteam_logins = @( |
| 'andyleejordan' |
| 'TravisEz13' |
| 'daxian-dbw' |
| 'adityapatwardhan' |
| 'SteveL-MSFT' |
| 'dependabot[bot]' |
| 'pwshBot' |
| 'jshigetomi' |
| 'SeeminglyScience' |
| 'anamnavi' |
| 'sdwheeler' |
| 'Copilot' |
| 'copilot-swe-agent' |
| 'app/copilot-swe-agent' |
| 'StevenBucher98' |
| 'alerickson' |
| 'tgauth' |
| ) |
|
|
| |
| $Script:community_login_map = @{ |
| "darpa@yandex.ru" = "iSazonov" |
| "c.bergmeister@gmail.com" = "bergmeister" |
| "github@markekraus.com" = "markekraus" |
| "info@powercode-consulting.se" = "powercode" |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function Get-ChildMergeCommit |
| { |
| [CmdletBinding(DefaultParameterSetName="TagName")] |
| param( |
| [Parameter(Mandatory, ParameterSetName="TagName")] |
| [string]$LastReleaseTag, |
|
|
| [Parameter(Mandatory, ParameterSetName="CommitHash")] |
| [string]$CommitHash |
| ) |
|
|
| $tag_hash = $CommitHash |
| if ($PSCmdlet.ParameterSetName -eq "TagName") { $tag_hash = git rev-parse "$LastReleaseTag^0" } |
|
|
| |
| $merge_commits_not_in_release_branch = git --no-pager log --merges "$tag_hash..HEAD" --format='%H||%P' |
| |
| $child_merge_commit = $merge_commits_not_in_release_branch | Select-String -SimpleMatch $tag_hash |
| return $child_merge_commit.Line |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function New-CommitNode |
| { |
| param( |
| [Parameter(ValueFromPipeline)] |
| [ValidatePattern("^.+\|.+\|.+\|.+\|.+$")] |
| [string]$CommitMetadata |
| ) |
|
|
| Process { |
| $hash, $parents, $name, $email, $subject = $CommitMetadata.Split("||") |
| $body = (git --no-pager show $hash -s --format=%b) -join "`n" |
| return [CommitNode]::new($hash, $parents, $name, $email, $subject, $body) |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function Get-ChangeLog |
| { |
| param( |
| [Parameter(Mandatory = $true)] |
| [string]$LastReleaseTag, |
|
|
| [Parameter(Mandatory = $true)] |
| [string]$ThisReleaseTag, |
|
|
| [Parameter(Mandatory = $false)] |
| [string]$Token, |
|
|
| [Parameter()] |
| [switch]$HasCherryPick |
| ) |
|
|
| if(-not $Token) { |
| $Token = Get-GHDefaultAuthToken |
| if(-not $Token) { |
| throw "No GitHub Auth Token provided" |
| } |
| } |
|
|
| $tag_hash = git rev-parse "$LastReleaseTag^0" |
| $format = '%H||%P||%aN||%aE||%s' |
| $header = @{"Authorization"="token $Token"} |
|
|
| |
| $child_merge_commit = Get-ChildMergeCommit -CommitHash $tag_hash |
| if($child_merge_commit) |
| { |
| $commit_hash, $parent_hashes = $child_merge_commit.Split("||") |
| } |
| |
| $other_parent_hash = ($parent_hashes -replace $tag_hash).Trim() |
|
|
| if ($HasCherryPick) { |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| $new_commits_from_other_parent = git --no-pager log --first-parent --cherry-pick --right-only "$tag_hash...$other_parent_hash" --format=$format | New-CommitNode |
| |
| $new_commits_from_last_release = git --no-pager log --first-parent --cherry-pick --left-only "$tag_hash...$other_parent_hash" --format=$format | New-CommitNode |
| |
| $duplicate_commits = $null |
| if($new_commits_from_last_release -and $new_commits_from_other_parent) |
| { |
| $duplicate_commits = Compare-Object $new_commits_from_last_release $new_commits_from_other_parent -Property PullRequest -ExcludeDifferent -IncludeEqual -PassThru |
| } |
| if ($duplicate_commits) { |
| $duplicate_pr_numbers = @($duplicate_commits | ForEach-Object -MemberName PullRequest) |
| $new_commits_from_other_parent = $new_commits_from_other_parent | Where-Object PullRequest -NotIn $duplicate_pr_numbers |
| } |
|
|
| |
| $new_commits_after_merge_commit = @(git --no-pager log --first-parent "$commit_hash..HEAD" --format=$format | New-CommitNode) |
| $new_commits = $new_commits_after_merge_commit + $new_commits_from_other_parent |
| } else { |
| |
| |
|
|
| |
| |
| |
|
|
| |
| $new_commits_during_last_release = @(git --no-pager log --first-parent "$tag_hash..$other_parent_hash" --format=$format | New-CommitNode) |
| |
| $new_commits_after_last_release = @(git --no-pager log --first-parent "$commit_hash..HEAD" --format=$format | New-CommitNode) |
| |
| $new_commits = $new_commits_during_last_release + $new_commits_after_last_release |
| } |
|
|
| |
| $unlabeledPRs = @() |
|
|
| |
| $multipleLabelsPRs = @() |
|
|
| |
| $clBreakingChange = @() |
|
|
| |
| $clBuildPackage = @() |
|
|
| |
| $clCodeCleanup = @() |
|
|
| |
| $clDocs = @() |
|
|
| |
| $clEngine = @() |
|
|
| |
| $clGeneral = @() |
|
|
| |
| $clPerformance = @() |
|
|
| |
| $clTest = @() |
|
|
| |
| $clTools = @() |
|
|
| |
| $clUntagged = @() |
|
|
| |
| $clExperimental = @() |
|
|
| foreach ($commit in $new_commits) { |
| $commitSubject = $commit.Subject |
| $prNumber = $commit.PullRequest |
| Write-Verbose "subject: $commitSubject" |
| Write-Verbose "authorname: $($commit.AuthorName)" |
|
|
| try { |
| $pr = Invoke-RestMethod ` |
| -Uri "https://api.github.com/repos/PowerShell/PowerShell/pulls/$prNumber" ` |
| -Headers $header ` |
| -ErrorAction Stop ` |
| -Verbose:$false |
| } catch { |
| |
| |
| if ($_.Exception.Response.StatusCode -ne 404) { |
| throw |
| } |
| } |
|
|
| if ($commitSubject -match '^\[release/v\d\.\d\] ') { |
| |
| if (-not $pr) { |
| throw "The commit is from a backport PR (#$prNumber), but the PR cannot be found.`nPR Title: $commitSubject" |
| } |
|
|
| $userPattern = 'Triggered by @.+ on behalf of @(.+)' |
| if ($pr.body -match $userPattern) { |
| $commit.AuthorGitHubLogin = ($Matches.1).Trim() |
| Write-Verbose "backport PR. real author login: $($commit.AuthorGitHubLogin)" |
| } else { |
| throw "The commit is from a backport PR (#$prNumber), but the PR description failed to match the pattern '$userPattern'. Was the template for backport PRs changed?`nPR Title: $commitSubject" |
| } |
| } |
|
|
| if ($commit.AuthorGitHubLogin) { |
| if ($script:psteam_logins -contains $commit.AuthorGitHubLogin) { |
| $commit.ChangeLogMessage = "- {0}" -f (Get-ChangeLogMessage $commitSubject) |
| } else { |
| $commit.ChangeLogMessage = ("- {0} (Thanks @{1}!)" -f (Get-ChangeLogMessage $commitSubject), $commit.AuthorGitHubLogin) |
| $commit.ThankYouMessage = ("@{0}" -f ($commit.AuthorGitHubLogin)) |
| } |
| } elseif ($commit.AuthorEmail.EndsWith("@microsoft.com") -or $powershell_team -contains $commit.AuthorName) { |
| $commit.ChangeLogMessage = "- {0}" -f (Get-ChangeLogMessage $commitSubject) |
| } else { |
| if ($community_login_map.ContainsKey($commit.AuthorEmail)) { |
| $commit.AuthorGitHubLogin = $community_login_map[$commit.AuthorEmail] |
| } else { |
| try{ |
| |
| $response = Invoke-RestMethod ` |
| -Uri "https://api.github.com/repos/PowerShell/PowerShell/commits/$($commit.Hash)" ` |
| -Headers $header ` |
| -ErrorAction Stop ` |
| -Verbose:$false |
| } catch { |
| |
| |
| if ($_.Exception.Response.StatusCode -ne 422) { |
| throw |
| } |
| } |
|
|
| if($response) |
| { |
| $commit.AuthorGitHubLogin = $response.author.login |
| $community_login_map[$commit.AuthorEmail] = $commit.AuthorGitHubLogin |
| } |
| } |
|
|
| $commit.ChangeLogMessage = ("- {0} (Thanks @{1}!)" -f (Get-ChangeLogMessage $commitSubject), $commit.AuthorGitHubLogin) |
| $commit.ThankYouMessage = ("@{0}" -f ($commit.AuthorGitHubLogin)) |
| } |
|
|
| if ($commit.IsBreakingChange) { |
| $commit.ChangeLogMessage = "{0} [Breaking Change]" -f $commit.ChangeLogMessage |
| } |
|
|
| |
| if($pr) |
| { |
| $clLabel = $pr.labels | Where-Object { $_.Name -match "^CL-"} |
| } |
| else { |
| Write-Warning -Message "Tagging $($commit.Hash) by $($commit.AuthorName), as CL-BuildPackaging as it does not have a PR." |
| $clLabel = [PSCustomObject]@{Name ='CL-BuildPackaging'} |
| } |
|
|
| if ($clLabel.count -gt 1 -and $clLabel.Name -notcontains 'CL-BreakingChange') { |
| $multipleLabelsPRs += $pr |
| } |
| elseif ($clLabel.count -eq 0) { |
| $unlabeledPRs += $pr |
| } |
| else { |
| switch ($clLabel.Name) { |
| "CL-BreakingChange" { $clBreakingChange += $commit } |
| "CL-BuildPackaging" { $clBuildPackage += $commit } |
| "CL-CodeCleanup" { $clCodeCleanup += $commit } |
| "CL-Docs" { $clDocs += $commit } |
| "CL-Engine" { $clEngine += $commit } |
| "CL-Experimental" { $clExperimental += $commit } |
| "CL-General" { $clGeneral += $commit } |
| "CL-Performance" { $clPerformance += $commit } |
| "CL-Test" { $clTest += $commit } |
| "CL-Tools" { $clTools += $commit } |
| "CL-Untagged" { $clUntagged += $commit } |
| "CL-NotInBuild" { continue } |
| Default { throw "unknown tag '$cLabel' for PR: '$prNumber'" } |
| } |
| } |
| } |
|
|
| if ($multipleLabelsPRs.count -gt 0) { |
| Write-Error "PRs should not be tagged with multiple CL labels. PRs with multiple labels: $($multipleLabelsPRs.number -join ' ')" |
| $shouldThrow = $true |
| } |
|
|
| if ($unlabeledPRs.count -gt 0) { |
| Write-Error "PRs should have at least one CL label. PRs missing labels: $($unlabeledPRs.number -join ' ')" |
| $shouldThrow = $true |
| } |
|
|
| if ($shouldThrow) { |
| throw "Some PRs are tagged multiple times or have no tags." |
| } |
|
|
| |
|
|
| $version = $ThisReleaseTag.TrimStart('v') |
|
|
| Write-Output "## [${version}] - $(Get-Date -Format yyyy-MM-dd)`n" |
|
|
| PrintChangeLog -clSection $clUntagged -sectionTitle 'UNTAGGED - Please classify' |
| PrintChangeLog -clSection $clBreakingChange -sectionTitle 'Breaking Changes' |
| PrintChangeLog -clSection $clEngine -sectionTitle 'Engine Updates and Fixes' |
| PrintChangeLog -clSection $clExperimental -sectionTitle 'Experimental Features' |
| PrintChangeLog -clSection $clPerformance -sectionTitle 'Performance' |
| PrintChangeLog -clSection $clGeneral -sectionTitle 'General Cmdlet Updates and Fixes' |
| PrintChangeLog -clSection $clCodeCleanup -sectionTitle 'Code Cleanup' -Compress |
| PrintChangeLog -clSection $clTools -sectionTitle 'Tools' |
| PrintChangeLog -clSection $clTest -sectionTitle 'Tests' |
| PrintChangeLog -clSection $clBuildPackage -sectionTitle 'Build and Packaging Improvements' -Compress |
| PrintChangeLog -clSection $clDocs -sectionTitle 'Documentation and Help Content' |
|
|
| Write-Output "[${version}]: https://github.com/PowerShell/PowerShell/compare/${LastReleaseTag}...${ThisReleaseTag}`n" |
| } |
|
|
| function Get-GHDefaultAuthToken { |
| $IsGHCLIInstalled = $false |
| if (Get-command -CommandType Application -Name gh -ErrorAction SilentlyContinue) { |
| $IsGHCLIInstalled = $true |
| } else { |
| Write-Error -Message "GitHub CLI is not installed. Please install it from https://cli.github.com/" -ErrorAction Stop |
| } |
|
|
| if ($IsGHCLIInstalled) { |
| try { |
| $Token = & gh auth token |
| } catch { |
| Write-Error -Message "Please login to GitHub CLI using 'gh auth login'" |
| } |
| } |
|
|
| if (-not $Token) { |
| $Token = Read-Host -Prompt "Enter GitHub Auth Token" |
| } |
|
|
| return $Token |
| } |
|
|
| function PrintChangeLog($clSection, $sectionTitle, [switch] $Compress) { |
| if ($clSection.Count -gt 0) { |
| "### $sectionTitle`n" |
|
|
| if ($Compress) { |
| $items = $clSection.ChangeLogMessage -join "`n" |
| $thankYou = "We thank the following contributors!`n`n" |
| $thankYou += ($clSection.ThankYouMessage | Select-Object -Unique | Where-Object { if($_) { return $true} return $false}) -join ", " |
|
|
| "<details>`n" |
| "<summary>`n" |
| $thankYou | ConvertFrom-Markdown | Select-Object -ExpandProperty Html |
| "</summary>`n" |
| $items | ConvertFrom-Markdown | Select-Object -ExpandProperty Html |
| "</details>" |
| } |
| else { |
| $clSection | ForEach-Object -MemberName ChangeLogMessage |
| } |
| "" |
| } |
| } |
|
|
| function Get-ChangeLogMessage |
| { |
| param($OriginalMessage) |
|
|
| switch -regEx ($OriginalMessage) |
| { |
| '^Merged PR (\d*): ' { |
| return $OriginalMessage.replace($Matches.0,'') + " (Internal $($Matches.1))" |
| } |
| '^Build\(deps\): ' { |
| return $OriginalMessage.replace($Matches.0,'') |
| } |
| '^\[release/v\d\.\d\] ' { |
| return $OriginalMessage.replace($Matches.0,'') |
| } |
| default { |
| return $OriginalMessage |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function Get-NewOfficalPackage |
| { |
| param( |
| [String] |
| $Path = (Join-Path -Path $PSScriptRoot -ChildPath '..\src'), |
| [Switch] |
| $IncludeAll |
| ) |
| |
| $filter = Join-Path -Path $Path -ChildPath '*.csproj' |
| $csproj = Get-ChildItem $filter -Recurse -Exclude 'PSGalleryModules.csproj' |
|
|
| $csproj | ForEach-Object{ |
| $file = $_ |
|
|
| |
| [xml] $csprojXml = (Get-Content -Raw -Path $_) |
|
|
| |
| $packages=$csprojXml.Project.ItemGroup.PackageReference |
|
|
| |
| foreach ($package in $packages) |
| { |
| |
| $name = $package.Include |
|
|
| if ($name) |
| { |
| |
| $versions = Find-Package -Name $name -Source https: |
| Add-Member -Type ScriptProperty -Name Published -Value { $this.Metadata['published']} -PassThru | |
| Where-Object { Test-IncludePackageVersion -NewVersion $_.Version -Version $package.version} |
|
|
| $revsionRegEx = Get-MatchingMajorMinorRegEx -Version $package.version |
| $newPackage = $versions | |
| Sort-Object -Descending | |
| Select-Object -First 1 |
|
|
| |
| $newRevision = $versions | |
| Where-Object {$_.Version -match $revsionRegEx } | |
| Sort-Object -Descending | |
| Select-Object -First 1 |
|
|
| |
| if ($newRevision -and $newRevision.Version.ToString() -ne $package.version -or $newPackage -and $newPackage.Version.ToString() -ne $package.version -or $IncludeAll.IsPresent) |
| { |
| if ($newRevision) |
| { |
| $newRevisionString = $newRevision.Version |
| } |
| else |
| { |
| |
| $newRevisionString = $package.Version |
| } |
|
|
| if ($newPackage) |
| { |
| $newVersionString = $newPackage.Version |
| } |
| else |
| { |
| |
| $newVersionString = $package.Version |
| } |
|
|
| [pscustomobject]@{ |
| Csproj = (Split-Path -Path $file -Leaf) |
| PackageName = $name |
| CsProjVersion = $Package.Version |
| NuGetRevision = $newRevisionString |
| NuGetVersion = $newVersionString |
| } |
| } |
| } |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function Test-IncludePackageVersion |
| { |
| param( |
| [string] |
| $NewVersion, |
| [string] |
| $Version |
| ) |
|
|
| $simpleCompare = $Version -notlike '*-*' |
|
|
| if($simpleCompare -and $NewVersion -like '*-*') |
| { |
| |
| return $false |
| } |
| elseif($simpleCompare -and [Version]$NewVersion -ge [Version] $Version) |
| { |
| |
| return $true |
| } |
| elseif($simpleCompare) |
| { |
| |
| return $false |
| } |
| elseif($NewVersion -notlike '*-*') |
| { |
| |
| |
| $versionOnly = ($Version -Split '\-')[0] |
| if([Version]$NewVersion -ge [Version] $versionOnly) |
| { |
| return $true |
| } |
| else |
| { |
| return $false |
| } |
| } |
| else |
| { |
| |
| return $true |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function Get-MatchingMajorMinorRegEx |
| { |
| param( |
| [Parameter(Mandatory)] |
| $Version |
| ) |
|
|
| $parts = $Version -split '\.' |
|
|
| $regEx = "^$($parts[0])\.$($parts[1])\..*" |
| return $regEx |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function Update-PsVersionInCode |
| { |
| param( |
| [Parameter(Mandatory)] |
| [ValidatePattern("^v\d+\.\d+\.\d+(-\w+(\.\d{1,2})?)?$")] |
| [String] |
| $NewReleaseTag, |
|
|
| [Parameter(Mandatory)] |
| [ValidatePattern("^v\d+\.\d+\.\d+(-\w+(\.\d{1,2})?)?$")] |
| [String] |
| $NextReleaseTag, |
|
|
| [String] |
| $Path = (Join-Path -Path $PSScriptRoot -ChildPath '..') |
| ) |
|
|
| $metaDataPath = (Join-Path -Path $PSScriptRoot -ChildPath 'metadata.json') |
| $metaData = Get-Content -Path $metaDataPath | ConvertFrom-Json |
| $currentTag = $metaData.StableReleaseTag |
|
|
| $currentVersion = $currentTag -replace '^v' |
| $newVersion = $NewReleaseTag -replace '^v' |
| $metaData.NextReleaseTag = $NextReleaseTag |
| Set-Content -Path $metaDataPath -Encoding ascii -Force -Value ($metaData | ConvertTo-Json) |
|
|
| Get-ChildItem -Path $Path -Recurse -File | |
| Where-Object {$_.Extension -notin '.icns','.svg' -and $_.NAME -ne 'CHANGELOG.md' -and $_.DirectoryName -notmatch '[\\/]docs|demos[\\/]'} | |
| Where-Object {$_ | Select-String -SimpleMatch $currentVersion -List} | |
| ForEach-Object { |
| $content = Get-Content -Path $_.FullName -Raw -ReadCount 0 |
| $newContent = $content.Replace($currentVersion,$newVersion) |
| Set-Content -Path $_.FullName -Encoding ascii -Force -Value $newContent -NoNewline |
| } |
| } |
|
|
|
|
| |
| |
| |
| |
| function Test-GitHubCli { |
| $gitHubCli = Get-Command -Name 'gh' -ErrorAction SilentlyContinue |
|
|
| if ($gitHubCli) { |
| return $true |
| } else { |
| return $false |
| } |
| } |
|
|
| |
| |
| |
| |
| function Test-GitHubCliVersion { |
| param( |
| [Parameter(Mandatory)] |
| [System.Management.Automation.SemanticVersion] |
| $RequiredVersion |
| ) |
| [System.Management.Automation.SemanticVersion] $version = gh --version | ForEach-Object { |
| if ($_ -match ' (\d+\.\d+\.\d+) ') { |
| $matches[1] |
| } |
| } |
|
|
| if ($version -ge $RequiredVersion) { |
| return $true |
| } else { |
| return $false |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function Get-PRBackportReport { |
| param( |
| [ValidateSet('Consider', 'Approved', 'Done')] |
| [String] $TriageState = 'Approved', |
| [ValidatePattern('^\d+\.\d+$')] |
| [string] $Version, |
| [switch] $Web |
| ) |
|
|
| if (!(Test-GitHubCli)) { |
| throw "GitHub CLI is not installed. Please install it from https://cli.github.com/" |
| } |
|
|
| $requiredVersion = '2.17' |
| if (!(Test-GitHubCliVersion -RequiredVersion $requiredVersion)) { |
| throw "Please upgrade the GitHub CLI to version $requiredVersion. Please install it from https://cli.github.com/" |
| } |
|
|
| if (!(gh auth status 2>&1 | Select-String 'logged in')){ |
| throw "Please login to GitHub CLI using 'gh auth login'" |
| } |
|
|
| $prs = gh pr list --state merged --label "Backport-$Version.x-$TriageState" --json title,number,mergeCommit,mergedAt | |
| ConvertFrom-Json | |
| ForEach-Object { |
| [PScustomObject]@{ |
| CommitId = $_.mergeCommit.oid |
| Number = $_.number |
| Title = $_.title |
| MergedAt = $_.mergedAt |
| } |
| } | Sort-Object -Property MergedAt |
|
|
| if ($Web) { |
| $prs | ForEach-Object { |
| gh pr view $_.Number --web |
| } |
| } else { |
| $prs |
| } |
| } |
| enum RemoteType { |
| GitHub |
| AzureRepo |
| } |
|
|
| function Get-UpstreamInfo { |
| param( |
| [Parameter(Mandatory=$true)] |
| [string[]]$Upstream, |
|
|
| [Parameter(Mandatory=$true)] |
| [string]$UpstreamRemote |
| ) |
|
|
| $upstreamName = '(powershell(core)?)(/_git)?/(powershell)' |
| $pattern = "^$UpstreamRemote\s*(.*)\:(.*/([-\w.]+)/)?$upstreamName(\.git)?.*fetch" |
| Write-Verbose -Verbose "searching for an upstream with regex: '$pattern'" |
| $Upstream = $Upstream | Where-Object { $_ -match $pattern } |
|
|
| Write-Verbose -Verbose "found $Upstream" |
|
|
| if (!$Upstream) { |
| throw "Please create an upstream remote that points to $upstreamName" |
| } |
|
|
| $matches | Format-Table | Out-String -Stream -Width 9999 | Write-Verbose |
| $org = $matches[3] |
| if ($org -ne 'github.com' -and $matches[1] -ne 'git@github.com') { |
| Write-Verbose 'parsing Azure repo remote' -Verbose |
| |
| $project = $matches[4] |
| $repo = $matches[7] |
| $upstreamHost = $matches[1] |
|
|
| if ($upstreamHost -eq 'https') { |
| $upstreamHost = $org |
| } |
| |
| |
| |
| if ($org -match '([^\..]*)\.') { |
| $org = $Matches[1] |
| } |
| } else { |
| Write-Verbose 'parsing github remote' -Verbose |
| |
| $org = $matches[4] |
| $repo = $matches[7] |
| $upstreamHost = 'github.com' |
| $project = $upstreamHost |
| } |
|
|
| $remoteType = [RemoteType]::GitHub |
|
|
| if ($upstreamHost -match '.*azure.com$' -or $upstreamHost -match '.*visualstudio.com$') { |
| [RemoteType] $remoteType = [RemoteType]::AzureRepo |
| } |
|
|
| $upstreamMatchInfo = @{ |
| org = $org |
| project = $project |
| repo = $repo |
| host = $upstreamHost |
| remoteType = $remoteType |
| } |
|
|
| return $upstreamMatchInfo |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function Invoke-PRBackport { |
| [cmdletbinding(SupportsShouldProcess, ConfirmImpact = 'High')] |
| param( |
| [Parameter(Mandatory)] |
| [string] |
| $PrNumber, |
|
|
| [Parameter(Mandatory)] |
| [ValidateScript({$_ -match '^release/v\d+\.\d+(\.\d+)?'})] |
| [string] |
| $Target, |
|
|
| [switch] |
| $Overwrite, |
|
|
| [string] |
| $BranchPostFix, |
|
|
| [string] |
| $UpstreamRemote = 'upstream' |
| ) |
| function script:Invoke-NativeCommand { |
| param( |
| [scriptblock] $ScriptBlock |
| ) |
| &$ScriptBlock |
| if ($LASTEXITCODE -ne 0) { |
| throw "$ScriptBlock fail with $LASTEXITCODE" |
| } |
| } |
| function script:Test-ShouldContinue { |
| param ( |
| $Message |
| ) |
| $continue = $false |
| while(!$continue) { |
| $value = Read-Host -Prompt ($Message + "`nType 'Yes<enter>' to continue 'No<enter>' to exit") |
| switch($value) { |
| 'yes' { |
| $continue= $true |
| } |
| 'no' { |
| throw "User abort" |
| } |
| } |
| } |
| } |
| $ErrorActionPreference = 'stop' |
|
|
| $pr = gh pr view $PrNumber --json 'mergeCommit,state,title' | ConvertFrom-Json |
|
|
| $commitId = $pr.mergeCommit.oid |
| $state = $pr.state |
| $originaltitle = $pr.title |
| $backportTitle = "[$Target]$originalTitle" |
|
|
| Write-Verbose -Verbose "commitId: $commitId; state: $state" |
| Write-Verbose -Verbose "title:$backportTitle" |
|
|
| if ($state -ne 'MERGED') { |
| throw "PR is not merged ($state)" |
| } |
|
|
| $upstream = Invoke-NativeCommand { git remote -v } |
| $upstreamMatchInfo = Get-UpstreamInfo -Upstream $upstream -UpstreamRemote $UpstreamRemote |
| $remoteType = $upstreamMatchInfo.remoteType |
|
|
| Write-Verbose -Verbose "remotetype: $remoteType" |
| $upstreamMatchInfo | Format-Table | Out-String -Stream -Width 9999 | Write-Verbose -Verbose |
|
|
| Invoke-NativeCommand { git fetch $UpstreamRemote $Target } |
|
|
| $switch = '-c' |
| if ($Overwrite) { |
| $switch = '-C' |
| } |
|
|
| $branchName = "backport-$PrNumber" |
| if ($BranchPostFix) { |
| $branchName += "-$BranchPostFix" |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create branch $branchName from $UpstreamRemote/$Target")) { |
| Invoke-NativeCommand { git switch $UpstreamRemote/$Target $switch $branchName } |
| } |
|
|
| try { |
| $revParseParams = @( |
| '--verify' |
| "$commitId^{commit}" |
| ) |
| Invoke-NativeCommand { git rev-parse --quiet $revParseParams } |
| } |
| catch { |
| throw "Commit does not exist. Try fetching the upstream. (git rev-parse $revParseParams)" |
| } |
|
|
|
|
| try { |
| Invoke-NativeCommand { git cherry-pick $commitId } |
| } |
| catch { |
| Test-ShouldContinue -Message "Fix any conflicts with the cherry-pick." |
| } |
|
|
| if ($PSCmdlet.ShouldProcess("Create the PR")) { |
| $body = "Backport #$PrNumber" |
| switch($remoteType) { |
| "AzureRepo" { |
| Write-Verbose -Verbose "Pushing branch to $UpstreamRemote" |
| git push --set-upstream $UpstreamRemote HEAD |
| $parameters = @( |
| 'repos' |
| 'pr' |
| 'create' |
| ) |
| |
| $parameters += @( |
| '--open' |
| ) |
| $parameters += @( |
| '--target-branch' |
| $Target |
| ) |
| $parameters += @( |
| '--title' |
| $backportTitle |
| ) |
| $parameters += @( |
| '--description' |
| $body |
| ) |
| $parameters += @( |
| '--squash' |
| 'true' |
| ) |
| $parameters += @( |
| '--auto-complete' |
| 'true' |
| ) |
| $parameters += @( |
| '--delete-source-branch' |
| 'true' |
| ) |
| $parameters += @( |
| '--org' |
| "https://dev.azure.com/$($upstreamMatchInfo.org)" |
| ) |
| $parameters += @( |
| '--project' |
| $upstreamMatchInfo.project |
| ) |
| $parameters += @( |
| '--source-branch' |
| $branchName |
| ) |
| $parameters += @( |
| '--repository' |
| $upstreamMatchInfo.repo |
| ) |
|
|
| Write-Verbose -Verbose "az $parameters" |
| $null = Invoke-NativeCommand { az $parameters } |
| } |
| "GitHub" { |
| Write-Verbose -Verbose "Creating PR using gh CLI" |
| gh pr create --base $Target --title $backportTitle --body $body --web |
| } |
| default { |
| throw "unknown remoteType: $remoteType" |
| } |
| } |
| } |
| } |
|
|
| |
| |
| |
| function Invoke-PRBackportApproved { |
| param( |
| [Parameter(Mandatory)] |
| [semver] |
| $Version |
| ) |
|
|
| $tagVersion = "$($Version.Major).$($Version.Minor)" |
| $target = "release/$ReleaseTag" |
|
|
| Get-PRBackportReport -Version $tagVersion | |
| ForEach-Object { |
| $prNumber = $_.Number |
| Invoke-PRBackport -ErrorAction Stop -PrNumber $prNumber -Target $target |
| } |
| } |
|
|
| Export-ModuleMember -Function Get-ChangeLog, Get-NewOfficalPackage, Update-PsVersionInCode, Get-PRBackportReport, Invoke-PRBackport, Invoke-PRBackportApproved, Get-UpstreamInfo |
|
|