How to Use Octokit Commit Method

The following is a guest post by Ericka Ward and originally appeared on her blog. Ericka is currently a student a The Flatiron School. You can learn more about her here, or follow her on twitter (https://twitter.com/ErickaJoy)_ Octokit is a Ruby wrapper that parses data from the Github API. This is awesome because it makes the data much more accessible and […]

Reading Time 1 min

The following is a guest post by Ericka Ward and originally appeared on her blog. Ericka is currently a student a The Flatiron School. You can learn more about her here, or follow her on twitter (https://twitter.com/ErickaJoy)_

Octokit is a Ruby wrapper that parses data from the Github API. This is awesome because it makes the data much more accessible and easier to work with.

Octokit has many methods that you can use to access information. I’m going to show you how to use the commits method.

In order to use Octokit, make sure to first gem install Octokit and run bundle install. Also make sure to require 'octokit'.

The commits method will show you the commits for one repo. The documentation for the commits is below:

Blog post image: tumblr_md89diDjqu1rtan47.png

Let’s try this! My github username is ejw1. I have a repository called ericka_playlister. If I want to see the commits for my ericka_playlister repository I would do:

Blog post image: tumblr_md89dvoP4G1rtan47.png

This also works:

Blog post image: tumblr_md89e8s5nA1rtan47.png

I personally like the second syntax better because it is more expressive.

The commits method returns an array of nested hashes. Each element in the array is a nested hash that represents a commit object. The nested hash has many keys, including message, date, email, url and more.

So let’s play with this a little. I want to create an array that includes the commit messages for each commit made on github user DavidBakerTv’s mixtapeapp repository. Shout out David! Here’s how I would do that:

Blog post image: tumblr_md89el3IST1rtan47.png

The list_commits method will return the array of commit objects that are nested hashes. I use the map method to iterate through that array and create a new modified array with the commit messages. I call the commit method in order to access the commit hash within the commit object. Finally, I call the message method to access the commit messages.

Let’s look at the return:

Blog post image: tumblr_md89f7ViIK1rtan47.png

Whenever using the commit method, it is best to play around in irb or rails console with your array. The array of nested hashes can be tricky.

Another helpful link:
https://github.com/pengwynn/octokit/issues/141#issuecomment-9002057

Disclaimer: The information in this blog is current as of November 10, 2012. Current policies, offerings, procedures, and programs may differ.

About Flatiron School

More articles by Flatiron School