【问题标题】:Use Octokit or the GitHub Rest API to upload multiple files使用 Octokit 或 GitHub Rest API 上传多个文件
【发布时间】:2020-03-08 11:12:16
【问题描述】:

我正在尝试编写一个从路径获取文件并将它们上传到 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)

即使这工作正常,它仍然不是理想的,因为这个项目可能会扩展到一次上传数百个文件的地步,有没有办法只上传一个目录或上传多个文件每次提交?失败了,谁能解决我的错误?

【问题讨论】:

    标签: javascript node.js github-api octokit octokit-js


    【解决方案1】:

    最后我解决了。 createFile 不是正确的方法,应该使用 createTree: https://octokit.github.io/rest.js/#octokit-routes-git-create-tree

    但这并不像只创建树那么简单,您还必须创建一个提交然后更新引用,我按照这个 GitHub 问题寻求指导:

    https://github.com/octokit/rest.js/issues/1308

    【讨论】:

    • 能否提供用于推送多个文件和文件夹的示例代码
    • 我备份了@mnvbrtn。链接已死,由于我正在为此苦苦挣扎,因此将不胜感激。
    • 他们更新了 repo 名称:github.com/octokit/octokit.js/issues/1308 主要文档可以在这里找到:octokit.github.io/octokit.js 我没有找到深层链接或搜索对我有用
    猜你喜欢
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    相关资源
    最近更新 更多