我正在使用npm GitHub API。
我有四个数据。
我要更新的文件的参考
我要更新的文件的路径
我要放在此文件中的新内容
我要进行此编辑的提交消息
此外,我可以通过API进行身份验证,并可以访问此存储库。
现在如何编辑此文件并推送此提交?
const GitHub = require('github-api')
const gh = new GitHub({
token: config.app.git_token,
}, githubUrl)
const repo = gh.getRepo(config.app.repoOwner, config.app.repoName)
repo.getRef(`heads/${config.app.repoBranch}`).then((response) => {
const ref = response.data.object.sha
const path = 'README.md'
const content = '#Foo Bar\nthis is foo bar'
const message = 'make readme foo bar'
console.log('ref to the file i want to update')
console.log(ref)
console.log('path to the file i want to update')
console.log(path)
console.log('contents i now want in this file')
console.log(content)
console.log('commit message message')
console.log(message)
// how do i now edit and add a commit to this remote file?
})
我已经尝试过使用.commit了,但是到目前为止,还没有使它起作用,我还不知道如何为该函数调用生成正确的参数。
最佳答案
得到它了!
这是执行此操作的语法:
const GitHub = require('github-api')
const gh = new GitHub({
token: config.app.git_token,
}, githubUrl)
const repo = gh.getRepo(config.app.repoOwner, config.app.repoName)
const branch = config.app.repoBranch
const path = 'README.md'
const content = '#Foo Bar\nthis is foo bar'
const message = 'add foo bar to the readme'
const options = {}
repo.writeFile(
branch,
path,
content,
message,
options
).then((r) => {
console.log(r)
})
我需要使用.writeFile方法!