@@ -151,23 +151,24 @@ public static bool MergeDownstream( BuildContext context, DownstreamMergeSetting
151151 return false ;
152152 }
153153
154- var sourceProductFamily = context . Product . ProductFamily ;
155- var downstreamProductFamily = context . Product . ProductFamily . DownstreamProductFamily ;
154+ var product = context . Product ;
155+ var sourceProductFamily = product . ProductFamily ;
156+ var downstreamProductFamily = product . ProductFamily . DownstreamProductFamily ;
156157
157158 if ( downstreamProductFamily == null )
158159 {
159160 context . Console . WriteWarning (
160- $ "The downstream version product family for '{ context . Product . ProductFamily . Version } ' is not configured. Skipping downstream merge." ) ;
161+ $ "The downstream version product family for '{ product . ProductFamily . Version } ' is not configured. Skipping downstream merge." ) ;
161162
162163 return true ;
163164 }
164165
165- var sourceBranch = context . Product . DependencyDefinition . Branch ;
166+ var sourceBranch = product . DependencyDefinition . Branch ;
166167
167- if ( ! downstreamProductFamily . TryGetDependencyDefinition ( context . Product . ProductName , out var downstreamDependencyDefinition ) )
168+ if ( ! downstreamProductFamily . TryGetDependencyDefinition ( product . ProductName , out var downstreamDependencyDefinition ) )
168169 {
169170 context . Console . WriteError (
170- $ "The '{ context . Product . ProductName } ' downstream product version '{ downstreamProductFamily . Version } ' is not configured." ) ;
171+ $ "The '{ product . ProductName } ' downstream product version '{ downstreamProductFamily . Version } ' is not configured." ) ;
171172
172173 return false ;
173174 }
@@ -222,10 +223,10 @@ public static bool MergeDownstream( BuildContext context, DownstreamMergeSetting
222223
223224 if ( isPullRequestRequired )
224225 {
225- targetBranch = $ "merge/{ downstreamProductFamily . Version } /{ context . Product . ProductFamily . Version } -{ sourceCommitHash } ";
226+ targetBranch = $ "merge/{ downstreamProductFamily . Version } /{ product . ProductFamily . Version } -{ sourceCommitHash } ";
226227
227228 context . Console . WriteMessage (
228- $ "Checking '{ context . Product . ProductName } ' product version '{ downstreamProductFamily . Version } ' for pending merge branches." ) ;
229+ $ "Checking '{ product . ProductName } ' product version '{ downstreamProductFamily . Version } ' for pending merge branches." ) ;
229230
230231 var filter = $ "merge/{ downstreamProductFamily . Version } /*";
231232
@@ -240,7 +241,17 @@ public static bool MergeDownstream( BuildContext context, DownstreamMergeSetting
240241
241242 var formerTargetBranchReferences = references . Where ( r => r . Reference != targetBranchReference ) . ToArray ( ) ;
242243
243- if ( formerTargetBranchReferences . Length > 0 && ! settings . Force )
244+ var reusableBranches = formerTargetBranchReferences . Where ( r => r . Reference . StartsWith (
245+ $ "refs/heads/merge/{ downstreamProductFamily . Version } /{ product . ProductFamily . Version } -",
246+ StringComparison . OrdinalIgnoreCase ) )
247+ . ToArray ( ) ;
248+
249+ if ( reusableBranches . Length == 1 )
250+ {
251+ targetBranch = reusableBranches [ 0 ] . Reference . Substring ( "refs/heads/" . Length ) ;
252+ targetBranchExistsRemotely = true ;
253+ }
254+ else if ( formerTargetBranchReferences . Length > 0 && ! settings . Force )
244255 {
245256 ExplainUnmergedBranches (
246257 context . Console ,
@@ -328,9 +339,7 @@ public static bool MergeDownstream( BuildContext context, DownstreamMergeSetting
328339 {
329340 return false ;
330341 }
331-
332- context . Console . WriteSuccess ( $ "Created pull request: { pullRequestUrl } " ) ;
333-
342+
334343 if ( requiresBuild )
335344 {
336345 if ( ! TryScheduleBuild (
@@ -345,7 +354,12 @@ public static bool MergeDownstream( BuildContext context, DownstreamMergeSetting
345354 return false ;
346355 }
347356
348- context . Console . WriteSuccess ( $ "Scheduled build: { buildUrl } " ) ;
357+ context . Console . WriteSuccess ( $ "Created pull request { pullRequestUrl } and scheduled build { buildUrl } ." ) ;
358+ }
359+ else
360+ {
361+ context . Console . WriteSuccess ( $ "Created and merged pull request { pullRequestUrl } ." ) ;
362+
349363 }
350364 }
351365
@@ -465,11 +479,9 @@ private static bool TryCreatePullRequest(
465479 {
466480 var pullRequestTitle = $ "Downstream merge from '{ sourceBranch } ' branch";
467481
468- ( bool Success , string ? Url , bool RequiresBuild ) newPullRequest ;
469-
470482 if ( VcsUrlParser . TryGetRepository ( remoteUrl , out var repository ) )
471483 {
472- newPullRequest = repository . TryCreatePullRequestAsync (
484+ var newPullRequest = repository . TryCreatePullRequestAsync (
473485 context . Console ,
474486 targetBranch ,
475487 downstreamBranch ,
0 commit comments