-
Notifications
You must be signed in to change notification settings - Fork 768
Add option to fork default branch only #1995
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
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
aceb784
add only fork default branch
Alaurant 8a8d235
Remove owner validation in fork builder tests
Alaurant bdb8131
update fork tests from personal to org account
Alaurant 33ff1ce
improve test coverage
Alaurant 306dcb2
remove mockito test
Alaurant 6172ee2
Merge branch 'main' into fork-default-branch
Alaurant 0f0a68f
improve more test coverage
Alaurant b2d215e
mvn spotless:apply
Alaurant 4f5e7ff
change sleep method scope
Alaurant a37cc66
Merge branch 'main' into fork-default-branch
bitwiseman 381931b
Merge branch 'main' into fork-default-branch
Alaurant 4d9581a
Update src/test/java/org/kohsuke/github/GHRepositoryForkBuilderTest.java
bitwiseman b2be143
Update src/test/java/org/kohsuke/github/GHRepositoryForkBuilderTest.java
bitwiseman 21b6d24
Update src/test/java/org/kohsuke/github/GHRepositoryForkBuilderTest.java
bitwiseman 6d48896
Update src/test/java/org/kohsuke/github/GHRepositoryForkBuilderTest.java
bitwiseman 1ce34d5
Cleanup extra test files
bitwiseman 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
144 changes: 144 additions & 0 deletions
144
src/main/java/org/kohsuke/github/GHRepositoryForkBuilder.java
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,144 @@ | ||
| package org.kohsuke.github; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InterruptedIOException; | ||
|
|
||
| /** | ||
| * A builder pattern object for creating a fork of a repository. | ||
| * | ||
| * @see GHRepository#createFork() GHRepository#createFork()GHRepository#createFork() | ||
| * @see <a href="https://docs.github.com/en/rest/repos/forks#create-a-fork">Repository fork API</a> | ||
| */ | ||
| public class GHRepositoryForkBuilder { | ||
| private final GHRepository repo; | ||
| private final Requester req; | ||
| private String organization; | ||
| private String name; | ||
| private Boolean defaultBranchOnly; | ||
|
|
||
| static int FORK_RETRY_INTERVAL = 3000; | ||
|
|
||
| /** | ||
| * Instantiates a new Gh repository fork builder. | ||
| * | ||
| * @param repo | ||
| * the repository | ||
| */ | ||
| GHRepositoryForkBuilder(GHRepository repo) { | ||
| this.repo = repo; | ||
| this.req = repo.root().createRequest(); | ||
| } | ||
|
|
||
| /** | ||
| * Sets whether to fork only the default branch. | ||
| * | ||
| * @param defaultBranchOnly | ||
| * the default branch only | ||
| * @return the gh repository fork builder | ||
| */ | ||
| public GHRepositoryForkBuilder defaultBranchOnly(boolean defaultBranchOnly) { | ||
| this.defaultBranchOnly = defaultBranchOnly; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Specifies the target organization for the fork. | ||
| * | ||
| * @param organization | ||
| * the organization | ||
| * @return the gh repository fork builder | ||
| */ | ||
| public GHRepositoryForkBuilder organization(GHOrganization organization) { | ||
| this.organization = organization.getLogin(); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets a custom name for the forked repository. | ||
| * | ||
| * @param name | ||
| * the desired repository name | ||
| * @return the builder | ||
| */ | ||
| public GHRepositoryForkBuilder name(String name) { | ||
| this.name = name; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Creates the fork with the specified parameters. | ||
| * | ||
| * @return the gh repository | ||
| * @throws IOException | ||
| * the io exception | ||
| */ | ||
| public GHRepository create() throws IOException { | ||
| if (defaultBranchOnly != null) { | ||
| req.with("default_branch_only", defaultBranchOnly); | ||
| } | ||
| if (organization != null) { | ||
| req.with("organization", organization); | ||
| } | ||
| if (name != null) { | ||
| req.with("name", name); | ||
| } | ||
|
|
||
| req.method("POST").withUrlPath(repo.getApiTailUrl("forks")).send(); | ||
|
|
||
| // this API is asynchronous. we need to wait for a bit | ||
| for (int i = 0; i < 10; i++) { | ||
| GHRepository r = lookupForkedRepository(); | ||
| if (r != null) { | ||
| return r; | ||
| } | ||
| sleep(FORK_RETRY_INTERVAL); | ||
| } | ||
| throw new IOException(createTimeoutMessage()); | ||
| } | ||
|
|
||
| private GHRepository lookupForkedRepository() throws IOException { | ||
| String repoName = name != null ? name : repo.getName(); | ||
|
|
||
| if (organization != null) { | ||
| return repo.root().getOrganization(organization).getRepository(repoName); | ||
| } | ||
| return repo.root().getMyself().getRepository(repoName); | ||
| } | ||
|
|
||
| /** | ||
| * Sleep. | ||
| * | ||
| * @param millis | ||
| * the millis | ||
| * @throws IOException | ||
| * the io exception | ||
| */ | ||
| protected void sleep(int millis) throws IOException { | ||
| try { | ||
| Thread.sleep(millis); | ||
| } catch (InterruptedException e) { | ||
| throw (IOException) new InterruptedIOException().initCause(e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create timeout message string. | ||
| * | ||
| * @return the string | ||
| */ | ||
| String createTimeoutMessage() { | ||
| StringBuilder message = new StringBuilder(repo.getFullName()); | ||
| message.append(" was forked"); | ||
|
|
||
| if (organization != null) { | ||
| message.append(" into ").append(organization); | ||
| } | ||
|
|
||
| if (name != null) { | ||
| message.append(" with name ").append(name); | ||
| } | ||
|
|
||
| message.append(" but can't find the new repository"); | ||
| return message.toString(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.