Skip to content

Commit

Permalink
fix(fork): First try at the createFork method
Browse files Browse the repository at this point in the history
It takes the default_branch_only parameter into account.
Tests still fail.
  • Loading branch information
gounthar committed Dec 10, 2024
1 parent 58dcca1 commit f750d99
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 22 deletions.
65 changes: 43 additions & 22 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -1459,22 +1459,9 @@ public PagedIterable<GHRepository> listForks(final ForkSort sort) {
* @throws IOException
* the io exception
*/
@Deprecated
public GHRepository fork() throws IOException {
root().createRequest().method("POST").withUrlPath(getApiTailUrl("forks")).send();

// this API is asynchronous. we need to wait for a bit
for (int i = 0; i < 10; i++) {
GHRepository r = root().getMyself().getRepository(name);
if (r != null) {
return r;
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw (IOException) new InterruptedIOException().initCause(e);
}
}
throw new IOException(this + " was forked but can't find the new repository");
return createFork(null, null, false);
}

/**
Expand Down Expand Up @@ -1504,16 +1491,50 @@ public GHBranchSync sync(String branch) throws IOException {
* @throws IOException
* the io exception
*/
@Deprecated
public GHRepository forkTo(GHOrganization org) throws IOException {
root().createRequest()
.method("POST")
.with("organization", org.getLogin())
.withUrlPath(getApiTailUrl("forks"))
.send();
return createFork(org.getLogin(), null, false);
}

/**
* Creates a fork of this repository with optional parameters.
*
* @param organization
* the organization to fork to, or null to fork to the authenticated user's account
* @param name
* the name of the new repository, or null to use the same name as the original repository
* @param defaultBranchOnly
* whether to fork only the default branch
* @return the newly forked repository
* @throws IOException
* if an I/O error occurs
*/
public GHRepository createFork(String organization, String name, boolean defaultBranchOnly) throws IOException {
if (organization != null && organization.isEmpty()) {
throw new IllegalArgumentException("Organization cannot be empty");
}
if (name != null && name.isEmpty()) {
throw new IllegalArgumentException("Name cannot be empty");
}

Requester requester = root().createRequest().method("POST").withUrlPath(getApiTailUrl("forks"));

if (organization != null) {
requester.with("organization", organization);
}
if (name != null) {
requester.with("name", name);
}
if (defaultBranchOnly) {
requester.with("default_branch_only", true);
}

requester.send();

// this API is asynchronous. we need to wait for a bit
for (int i = 0; i < 10; i++) {
GHRepository r = org.getRepository(name);
GHRepository r = organization != null ? root().getOrganization(organization).getRepository(name != null ? name : this.name)
: root().getMyself().getRepository(name != null ? name : this.name);
if (r != null) {
return r;
}
Expand All @@ -1523,7 +1544,7 @@ public GHRepository forkTo(GHOrganization org) throws IOException {
throw (IOException) new InterruptedIOException().initCause(e);
}
}
throw new IOException(this + " was forked into " + org.getLogin() + " but can't find the new repository");
throw new IOException(this + " was forked but can't find the new repository");
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/kohsuke/github/GHRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,33 @@ public void ghRepositorySearchBuilderForkDefaultResetForksSearchTerms() {
assertThat(ghRepositorySearchBuilder.terms.stream().filter(item -> item.contains("fork:")).count(), is(0L));
}

/**
* Test createFork method with valid parameters.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void testCreateForkWithValidParameters() throws IOException {
GHRepository repository = getRepository();
GHRepository forkedRepository = repository.createFork("new-owner", "new-repo", true);
assertThat(forkedRepository, notNullValue());
assertThat(forkedRepository.getOwnerName(), equalTo("new-owner"));
assertThat(forkedRepository.getName(), equalTo("new-repo"));
}

/**
* Test createFork method with invalid parameters.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test(expected = IllegalArgumentException.class)
public void testCreateForkWithInvalidParameters() throws IOException {
GHRepository repository = getRepository();
repository.createFork(null, "", true);
}

/**
* List commit comments some comments.
*
Expand Down

0 comments on commit f750d99

Please sign in to comment.