Skip to content

Commit 2ed4d64

Browse files
committed
Add option to GithubCreatePRCommand for closing PRs with a specific prefix
Adds the --also-close-prefixed option in the GithubCreatePRCommand. It enables the command to close open PRs that start with the provided prefix.
1 parent bc625cc commit 2ed4d64

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

cli/src/main/java/com/box/l10n/mojito/cli/command/GithubCreatePRCommand.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import com.box.l10n.mojito.github.GithubClient;
77
import com.box.l10n.mojito.github.GithubClients;
88
import com.box.l10n.mojito.github.GithubException;
9+
import java.io.IOException;
910
import java.util.List;
1011
import org.fusesource.jansi.Ansi;
12+
import org.kohsuke.github.GHIssueState;
1113
import org.kohsuke.github.GHPullRequest;
1214
import org.springframework.beans.factory.annotation.Autowired;
1315
import 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
}

common/src/main/java/com/box/l10n/mojito/github/GithubClient.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.kohsuke.github.GHAppInstallationToken;
2424
import org.kohsuke.github.GHCommitState;
2525
import org.kohsuke.github.GHIssueComment;
26+
import org.kohsuke.github.GHIssueState;
2627
import org.kohsuke.github.GHPullRequest;
2728
import org.kohsuke.github.GHUser;
2829
import org.kohsuke.github.GitHub;
@@ -314,10 +315,11 @@ public List<GHIssueComment> getPRComments(String repository, int prNumber) {
314315
}
315316
}
316317

317-
public void listPR(String repository) {
318+
public List<GHPullRequest> listPR(String repository, GHIssueState state) {
318319
try {
319-
GitHub gc = getGithubClient(repository);
320-
gc.getRepository(repository);
320+
return getGithubClient(repository)
321+
.getRepository(repository)
322+
.getPullRequests(GHIssueState.OPEN);
321323
} catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) {
322324
throw new RuntimeException(e);
323325
}

0 commit comments

Comments
 (0)