diff --git a/README.md b/README.md index 76474f45b4..9df3dcaa62 100644 --- a/README.md +++ b/README.md @@ -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()); +``` diff --git a/src/main/java/org/kohsuke/github/GHRepositoryBuilder.java b/src/main/java/org/kohsuke/github/GHRepositoryBuilder.java index d32dbd2563..62d1c0ee48 100644 --- a/src/main/java/org/kohsuke/github/GHRepositoryBuilder.java +++ b/src/main/java/org/kohsuke/github/GHRepositoryBuilder.java @@ -5,277 +5,95 @@ import java.io.IOException; import java.net.URL; -// TODO: Auto-generated Javadoc -/** - * The Class GHRepositoryBuilder. - * - * @param - * the generic type - */ abstract class GHRepositoryBuilder extends AbstractBuilder { - /** - * 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 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); } diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index aa5b6239e3..b481644204 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -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); + } } diff --git a/src/test/java/org/kohsuke/github/GitHubTest.java b/src/test/java/org/kohsuke/github/GitHubTest.java index ada391e326..3c02b77176 100644 --- a/src/test/java/org/kohsuke/github/GitHubTest.java +++ b/src/test/java/org/kohsuke/github/GitHubTest.java @@ -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)); + } }