|
| 1 | +import java.io.File; |
| 2 | +import java.util.List; |
| 3 | + |
| 4 | +import org.eclipse.jgit.api.Git; |
| 5 | +import org.eclipse.jgit.lib.Repository; |
| 6 | +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; |
| 7 | +import org.eclipse.jgit.transport.RemoteConfig; |
| 8 | +import org.eclipse.jgit.transport.URIish; |
| 9 | +import org.kohsuke.github.GHIssueState; |
| 10 | +import org.kohsuke.github.GHPullRequest; |
| 11 | +import org.kohsuke.github.GHRepository; |
| 12 | +import org.kohsuke.github.GitHub; |
| 13 | +import org.kohsuke.github.GitHubBuilder; |
| 14 | + |
| 15 | +///usr/bin/env jbang "$0" "$@" ; exit $? |
| 16 | + |
| 17 | +//JAVA 21+ |
| 18 | +//RUNTIME_OPTIONS --enable-native-access=ALL-UNNAMED |
| 19 | + |
| 20 | +//DEPS org.kohsuke:github-api:2.0-rc.3 |
| 21 | +//DEPS org.eclipse.jgit:org.eclipse.jgit.pgm:7.2.1.202505142326-r |
| 22 | + |
| 23 | +public class CheckoutPR { |
| 24 | + public static void main(String[] args) throws Exception { |
| 25 | + GitHub github = new GitHubBuilder().build(); |
| 26 | + GHRepository repo = github.getRepository("JabRef/jabref"); |
| 27 | + |
| 28 | + if (args.length != 1) { |
| 29 | + System.err.println("Usage: java CheckoutPR <pull-request-number>|<contributor:branch-name>"); |
| 30 | + System.exit(1); |
| 31 | + } |
| 32 | + |
| 33 | + String arg = args[0]; |
| 34 | + GHPullRequest pr; |
| 35 | + if (arg.contains(":")) { |
| 36 | + String[] parts = arg.split(":"); |
| 37 | + String contributor = parts[0]; |
| 38 | + String branchName = parts[1]; |
| 39 | + pr = repo.getPullRequests(GHIssueState.OPEN) |
| 40 | + .stream() |
| 41 | + .filter(p -> p.getHead().getUser().getLogin().equals(contributor)) |
| 42 | + .filter(p -> p.getHead().getRef().equals(branchName)) |
| 43 | + .findFirst() |
| 44 | + .orElseThrow(() -> new IllegalArgumentException("Pull request not found for branch: " + branchName)); |
| 45 | + } else { |
| 46 | + pr = repo.getPullRequest(Integer.parseInt(args[0])); |
| 47 | + } |
| 48 | + |
| 49 | + String headRef = pr.getHead().getRef(); |
| 50 | + String headRepoCloneUrl = pr.getHead().getRepository().getHttpTransportUrl(); |
| 51 | + |
| 52 | + final String remoteName = "tmp-remote"; |
| 53 | + final String localBranchName = "pr--" + pr.getNumber() + "--" + pr.getUser().getLogin() + "--" + headRef; |
| 54 | + |
| 55 | + // Open the repository in the current directory (".") |
| 56 | + File repoDir = new File("."); |
| 57 | + Repository repository = new FileRepositoryBuilder() |
| 58 | + .setGitDir(new File(repoDir, ".git")) |
| 59 | + .readEnvironment() |
| 60 | + .findGitDir() |
| 61 | + .build(); |
| 62 | + |
| 63 | + try (Git git = new Git(repository)) { |
| 64 | + // If current branch is "tmp-branch", checkout "main" |
| 65 | + String currentBranch = repository.getBranch(); |
| 66 | + if (localBranchName.equals(currentBranch)) { |
| 67 | + System.out.println("Checking out 'main' branch"); |
| 68 | + git.checkout().setName("main").call(); |
| 69 | + } |
| 70 | + |
| 71 | + // Check if branch "tmp-branch" exists and remove it if present |
| 72 | + List<org.eclipse.jgit.lib.Ref> branches = git.branchList().call(); |
| 73 | + boolean branchExists = branches.stream().anyMatch(branch -> branch.getName().endsWith(localBranchName)); |
| 74 | + if (branchExists) { |
| 75 | + System.out.println("Deleting branch 'tmp-branch'"); |
| 76 | + git.branchDelete().setBranchNames(localBranchName).setForce(true).call(); |
| 77 | + } |
| 78 | + |
| 79 | + // Check if the remote "tmp-remote" exists and remove it if present |
| 80 | + List<RemoteConfig> remotes = git.remoteList().call(); |
| 81 | + boolean remoteExists = remotes.stream().anyMatch(remote -> remote.getName().equals(remoteName)); |
| 82 | + if (remoteExists) { |
| 83 | + System.out.println("Removing remote 'tmp-remote'"); |
| 84 | + git.remoteRemove().setRemoteName(remoteName).call(); |
| 85 | + } |
| 86 | + |
| 87 | + System.out.println("Adding remote 'tmp-remote'"); |
| 88 | + git.remoteAdd() |
| 89 | + .setName(remoteName) |
| 90 | + .setUri(new URIish(headRepoCloneUrl)) |
| 91 | + .call(); |
| 92 | + } |
| 93 | + |
| 94 | + // Has nice output, therefore we use pgm |
| 95 | + System.out.println("Fetching..."); |
| 96 | + String[] jGitArgsFetch = {"fetch", remoteName}; |
| 97 | + org.eclipse.jgit.pgm.Main.main(jGitArgsFetch); |
| 98 | + |
| 99 | + try (Git git = new Git(repository)) { |
| 100 | + System.out.println("Checking out..."); |
| 101 | + git.checkout() |
| 102 | + .setCreateBranch(true) |
| 103 | + .setName(localBranchName) |
| 104 | + .setStartPoint(remoteName + "/" + headRef) |
| 105 | + .call(); |
| 106 | + } |
| 107 | + |
| 108 | + System.out.println("Checked out PR #" + pr.getNumber() + " (" + pr.getTitle() + ") to branch '" + localBranchName + "'."); |
| 109 | + } |
| 110 | +} |
0 commit comments