An Annoying Github Commit[Github API]

Relax! Relax! Relax!
Don’t worry I won’t annoy you!
Github API, If you are dealing first time with this then be ready to face, But Wait Wait Wait What I am doing here? Obviously, I am here to help you! Let me make it simpler for you. 😀

Now to make a commit using commands is very easy what you have to do is

$git add -A #this will add the files which you are going to commit
$git commit -m "message" #This will make a commit locally!
$git push origin master

that’s it you are done!

Now as you know I am working on Editor basically Github editor I have to fetch repository and then commit back to the master repo on Github. For this purpose, We have to use a Github API
Here, You see Http requests are transfering like Get request or Post Request. Now you might wonder how we can do this in Rails. It is very simple. Just Focus!

There are 2 ways to do that
1. Using Rest Client
2. Using rails github-api gem

REST Client
This will help you in making GET and POST request directly to the API and API will Respond accordingly.
Now Generally to make a Commit you have to follow few steps
which is very well and beautifully explained by levibotelho

Github-API Gem
This is a Ruby client for the official GitHub API.
Installation
Fitst, Open Gemfile and add
gem "github_api"
And then Run

$bundle install

Now, How to make a Commit

In order to be able to commit (create/update/remove) files you need to use Contents API like so:

contents = Github::Client::Repos::Contents.new oauth_token: '...'

Having instantiated the contents, to create a file do:

contents.create 'username', 'repo_name', 'full_path_to/file.ext',
path: 'full_path_to/file.ext',
message: 'Your commit message',
content: 'The contents of your file'

Content is all Base64 encoded to/from the API, and when you create a file it encodes it automatically for you.

To update a file, first you need to find the file so you can get the SHA you’re updating off of:

file = contents.find path: 'full_path_to/file.ext'

Then update the file just like you do with creating:

contents.update 'username', 'repo_name', 'full_path_to/file.ext',
path: 'full_path_to/file.ext'
message: 'Your commit message',
content: 'The contents to be updated',
sha: file.sha

That’s It This will commit ur changes in Remote Repository!
The First method is laborous as you have to perform each step manually. But This Gem makes our life easiear.
I’ll love to here your views in comments.
Keep Rocking! 😀

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s