diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/GithubCreatePRCommand.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/GithubCreatePRCommand.java new file mode 100644 index 000000000..52de84d99 --- /dev/null +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/GithubCreatePRCommand.java @@ -0,0 +1,95 @@ +package com.box.l10n.mojito.cli.command; + +import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; +import com.box.l10n.mojito.cli.console.ConsoleWriter; +import com.box.l10n.mojito.github.GithubClients; +import com.box.l10n.mojito.github.GithubException; +import java.util.List; +import org.fusesource.jansi.Ansi; +import org.kohsuke.github.GHPullRequest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +@Component +@Scope("prototype") +@Parameters( + commandNames = {"github-pr-create"}, + commandDescription = "Create a Github PR") +public class GithubCreatePRCommand extends Command { + + @Autowired GithubClients githubClients; + + @Qualifier("ansiCodeEnabledFalse") + @Autowired + ConsoleWriter consoleWriter; + + @Parameter( + names = {"--owner", "-o"}, + required = true, + arity = 1, + description = "The Github repository owner") + String owner; + + @Parameter( + names = {"--repository", "-r"}, + required = true, + arity = 1, + description = "The Github repository name") + String repository; + + @Parameter( + names = {"--title"}, + required = true, + arity = 1, + description = "The PR title") + String title; + + @Parameter( + names = {"--head"}, + required = true, + arity = 1, + description = "The PR head") + String head; + + @Parameter( + names = {"--base"}, + required = true, + arity = 1, + description = "The PR base") + String base; + + @Parameter( + names = {"--body"}, + required = false, + arity = 1, + description = "The PR body") + String body; + + @Parameter( + names = {"--reviewers"}, + required = false, + variableArity = true, + description = "The PR reviewers") + List reviewers; + + @Override + public boolean shouldShowInCommandList() { + return false; + } + + @Override + protected void execute() throws CommandException { + try { + + GHPullRequest pr = + githubClients.getClient(owner).createPR(repository, title, head, base, body, reviewers); + + consoleWriter.a("PR created: ").fg(Ansi.Color.CYAN).a(pr.getHtmlUrl().toString()).println(); + } catch (GithubException e) { + throw new CommandException(e); + } + } +} diff --git a/common/src/main/java/com/box/l10n/mojito/github/GithubClient.java b/common/src/main/java/com/box/l10n/mojito/github/GithubClient.java index 023c11f7f..4fd471ff1 100644 --- a/common/src/main/java/com/box/l10n/mojito/github/GithubClient.java +++ b/common/src/main/java/com/box/l10n/mojito/github/GithubClient.java @@ -15,6 +15,8 @@ import org.kohsuke.github.GHAppInstallationToken; import org.kohsuke.github.GHCommitState; import org.kohsuke.github.GHIssueComment; +import org.kohsuke.github.GHPullRequest; +import org.kohsuke.github.GHUser; import org.kohsuke.github.GitHub; import org.kohsuke.github.GitHubBuilder; import org.slf4j.Logger; @@ -304,6 +306,45 @@ public List getPRComments(String repository, int prNumber) { } } + public GHPullRequest createPR( + String repository, + String title, + String head, + String base, + String body, + List reviewers) { + String repoFullPath = getRepositoryPath(repository); + try { + GHPullRequest pullRequest = + getGithubClient(repository) + .getRepository(repoFullPath) + .createPullRequest(title, head, base, body); + + if (reviewers != null) { + List reviewersGH = + reviewers.stream() + .map( + s -> { + try { + return gitHubClient.getUser(s); + } catch (IOException e) { + throw new RuntimeException(e); + } + }) + .toList(); + + pullRequest.requestReviewers(reviewersGH); + } + + return pullRequest; + } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) { + String message = + String.format("Error creating a PR in repository '%s': %s", repoFullPath, e.getMessage()); + logger.error(message, e); + throw new GithubException(message, e); + } + } + public String getOwner() { return owner; } diff --git a/webapp/src/test/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/MessageFormatIntegrityCheckerTest.java b/webapp/src/test/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/MessageFormatIntegrityCheckerTest.java index 1d421305a..faf288f85 100644 --- a/webapp/src/test/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/MessageFormatIntegrityCheckerTest.java +++ b/webapp/src/test/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/MessageFormatIntegrityCheckerTest.java @@ -143,5 +143,4 @@ public void testQuoteCurlyEscaping() throws MessageFormatIntegrityCheckerExcepti String format = messageFormat.format(ImmutableMap.of("placeholder", "stuff")); assertEquals("C'est un {placeholder}", format); } - }