githubgithub-apigithub-actionsrestgithub-projects

How can we create a Repository in GitHub with other repository content with template using GitHub Rest API


I have used the Get Repo API (https://api.github.com/repos/myId/myRepoName)to get the repository details. Now I want to create a new repository with the content and files in my "myRepoName".How can I achieve that. Create API(Post)(https://api.github.com/user/repos)


Solution

  • If your source repository is a template repository (https://help.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-template-repository), you can use the following API:

    Note: Creating and using repository templates is currently available for developers to preview. To access this new endpoint during the preview period, you must provide a custom media type in the Accept header:

    application/vnd.github.baptiste-preview+json

    Creates a new repository using a repository template. Use the template_owner and template_repo route parameters to specify the repository to use as the template. The authenticated user must own or be a member of an organization that owns the repository. To check if a repository is available to use as a template, get the repository's information using the Get a repository endpoint and check that the is_template key is true.

    POST /repos/:template_owner/:template_repo/generate

    Parameters

    Name  Type    Description
    owner     string  The organization or person who will own the new repository. To > >create a new repository in an organization, the authenticated user must be a member >of the specified organization.
    name   string  Required. The name of the new repository.
    description    string  A short description of the new repository.
    private    boolean     Either true to create a new private repository or false to >create a new public one. Default: false
    

    https://developer.github.com/v3/repos/#create-a-repository-using-a-template