使用GitHub API编辑和添加提交消息到文件

我正在使用npm GitHub API 。

我有四块数据。

  1. 参考我想要更新的文件
  2. 我要更新的文件的path
  3. 我想要在这个文件中的新内容
  4. 我想要编辑的提交消息

另外,我可以对API进行身份validation,并有权访问此回购。

我现在如何编辑这个文件并推送这个提交?

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方法!