使用GitHub API编辑并向文件添加提交消息

时间:2017-08-09 23:37:18

标签: javascript node.js github github-api

我正在使用npm GitHub API

我有四个数据。

  1. 我想要更新的文件的引用
  2. 我想要更新的文件的路径
  3. 我想要在此文件中的新内容
  4. 我想要进行此修改的提交消息
  5. 此外,我可以对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但到目前为止还没有使用它,我不明白如何为该函数调用生成正确的参数。

1 个答案:

答案 0 :(得分:0)

知道了!

以下是如何执行此操作的语法:

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

相关问题