Skip to content

Commit

Permalink
Add command to create pull request in Github
Browse files Browse the repository at this point in the history
This is exploratory to see if we can avoid having to install the GH CLI or make curl command
  • Loading branch information
ja-openai committed Sep 7, 2024
1 parent a3ffeb3 commit e03520b
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}
}
41 changes: 41 additions & 0 deletions common/src/main/java/com/box/l10n/mojito/github/GithubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -304,6 +306,45 @@ public List<GHIssueComment> getPRComments(String repository, int prNumber) {
}
}

public GHPullRequest createPR(
String repository,
String title,
String head,
String base,
String body,
List<String> reviewers) {
String repoFullPath = getRepositoryPath(repository);
try {
GHPullRequest pullRequest =
getGithubClient(repository)
.getRepository(repoFullPath)
.createPullRequest(title, head, base, body);

if (reviewers != null) {
List<GHUser> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,4 @@ public void testQuoteCurlyEscaping() throws MessageFormatIntegrityCheckerExcepti
String format = messageFormat.format(ImmutableMap.of("placeholder", "stuff"));
assertEquals("C'est un {placeholder}", format);
}

}

0 comments on commit e03520b

Please sign in to comment.