Tuesday, February 12, 2013

HOWTO: Generate C# request object for GitHub hooks

This week I needed to create a web service for synchronizing changes from GitHub repository into our internal environment. This is easily achievable using two great tools, I just came across.

First, GitHub has created requestb.in - which is really awesome development tool. You can create your own bins, which exposes an endpoint for you that tracks all the requests made to it. As GitHub suggested, I added new hook (go to github.com -> settings -> service hooks -> webhook urls) with this bin I created in requestb.in. This way, I received the json object that GitHub sends to the endpoint.

After I got the json object, I used another awesome tool that I'm going to keep in my bookmarks: http://json2csharp.com/ It converts your json object to c# class definition. Much more convenient than writing all the classes from scratch.

Finally, I had to replace some variables that json2csharp identified to be of typed object (basically, because they are empty) with variables of type string. And delete some classes that were duplicates. Here is what I ended up with:

Feel free to use it out of the box or you can use my approach in case GitHub update their API in the future. I hope this will help someone else, too.