2

I am using the npm GitHub API.

And I have four pieces of data.

  1. the ref to the file I want to update
  2. the path to the file I want to update
  3. the new contents I want to be in this file
  4. the commit message I want for this edit

Additionally, I can authenticate to the API, and have access to this repo.

How do I now edit this file and push this 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)
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?
})

I've tried using .commit but, so far, have not gotten it to work, I don't understand how to generate the correct params to that function call.

1 Answer 1

1

Got it!

Here's the syntax for how to do this:

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)
})

I needed to use the .writeFile method!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.