如何使用GitHub API和Octokit JavaScript将大于1mb的文件提交到GitHub [英] How to commit files larger than 1mb to GitHub using the GitHub API and Octokit JavaScript
问题描述
我需要从客户端浏览器应用程序中运行的JavaScript更新GitHub上的文件.该文件可能大于1mb,因此我不能使用octokit createFile()函数,该函数仅限于小于1mb的文件.
I need to update a file on GitHub from JavaScript running in a client browser application. The file may be larger than 1mb so I cannot use the octokit createFile() function which is limited to files less than 1mb.
我通过oauth,一个文件以及一个文件最终需要存储在的存储库拥有一个经过身份验证的用户.基本上,我拥有:
I have an authenticated user through oauth, a file, and a repository that the file needs to end up in. Basically I have:
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 => {
我需要弄清楚如何将this.file的内容放入该仓库的根文件夹中的名为project.stl的文件中.
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.
我发现了这个出色的博客文章,其中描述了使用octokit库的ruby版本该过程的工作方式.我没有任何关于红宝石的经验,因此了解那里发生的事情将花费我一些时间.看来这肯定是一个足够普遍的问题,应该在那里提供JavaScript解决方案的示例.我希望任何人都能提供将JavaScript从JavaScript上载文件到GitHub的任何帮助,如果可以上班,我将发布我的解决方案.
I have found this excellent blog post with a description of how the process works using the ruby version of the octokit library. I don't have any experience with ruby so understanding what is going on there will take me some time. This seems like it must be a common enough problem that there should be an example of a JavaScript solution out there. 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.
推荐答案
我重写了它的工作方式,我认为这要好得多:
I rewrote how it works like this, which I think is much better:
async createCommit(octokit,{所有者,仓库,基础,更改}){ 让我们回应;
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");
}
这篇关于如何使用GitHub API和Octokit JavaScript将大于1mb的文件提交到GitHub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!