我正在尝试编写一种方法,从路径获取文件并将它们上传到 GitHub 存储库。文件必须保持完整且独立(不能压缩)。这是我到目前为止所得到的:
addFiles(branch) {
const filePath = this.filePath
fs.readdirSync(filePath).forEach((file, index) => {
if (file.match('.txt')) {
const fileData = fs.readFileSync(path.resolve(filePath, file));
this.octokit.repos.createOrUpdateFile({
owner,
repo,
path: `test/${file}`,
branch,
message: `Commit ${index}`,
content: encode(fileData)
})
.catch(err => console.log(err))
}
})
}
这在一定程度上是有效的,但它只会上传一个文件,然后会失败并出现以下错误:
PUT /path/to/repo/contents/test/example.txt - 201 in 1224ms
PUT /path/to/repo/contents/test/example-2.txt - 409 in 1228ms
{ HttpError: is at 90db2dadca8d061e77ca06fe7196197ada6f6687 but expected b7933883cbed4ff91cc2762e24c183b797db0b74
at response.text.then.message (/project/node_modules/@octokit/request/dist-node/index.js:66:23)
即使这工作正常,它仍然不理想,因为这个项目可能会扩展到一次上传数百个文件的程度,有没有办法只上传一个目录或上传多个文件每次提交?如果失败,任何人都可以解决我的错误吗?
最佳答案
最后我解决了。 createFile
不是正确的方法,应该使用 createTree
来完成:https://octokit.github.io/rest.js/#octokit-routes-git-create-tree
这并不像只创建树那么简单,您还必须创建提交,然后更新引用,我遵循此 GitHub 问题作为指导:
关于javascript - 使用 Octokit 或 GitHub Rest API 上传多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58823829/