[英]How to commit files larger than 1mb to GitHub using the GitHub API and Octokit JavaScript
我需要在客戶端瀏覽器應用程序中運行的JavaScript上更新GitHub上的文件。 該文件可能大於1mb所以我不能使用octokit createFile()函數,該函數僅限於小於1mb的文件。
我有一個經過身份驗證的用戶通過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 => {
我需要弄清楚如何將this.file的內容放入該repo的根文件夾中的文件中說project.stl。
我已經找到了這篇優秀的博客文章 ,其中描述了該過程如何使用octokit庫的ruby版本。 我對ruby沒有任何經驗,所以理解那里發生的事情需要一些時間。 這似乎是一個常見的問題,應該有一個JavaScript解決方案的例子。 我希望任何人都可以提供從JavaScript上傳文件到GitHub的任何幫助,如果我可以作為一個例子,我會發布我的解決方案。
編輯:我重寫了它是如何工作的,我認為這更好:
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.