hub4j / github-api

Java API for GitHub
https://github-api.kohsuke.org/
MIT License
1.12k stars 718 forks source link

Add Support for Retrieving Template Repository Information for a Repository #1812

Closed jbenaventem closed 4 months ago

jbenaventem commented 4 months ago

I'm currently working on a project where I need to retrieve the information of the template repository from which a repository was created. However, it seems that the current version of the GitHub API kosuke library does not support this feature. Here's an example of how I'm trying to use this feature:

GHRepository repository = organization.createRepository(repositoryName)
    .owner("owner")
    .description("Component-Manager Test create repository from template repository")
    .private_(true)
    .fromTemplateRepository("template_owner", "repository_template")
    .create();

   //If I retrieve the GHRepository there's not a way to retrieve the templateRepository
GHRepository repoFind = organization.getRepository(repositoryName);
assert repoFind.getTemplateRepository().getFullName().equals("template_owner/template_repository");

In this code, I'm creating a new repository from a template repository. After the repository is created, I'm trying to retrieve the template repository information using repoFind.getTemplateRepository(). However, this method does not exist in the current version of the library.

Proposed Solution

I propose adding a new method to the GHRepository class that allows retrieving the template repository information. This method could return a GHRepository object representing the template repository. Here's a rough idea of how this could be implemented:

GHRepository.java


    private GHRepository template_repository; //Repository template from it was created

   /**
    * Get Repository template was the repository created from.
    *
    * @return the repository template
    */
    public GHRepository getTemplateRepository() {
        return template_repository;
    }

    /**
     * Sets repository template
     *
     */
    public void setTemplateRepository(GHRepository value) throws IOException {
        set().template_repository(value);
    }

And In the abstract class GHRepositoryBuilder (I'm not absolutly sure about this method is needed)

    public S template_repository(GHRepository repositoryTemplate) throws IOException {
        return with("template_repository", repositoryTemplate);
    }
jbenaventem commented 4 months ago

Assuming you have no objections, I would like to initiate the collaboration process.

bitwiseman commented 4 months ago

@jbenaventem

REST API Docs:

For Create, the only parameters for template-based creation are template_owner and template_repo. There's no template_repository.

For the builder related to Create, since there isn't a template_repository field, I agree you could add an overload for fromTemplateRepository that takes a GHRepository instance and then pulls the owner and repo from the instance. That would be a nice convenience.

For Update, the API does not support changing template_repository. So, there's no setTemplateRepository().

For Read, I agree we should have a method that returns the template repository details.

bitwiseman commented 4 months ago

This looks similar to #1549 and associated PR #1579 .