Github WebHooks handler using Asp .Net

In previous post we have seen WebHooks handler for Instagram here.

In this post we will see WebHooks handler for Github.

Github gives very powerful support to the developers.

git

Currnetly there are more than 20 events supported by Github which includes Wildcard Event.

What is Wildcard Event?

When you add the wildcard event, it will be replaced by any existing events you have configured with the wildcard event and send you payloads for all supported events. You’ll also automatically get any new events we might add in the future.

(*) is Wildcard Event.

Considering that you have followed all the steps shown here, We will directly see code for Github handler which is as below:

public class InstagramWebHookHandler : WebHookHandler
{
   public InstagramWebHookHandler()
   {
       this.Receiver = "Github";
   }
 
   public override async Task ExecuteAsync(string generator, WebHookHandlerContext context)
   {     
       JObject entry = context.GetDataOrDefault<JObject>();

       // Extract the action -- for Bitbucket we have only one.
       string action = context.Actions.First();
       switch (action)
       {
           case "push":
           // Any Git push to a Repository, including editing tags or branches. Commits via               API actions that update references are also counted. This is the default event        
          break;
   
          case "*":
          // Any time any event is triggered (Wildcard Event).
          break;
         
          case "commit_comment":
          // Any time a Commit is commented on.
          break;

          case "issues":
          // Any time an Issue is assigned, unassigned, labeled, unlabeled, opened, closed,              or reopened.
          break;

          case "create":
          // Any time a Branch or Tag is created.
          break;
          
          case "delete":
          // Any time a Branch or Tag is deleted.
          break;
          
          case "fork":
          // Any time a Repository is forked.
          break;
          
          case "issue_comment":
          // Any time an Issue or Pull Request is commented on.
          break; 
          
          case "repository":
          // Any time a Repository is created. Organization hooks only.
          break; 
          
          case "team_add":
          // Any time a team is added or modified on a Repository.
          break; 
          
          case "watch":
          // Any time a User stars a Repository.
          break;
          
          case "page_build":
          // Any time a Pages site is built or results in a failed build.
          break;

          case "release":
          // Any time a Release is published in a Repository.
          break;

          case "pull_request":
          // Any time a Pull Request is assigned, unassigned, labeled, unlabeled, opened, cl             osed, reopened, or synchronized 
          break;

         default:
         Trace.WriteLine(entry.ToString());
         break;
     }

     return Task.FromResult(true); } 
}

Above are different events supported by Github. You can write your code as per your requirement.

There are other events as well like gollum, deployment_status, membership etc.

Now Let us see how WebHook POST request body looks like when it comes from Github.

For example below is an example of a WebHook POST request body from GitHub looks like this as a result if anyone comments on any issue in a particular repository:

{
   "action": "opened",
   "issue": {
                "url": "https://api.github.com/repos/NeelBhatt/NewBornVNext",
               "number": 1,
               ...
                 },
   "comment": {
             "url": "https://api.github.com/repos/NeelBhatt/NewBornVNext/issues/comments/1",
             "issue_url": "https://api.github.com/repos/NeelBhatt/NewBornVNext/public-repo/issues/2",
   "id": 1,
   "user": {
              "login": "human",
              "id": 6752317,
              ...
            },
             ...
              },
   "repository": {
             "id": 1,
             "full_name": "octocat/NewBornVNext",
            "owner": {
                    "login": "NeelBhatt",
                    "id": 1
                    ...
                    },
                    ...
                  },
   "sender": {
              "login": "NeelBhatt",
              "id": 1,
              ...
              }
 }

For more information regarding Event Types & Payloads for Github, Have a look here.

Stay tuned for more updates.

Advertisements

One thought on “Github WebHooks handler using Asp .Net

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