@@ -724,4 +724,86 @@ function Get-PRBackportReport {
724
724
}
725
725
}
726
726
727
- Export-ModuleMember - Function Get-ChangeLog , Get-NewOfficalPackage , Update-PsVersionInCode , Get-PRBackportReport
727
+ # Backports a PR
728
+ # requires:
729
+ # * a remote called upstream pointing to powershell/powershell
730
+ # * the github cli installed and authenticated
731
+ # Usage:
732
+ # Invoke-PRBackport -PRNumber 1234 -Target release/v7.0.1
733
+ # To overwrite a local branch add -Overwrite
734
+ # To add an postfix to the branch name use -BranchPostFix <postfix>
735
+ function Invoke-PRBackport {
736
+ [cmdletbinding (SupportsShouldProcess , ConfirmImpact = ' High' )]
737
+ param (
738
+ [Parameter (Mandatory )]
739
+ [string ]
740
+ $PrNumber ,
741
+
742
+ [Parameter (Mandatory )]
743
+ [ValidateScript ({$_ -match ' ^release/v\d+\.\d+\.\d+' })]
744
+ [string ]
745
+ $Target ,
746
+
747
+ [switch ]
748
+ $Overwrite ,
749
+
750
+ [string ]
751
+ $BranchPostFix
752
+ )
753
+ function script :Invoke-NativeCommand {
754
+ param (
755
+ [scriptblock ] $ScriptBlock
756
+ )
757
+ & $ScriptBlock
758
+ if ($LASTEXITCODE -ne 0 ) {
759
+ throw " $ScriptBlock fail with $LASTEXITCODE "
760
+ }
761
+ }
762
+ $ErrorActionPreference = ' stop'
763
+
764
+ $pr = gh pr view $PrNumber -- json ' mergeCommit,state,title' | ConvertFrom-Json
765
+
766
+ $commitId = $pr.mergeCommit.oid
767
+ $state = $pr.state
768
+ $originaltitle = $pr.title
769
+ $backportTitle = " [$Target ]$originalTitle "
770
+
771
+ Write-Verbose - Verbose " commitId: $commitId ; state: $state "
772
+ Write-Verbose - Verbose " title:$backportTitle "
773
+
774
+ if ($state -ne ' MERGED' ) {
775
+ throw " PR is not merged ($state )"
776
+ }
777
+
778
+ $upstream = $null
779
+ $upstreamName = ' powershell/powershell'
780
+ $upstream = Invoke-NativeCommand { git remote - v } | Where-Object { $_ -match " ^upstream.*$upstreamName .*fetch" }
781
+
782
+ if (! $upstream ) {
783
+ throw " Please create an upstream remote that points to $upstreamName "
784
+ }
785
+
786
+ Invoke-NativeCommand { git fetch upstream $Target }
787
+
788
+ $switch = ' -c'
789
+ if ($Overwrite ) {
790
+ $switch = ' -C'
791
+ }
792
+
793
+ $branchName = " backport-$PrNumber "
794
+ if ($BranchPostFix ) {
795
+ $branchName += " -$BranchPostFix "
796
+ }
797
+
798
+ if ($PSCmdlet.ShouldProcess (" Create branch $branchName from upstream/$Target " )) {
799
+ Invoke-NativeCommand { git switch upstream/ $Target $switch $branchName }
800
+ }
801
+
802
+ Invoke-NativeCommand { git cherry- pick $commitId }
803
+
804
+ if ($PSCmdlet.ShouldProcess (" Create the PR" )) {
805
+ gh pr create -- base $Target -- title $backportTitle -- body " Backport #$PrNumber "
806
+ }
807
+ }
808
+
809
+ Export-ModuleMember - Function Get-ChangeLog , Get-NewOfficalPackage , Update-PsVersionInCode , Get-PRBackportReport , Invoke-PRBackport
0 commit comments