Stan Wiechers
Stan Wiechers

Reputation: 2092

Setting the branch when comitting via the github API

I am using the a ruby interface to the github API to push content from a ruby app to github. All works well, but I can't seem to find how to specify the branch when using the Github API.

github = Github.new :oauth_token => 'MYPERSONALACCESSTOKEN'
blobs = github.git_data.blobs.create 'whoisstan', 'test_for_github_api', content: 'Sample', encoding: 'utf-8'

trees = github.git_data.trees.create 'whoisstan', 'test_for_github_api',
    "tree" => [
      {
        "path" => "file.rb",
        "mode" => "100644",
        "type" => "blob",
        "sha" =>  blobs.body['sha']
      }
    ]
commits = github.git_data.commits.create 'whoisstan', 'test_for_github_api', :tree => trees.body['sha'], :message => 'new version', :parents => nil

Also strangely the content doesn't appear linked in the website, I can only get to it when I use the html_url of the response.

https://github.com/whoisstan/test_for_github_api/commit/f349a436e2fdbffddfc6715490da7282160798fe

What looks suspicious is that the commit parent appears to be null. Also the post commit hooks are not triggered. How do I specify the branch and let the content appear proper in the github website?

Upvotes: 0

Views: 86

Answers (1)

Ivan Zuzak
Ivan Zuzak

Reputation: 18772

The documentation over at https://developer.github.com/v3/git/ explains the process for adding a commit to a specific branch. The things you need to do differently are:

  • set the parent of the commit to point to the previous commit on top of which you are creating this commit
  • modify the reference for the branch you want to update so that it points to the created commit

The Git Data API is a low-level API which works with blobs, trees, commits and refs. In some cases, you need this flexibility to achieve the goals of your application. In other cases, you might consider using a higher-level API, like the Repo Contents API, which works with files (and creates all the necessary Git objects behind the scenes)

Upvotes: 1

Related Questions