2017-08-09 93 views
1

我正在使用npm GitHub API使用GitHub API編輯和添加提交消息到文件

而且我有四塊數據。

  1. 的裁判我想更新
  2. 的路徑,我想更新
  3. 新內容的文件,我想在這個文件
  4. 提交信息我想爲這個文件編輯

此外,我可以驗證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嘗試,但到目前爲止,還沒有得到它的工作,我不知道如何來生成正確的PARAMS該函數調用。

回答

0

Got it!

下面是如何做到這一點的語法:

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