Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@


See https://github-api.kohsuke.org/ for more details

## Forking a Repository

To fork a repository and retrieve only the default branch, you can use the `forkRepository` method in the `GitHub` class. Here's an example:

```java
GitHub github = new GitHubBuilder().withOAuthToken("YOUR_OAUTH_TOKEN").build();
GHRepository forkedRepo = github.forkRepository("owner", "repository");
System.out.println("Forked repository: " + forkedRepo.getFullName());
```
Comment on lines +13 to +19
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Documentation needs improvement for clarity and security

The example code needs several improvements:

  1. It doesn't demonstrate the new defaultBranchOnly parameter
  2. It shows hardcoded OAuth token which is a security anti-pattern
  3. Missing explanation of the default branch only behavior and its implications

Consider updating the example as follows:

-GitHub github = new GitHubBuilder().withOAuthToken("YOUR_OAUTH_TOKEN").build();
-GHRepository forkedRepo = github.forkRepository("owner", "repository");
-System.out.println("Forked repository: " + forkedRepo.getFullName());
+// Create GitHub instance using environment variable for security
+GitHub github = new GitHubBuilder().withOAuthToken(System.getenv("GITHUB_TOKEN")).build();
+
+// Fork repository with only the default branch
+GHRepository forkedRepo = github.forkRepository("owner", "repository")
+    .forkDefaultBranchOnly(true)
+    .done();
+
+// The forked repository will only contain the default branch
+System.out.println("Forked repository: " + forkedRepo.getFullName());
+System.out.println("Default branch: " + forkedRepo.getDefaultBranch());
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
To fork a repository and retrieve only the default branch, you can use the `forkRepository` method in the `GitHub` class. Here's an example:
```java
GitHub github = new GitHubBuilder().withOAuthToken("YOUR_OAUTH_TOKEN").build();
GHRepository forkedRepo = github.forkRepository("owner", "repository");
System.out.println("Forked repository: " + forkedRepo.getFullName());
```
To fork a repository and retrieve only the default branch, you can use the `forkRepository` method in the `GitHub` class. Here's an example:

202 changes: 10 additions & 192 deletions src/main/java/org/kohsuke/github/GHRepositoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,277 +5,95 @@
import java.io.IOException;
import java.net.URL;

