简体   繁体   English

如何使用GitHub API和Octokit JavaScript将大于1mb的文件提交给GitHub

[英]How to commit files larger than 1mb to GitHub using the GitHub API and Octokit JavaScript

I need to update a file on GitHub from JavaScript running in a client browser application. 我需要在客户端浏览器应用程序中运行的JavaScript上更新GitHub上的文件。 The file may be larger than 1mb so I cannot use the octokit createFile() function which is limited to files less than 1mb. 该文件可能大于1mb所以我不能使用octokit createFile()函数,该函数仅限于小于1mb的文件。

I have an authenticated user through oauth, a file, and a repository that the file needs to end up in. Basically I have: 我有一个经过身份验证的用户通过oauth,一个文件和一个文件库需要最终进入的存储库。基本上我有:

this.octokit = new Octokit();
this.octokit.authenticate({
    type: "oauth",
    token: github.access_token
})
this.file = "some text in a string thing";
this.octokit.repos.getContents({
    owner: this.currentUser,
    repo: this.currentRepoName,
    path: path
}).then(thisRepo => {

And I need to figure out how to get the contents of this.file into a file say project.stl in the root folder of that repo. 我需要弄清楚如何将this.file的内容放入该repo的根文件夹中的文件中说project.stl。

I have found this excellent blog post with a description of how the process works using the ruby version of the octokit library. 我已经找到了这篇优秀的博客文章 ,其中描述了该过程如何使用octokit库的ruby版本。 I don't have any experience with ruby so understanding what is going on there will take me some time. 我对ruby没有任何经验,所以理解那里发生的事情需要一些时间。 This seems like it must be a common enough problem that there should be an example of a JavaScript solution out there. 这似乎是一个常见的问题,应该有一个JavaScript解决方案的例子。 I would love any help that anyone can offer with uploading a file from JavaScript to GitHub, and I will post my solution if I can get to work to serve as an example. 我希望任何人都可以提供从JavaScript上传文件到GitHub的任何帮助,如果我可以作为一个例子,我会发布我的解决方案。

Edit: I rewrote how it works like this, which I think is much better: 编辑:我重写了它是如何工作的,我认为这更好:

async createCommit (octokit, { owner, repo, base, changes }) { let response; async createCommit(octokit,{owner,repo,base,changes}){let response;

  if (!base) {
    response = await octokit.repos.get({ owner, repo })
    base = response.data.default_branch
  }

  response = await octokit.repos.listCommits({
    owner,
    repo,
    sha: base,
    per_page: 1
  })
  let latestCommitSha = response.data[0].sha
  const treeSha = response.data[0].commit.tree.sha

  response = await octokit.git.createTree({
    owner,
    repo,
    base_tree: treeSha,
    tree: Object.keys(changes.files).map(path => {
      return {
        path,
        mode: '100644',
        content: changes.files[path]
      }
    })
  })
  const newTreeSha = response.data.sha

  response = await octokit.git.createCommit({
    owner,
    repo,
    message: changes.commit,
    tree: newTreeSha,
    parents: [latestCommitSha]
  })
  latestCommitSha = response.data.sha

  await octokit.git.updateRef({
    owner,
    repo,
    sha: latestCommitSha,
    ref: `heads/master`,
    force: true
  })

  console.log("Project saved");

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM