1212using Microsoft . DotNet . DarcLib . Models . AzureDevOps ;
1313using Microsoft . DotNet . DarcLib . Models . Darc ;
1414using Microsoft . DotNet . Internal . Testing . Utility ;
15+ using Microsoft . DotNet . ProductConstructionService . Client ;
16+ using Microsoft . DotNet . ProductConstructionService . Client . Models ;
1517using Microsoft . Extensions . Logging ;
1618using Newtonsoft . Json . Linq ;
1719using NuGet . Configuration ;
1820using NUnit . Framework ;
19- using Microsoft . DotNet . ProductConstructionService . Client ;
20- using Microsoft . DotNet . ProductConstructionService . Client . Models ;
2121using ProductConstructionService . ScenarioTests . ObjectHelpers ;
2222
2323[ assembly: Parallelizable ( ParallelScope . Fixtures ) ]
@@ -46,7 +46,7 @@ public void BaseSetup()
4646 _packageNameSalt = Guid . NewGuid ( ) . ToString ( ) . Substring ( 0 , 8 ) ;
4747 }
4848
49- protected async Task < Octokit . PullRequest > WaitForPullRequestAsync ( string targetRepo , string targetBranch )
49+ protected async Task < Octokit . PullRequest > WaitForPullRequestAsync ( string targetRepo , string targetBranch , Octokit . ItemStateFilter prState = Octokit . ItemStateFilter . Open )
5050 {
5151 Octokit . Repository repo = await GitHubApi . Repository . Get ( TestParameters . GitHubTestOrg , targetRepo ) ;
5252
@@ -56,6 +56,7 @@ public void BaseSetup()
5656 IReadOnlyList < Octokit . PullRequest > prs = await GitHubApi . PullRequest . GetAllForRepository ( repo . Id , new Octokit . PullRequestRequest
5757 {
5858 Base = targetBranch ,
59+ State = prState
5960 } ) ;
6061
6162 if ( prs . Count == 1 )
@@ -1189,10 +1190,21 @@ protected string GetUniqueAssetName(string packageName)
11891190 return $ "{ packageName } .{ _packageNameSalt } ";
11901191 }
11911192
1192- protected static IAsyncDisposable CleanUpPullRequestAfter ( string owner , string repo , Octokit . PullRequest pullRequest )
1193+ protected static IAsyncDisposable CleanUpPullRequestAfter (
1194+ string owner ,
1195+ string repo ,
1196+ Octokit . PullRequest pullRequest ,
1197+ PullRequestCleanupOperation cleanupOperation = PullRequestCleanupOperation . Close )
11931198 => AsyncDisposable . Create ( async ( ) =>
11941199 {
1195- await ClosePullRequest ( owner , repo , pullRequest ) ;
1200+ if ( cleanupOperation == PullRequestCleanupOperation . Merge )
1201+ {
1202+ await MergePullRequest ( owner , repo , pullRequest ) ;
1203+ }
1204+ else
1205+ {
1206+ await ClosePullRequest ( owner , repo , pullRequest ) ;
1207+ }
11961208 } ) ;
11971209
11981210 protected static async Task ClosePullRequest ( string owner , string repo , Octokit . PullRequest pullRequest )
@@ -1220,6 +1232,34 @@ protected static async Task ClosePullRequest(string owner, string repo, Octokit.
12201232 }
12211233 }
12221234
1235+ protected static async Task MergePullRequest ( string owner , string repo , Octokit . PullRequest pullRequest )
1236+ {
1237+ try
1238+ {
1239+ await GitHubApi . PullRequest . Merge (
1240+ owner ,
1241+ repo ,
1242+ pullRequest . Number ,
1243+ new Octokit . MergePullRequest
1244+ {
1245+ CommitMessage = "Merge pull request" ,
1246+ MergeMethod = Octokit . PullRequestMergeMethod . Merge
1247+ } ) ;
1248+ }
1249+ catch
1250+ {
1251+ // Closed already
1252+ }
1253+ try
1254+ {
1255+ await GitHubApi . Git . Reference . Delete ( owner , repo , $ "heads/{ pullRequest . Head . Ref } ") ;
1256+ }
1257+ catch
1258+ {
1259+ // branch already deleted
1260+ }
1261+ }
1262+
12231263 protected static async Task CreateTargetBranchAndExecuteTest ( string targetBranchName , string targetDirectory , Func < Task > test )
12241264 {
12251265 // first create a new target branch
@@ -1285,3 +1325,9 @@ protected static async Task WaitForNewCommitInPullRequest(string repo, Octokit.P
12851325 return pr ;
12861326 }
12871327}
1328+
1329+ internal enum PullRequestCleanupOperation
1330+ {
1331+ Close ,
1332+ Merge
1333+ }
0 commit comments