我们如何使用 GitHub Rest API 使用模板在 GitHub 中使用其他存储库内容创建存储库

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

我已使用 Get Repo API (https://api.github.com/repos/myId/myRepoName) 获取存储库详细信息。现在我想用我的“myRepoName”中的内容和文件创建一个新的存储库。我该如何实现。 创建 API(Post)(https://api.github.com/user/repos)

如果您的源存储库是模板存储库 (https://help.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-template-repository),您可以使用以下 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

使用节点 js v12.18.1 或更高版本和 octokit/rest api v17:只需从您的 github 配置文件设置生成个人访问令牌并使用以下代码:

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();