66import  com .box .l10n .mojito .github .GithubClient ;
77import  com .box .l10n .mojito .github .GithubClients ;
88import  com .box .l10n .mojito .github .GithubException ;
9+ import  java .io .IOException ;
910import  java .util .List ;
1011import  org .fusesource .jansi .Ansi ;
12+ import  org .kohsuke .github .GHIssueState ;
1113import  org .kohsuke .github .GHPullRequest ;
1214import  org .springframework .beans .factory .annotation .Autowired ;
1315import  org .springframework .beans .factory .annotation .Qualifier ;
@@ -90,6 +92,13 @@ public class GithubCreatePRCommand extends Command {
9092      description  = "The PR labels" )
9193  List <String > labels ;
9294
95+   @ Parameter (
96+       names  = {"--also-close-prefixed" },
97+       description  =
98+           "Closes all PRs that start with the specified prefix. Use this to manage PRs that are part of a batch or related by a common feature." ,
99+       required  = false )
100+   String  alsoClosePrefixed  = null ;
101+ 
93102  enum  EnableAutoMergeType  {
94103    SQUASH ,
95104    MERGE ,
@@ -107,6 +116,10 @@ protected void execute() throws CommandException {
107116
108117      GithubClient  githubClient  = githubClients .getClient (owner );
109118
119+       if  (alsoClosePrefixed  != null ) {
120+         closePRPrefixedWith (githubClient , alsoClosePrefixed );
121+       }
122+ 
110123      GHPullRequest  pr  = githubClient .createPR (repository , title , head , base , body , reviewers );
111124
112125      consoleWriter .a ("PR created: " ).fg (Ansi .Color .CYAN ).a (pr .getHtmlUrl ().toString ()).println ();
@@ -120,4 +133,22 @@ protected void execute() throws CommandException {
120133      throw  new  CommandException (e );
121134    }
122135  }
136+ 
137+   void  closePRPrefixedWith (GithubClient  githubClient , String  alsoClosePrefixed ) {
138+     githubClient .listPR (repository , GHIssueState .OPEN ).stream ()
139+         .filter (pr  -> pr .getTitle ().startsWith (alsoClosePrefixed ))
140+         .forEach (
141+             pullRequest  -> {
142+               try  {
143+                 consoleWriter 
144+                     .a ("Closing: " )
145+                     .fg (Ansi .Color .CYAN )
146+                     .a (pullRequest .getHtmlUrl ().toString ())
147+                     .println ();
148+                 pullRequest .close ();
149+               } catch  (IOException  e ) {
150+                 throw  new  CommandException (e );
151+               }
152+             });
153+   }
123154}
0 commit comments