-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add CheckoutPR.java #13184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add CheckoutPR.java #13184
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import java.io.File; | ||
| import java.util.List; | ||
|
|
||
| import org.eclipse.jgit.api.Git; | ||
| import org.eclipse.jgit.lib.Repository; | ||
| import org.eclipse.jgit.storage.file.FileRepositoryBuilder; | ||
| import org.eclipse.jgit.transport.RemoteConfig; | ||
| import org.eclipse.jgit.transport.URIish; | ||
| import org.kohsuke.github.GHIssueState; | ||
| import org.kohsuke.github.GHPullRequest; | ||
| import org.kohsuke.github.GHRepository; | ||
| import org.kohsuke.github.GitHub; | ||
| import org.kohsuke.github.GitHubBuilder; | ||
|
|
||
| ///usr/bin/env jbang "$0" "$@" ; exit $? | ||
|
|
||
| //JAVA 21+ | ||
| //RUNTIME_OPTIONS --enable-native-access=ALL-UNNAMED | ||
|
|
||
| //DEPS org.kohsuke:github-api:2.0-rc.3 | ||
| //DEPS org.eclipse.jgit:org.eclipse.jgit.pgm:7.2.1.202505142326-r | ||
|
|
||
| public class CheckoutPR { | ||
| public static void main(String[] args) throws Exception { | ||
| GitHub github = new GitHubBuilder().build(); | ||
| GHRepository repo = github.getRepository("JabRef/jabref"); | ||
|
|
||
| if (args.length != 1) { | ||
| System.err.println("Usage: java CheckoutPR <pull-request-number>|<contributor:branch-name>"); | ||
| System.exit(1); | ||
| } | ||
|
|
||
| String arg = args[0]; | ||
| GHPullRequest pr; | ||
| if (arg.contains(":")) { | ||
| String[] parts = arg.split(":"); | ||
| String contributor = parts[0]; | ||
| String branchName = parts[1]; | ||
| pr = repo.getPullRequests(GHIssueState.OPEN) | ||
| .stream() | ||
| .filter(p -> p.getHead().getUser().getLogin().equals(contributor)) | ||
| .filter(p -> p.getHead().getRef().equals(branchName)) | ||
| .findFirst() | ||
| .orElseThrow(() -> new IllegalArgumentException("Pull request not found for branch: " + branchName)); | ||
| } else { | ||
| pr = repo.getPullRequest(Integer.parseInt(args[0])); | ||
| } | ||
|
|
||
| String headRef = pr.getHead().getRef(); | ||
| String headRepoCloneUrl = pr.getHead().getRepository().getHttpTransportUrl(); | ||
|
|
||
| final String remoteName = "tmp-remote"; | ||
| final String localBranchName = "pr--" + pr.getNumber() + "--" + pr.getUser().getLogin() + "--" + headRef; | ||
|
|
||
| // Open the repository in the current directory (".") | ||
| File repoDir = new File("."); | ||
| Repository repository = new FileRepositoryBuilder() | ||
| .setGitDir(new File(repoDir, ".git")) | ||
| .readEnvironment() | ||
| .findGitDir() | ||
| .build(); | ||
|
|
||
| try (Git git = new Git(repository)) { | ||
| // If current branch is "tmp-branch", checkout "main" | ||
| String currentBranch = repository.getBranch(); | ||
| if (localBranchName.equals(currentBranch)) { | ||
| System.out.println("Checking out 'main' branch"); | ||
|
koppor marked this conversation as resolved.
|
||
| git.checkout().setName("main").call(); | ||
| } | ||
|
|
||
| // Check if branch "tmp-branch" exists and remove it if present | ||
| List<org.eclipse.jgit.lib.Ref> branches = git.branchList().call(); | ||
| boolean branchExists = branches.stream().anyMatch(branch -> branch.getName().endsWith(localBranchName)); | ||
| if (branchExists) { | ||
| System.out.println("Deleting branch 'tmp-branch'"); | ||
|
koppor marked this conversation as resolved.
|
||
| git.branchDelete().setBranchNames(localBranchName).setForce(true).call(); | ||
| } | ||
|
|
||
| // Check if the remote "tmp-remote" exists and remove it if present | ||
| List<RemoteConfig> remotes = git.remoteList().call(); | ||
| boolean remoteExists = remotes.stream().anyMatch(remote -> remote.getName().equals(remoteName)); | ||
| if (remoteExists) { | ||
| System.out.println("Removing remote 'tmp-remote'"); | ||
|
koppor marked this conversation as resolved.
|
||
| git.remoteRemove().setRemoteName(remoteName).call(); | ||
| } | ||
|
|
||
| System.out.println("Adding remote 'tmp-remote'"); | ||
|
koppor marked this conversation as resolved.
|
||
| git.remoteAdd() | ||
| .setName(remoteName) | ||
| .setUri(new URIish(headRepoCloneUrl)) | ||
| .call(); | ||
| } | ||
|
|
||
| // Has nice output, therefore we use pgm | ||
| System.out.println("Fetching..."); | ||
|
koppor marked this conversation as resolved.
|
||
| String[] jGitArgsFetch = {"fetch", remoteName}; | ||
| org.eclipse.jgit.pgm.Main.main(jGitArgsFetch); | ||
|
|
||
| try (Git git = new Git(repository)) { | ||
| System.out.println("Checking out..."); | ||
|
koppor marked this conversation as resolved.
|
||
| git.checkout() | ||
| .setCreateBranch(true) | ||
| .setName(localBranchName) | ||
| .setStartPoint(remoteName + "/" + headRef) | ||
| .call(); | ||
| } | ||
|
|
||
| System.out.println("Checked out PR #" + pr.getNumber() + " (" + pr.getTitle() + ") to branch '" + localBranchName + "'."); | ||
|
koppor marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.