我已使用 Get Repo API ( https://api.github.com/repos/myId/myRepoName ) 来获取存储库详细信息。现在我想用我的“myRepoName”中的内容和文件创建一个新的存储库。我怎样才能做到这一点。创建 API(发布)(https://api.github.com/user/repos)
2 回答
如果您的源存储库是模板存储库 ( https://help.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-template-repository ),您可以使用以下 API:
注意:创建和使用存储库模板目前可供开发人员预览。要在预览期间访问此新端点,您必须在 Accept 标头中提供自定义媒体类型:
application/vnd.github.baptiste-preview+json
使用存储库模板创建新存储库。使用 template_owner 和 template_repo 路由参数来指定要用作模板的存储库。经过身份验证的用户必须拥有或者是拥有存储库的组织的成员。要检查存储库是否可用作模板,请使用获取存储库端点获取存储库的信息并检查 is_template 键是否为真。
POST /repos/:template_owner/:template_repo/generate
参数
名称 类型 描述 owner string 将拥有新存储库的组织或个人。> >在组织中创建新存储库,经过身份验证的用户必须是指定组织的成员。 名称字符串 必需。新存储库的名称。 描述字符串 新存储库的简短描述。 private boolean true 创建一个新的私有存储库或 false > 创建一个新的公共存储库。默认值:假
https://developer.github.com/v3/repos/#create-a-repository-using-a-template
使用 node 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();