Ashwin Kumar
Ashwin Kumar

Reputation: 71

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)

Upvotes: 0

Views: 1274

Answers (2)

Using node js v12.18.1 or later and octokit/rest api v17: Just generate a personal access token from your github profile settings and use the code below:

require('dotenv').config();

const { Octokit } = require('@octokit/rest');
const clientWithAuth = new Octokit({
  auth: process.env.TOKEN, //create github personal token
});

const main = async () => {
  const owner = process.env.OWNER;
  const username = process.env.USERNAME;

  const response = await clientWithAuth.repos.createUsingTemplate({
    template_owner: owner,
    template_repo: 'template-testing',
    name: `template-${username}`,
  });

  console.log('repository successfully create 🎉');
};

main();

Upvotes: -1

riQQ
riQQ

Reputation: 12823

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

Upvotes: 2

Related Questions