// TODO: Auto-generated Javadoc
/**
* The Class GHRepositoryBuilder.
*
* @param <S>
* the generic type
*/
abstract class GHRepositoryBuilder<S> extends AbstractBuilder<GHRepository, S> {

/**
* Instantiates a new GH repository builder.
*
* @param intermediateReturnType
* the intermediate return type
* @param root
* the root
* @param baseInstance
* the base instance
*/
private boolean defaultBranchOnly = false;

protected GHRepositoryBuilder(Class<S> intermediateReturnType, GitHub root, GHRepository baseInstance) {
super(GHRepository.class, intermediateReturnType, root, baseInstance);
}

/**
* Allow or disallow squash-merging pull requests.
*
* @param enabled
* true if enabled
*
* @return a builder to continue with building
*
* @throws IOException
* In case of any networking error or error from the server.
*/
public S allowSquashMerge(boolean enabled) throws IOException {
return with("allow_squash_merge", enabled);
}

/**
* Allow or disallow merging pull requests with a merge commit.
*
* @param enabled
* true if enabled
*
* @return a builder to continue with building
*
* @throws IOException
* In case of any networking error or error from the server.
*/
public S allowMergeCommit(boolean enabled) throws IOException {
return with("allow_merge_commit", enabled);
}

/**
* Allow or disallow rebase-merging pull requests.
*
* @param enabled
* true if enabled
*
* @return a builder to continue with building
*
* @throws IOException
* In case of any networking error or error from the server.
*/
public S allowRebaseMerge(boolean enabled) throws IOException {
return with("allow_rebase_merge", enabled);
}

/**
* Allow or disallow private forks
*
* @param enabled
* true if enabled
* @return a builder to continue with building
* @throws IOException
* In case of any networking error or error from the server.
*/
public S allowForking(boolean enabled) throws IOException {
return with("allow_forking", enabled);
}

/**
* After pull requests are merged, you can have head branches deleted automatically.
*
* @param enabled
* true if enabled
*
* @return a builder to continue with building
*
* @throws IOException
* In case of any networking error or error from the server.
*/
public S deleteBranchOnMerge(boolean enabled) throws IOException {
return with("delete_branch_on_merge", enabled);
}

/**
* Default repository branch.
*
* @param branch
* branch name
* @return a builder to continue with building
* @throws IOException
* In case of any networking error or error from the server.
*/
public S defaultBranch(String branch) throws IOException {
return with("default_branch", branch);
}

/**
* Description for repository.
*
* @param description
* description of repository
* @return a builder to continue with building
* @throws IOException
* In case of any networking error or error from the server.
*/
public S description(String description) throws IOException {
return with("description", description);
}

/**
* Homepage for repository.
*
* @param homepage
* homepage of repository
* @return a builder to continue with building
* @throws IOException
* In case of any networking error or error from the server.
*/
public S homepage(URL homepage) throws IOException {
return homepage(homepage.toExternalForm());
}

/**
* Homepage for repository.
*
* @param homepage
* homepage of repository
* @return a builder to continue with building
* @throws IOException
* In case of any networking error or error from the server.
*/
public S homepage(String homepage) throws IOException {
return with("homepage", homepage);
}

/**
* Sets the repository to private.
*
* @param enabled
* private if true
* @return a builder to continue with building
* @throws IOException
* In case of any networking error or error from the server.
*/
public S private_(boolean enabled) throws IOException {
return with("private", enabled);
}

/**
* Sets the repository visibility.
*
* @param visibility
* visibility of repository
* @return a builder to continue with building
* @throws IOException
* In case of any networking error or error from the server.
*/
public S visibility(final Visibility visibility) throws IOException {
return with("visibility", visibility.toString());
}

/**
* Enables issue tracker.
*
* @param enabled
* true if enabled
* @return a builder to continue with building
* @throws IOException
* In case of any networking error or error from the server.
*/
public S issues(boolean enabled) throws IOException {
return with("has_issues", enabled);
}

/**
* Enables projects.
*
* @param enabled
* true if enabled
* @return a builder to continue with building
* @throws IOException
* In case of any networking error or error from the server.
*/
public S projects(boolean enabled) throws IOException {
return with("has_projects", enabled);
}

/**
* Enables wiki.
*
* @param enabled
* true if enabled
* @return a builder to continue with building
* @throws IOException
* In case of any networking error or error from the server.
*/
public S wiki(boolean enabled) throws IOException {
return with("has_wiki", enabled);
}

/**
* Enables downloads.
*
* @param enabled
* true if enabled
* @return a builder to continue with building
* @throws IOException
* In case of any networking error or error from the server.
*/
public S downloads(boolean enabled) throws IOException {
return with("has_downloads", enabled);
}

/**
* Specifies whether the repository is a template.
*
* @param enabled
* true if enabled
* @return a builder to continue with building
* @throws IOException
* In case of any networking error or error from the server.
*/
public S isTemplate(boolean enabled) throws IOException {
return with("is_template", enabled);
}

/**
* Done.
*
* @return the GH repository
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public S forkDefaultBranchOnly(boolean enabled) throws IOException {
this.defaultBranchOnly = enabled;
return (S) this;
}

@Override
public GHRepository done() throws IOException {
if (defaultBranchOnly) {
requester.with("default_branch_only", true);
}
return super.done();
}

/**
* Archive.
*
* @return the s
* @throws IOException
* Signals that an I/O exception has occurred.
*/
S archive() throws IOException {
return with("archived", true);
}

/**
* Name.
*
* @param name
* the name
* @return the s
* @throws IOException
* Signals that an I/O exception has occurred.
*/
S name(String name) throws IOException {
return with("name", name);
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -1333,4 +1333,22 @@ GHUser intern(GHUser user) throws IOException {
}

private static final Logger LOGGER = Logger.getLogger(GitHub.class.getName());

/**
* Forks a repository to the authenticated user's account, retrieving only the default branch.
*
* @param owner
* the owner of the repository to fork
* @param repo
* the name of the repository to fork
* @return the newly forked repository
* @throws IOException
* if an I/O error occurs
*/
public GHRepository forkRepository(String owner, String repo) throws IOException {
return createRequest().method("POST")
.with("default_branch_only", true)
.withUrlPath("/repos/" + owner + "/" + repo + "/forks")
.fetch(GHRepository.class);
}
}
14 changes: 14 additions & 0 deletions src/test/java/org/kohsuke/github/GitHubTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,18 @@ public void testCatchServiceDownException() {
assertThat(e.getClass().getName(), equalToIgnoringCase(ServiceDownException.class.getName()));
}
}

/**
* Test forking a repository with only the default branch.
*
* @throws IOException
* if an I/O error occurs
*/
@Test
public void testForkRepository() throws IOException {
GHRepository forkedRepo = gitHub.forkRepository("hub4j", "github-api");
assertThat(forkedRepo, notNullValue());
assertThat(forkedRepo.getDefaultBranch(), equalTo("main"));
assertThat(forkedRepo.getBranches().size(), equalTo(1));
}
}