Code Listing 16 public class WebHookHandler public

Code listing 16 public class webhookhandler public

This preview shows page 56 - 60 out of 94 pages.

Code Listing 16 public class BitBucketWebHookHandler : WebHookHandler { public override Task ExecuteAsync( string receiver, WebHookHandlerContext context) { if ( Common .IsBitBucketReceiver(receiver)) { var dataJObject = context.GetDataOrDefault< JObject >(); var action = context.Actions.First(); var enumAction = EnumHelper .GetEnumValue< EnumRepository >(action, EnumProp .DisplayCode, false ); switch (enumAction)
Image of page 56
57 { case EnumRepository .push: //do something break ; case EnumRepository .fork: //do something break ; case EnumRepository .updated: //do something break ; case EnumRepository .commitcommentcreated: //do something break ; case EnumRepository .commitstatuscreated: //do something break ; case EnumRepository .commitstatusupdated: //do something break ; default : var data = dataJObject.ToString(); break ; } } return Task .FromResult( true ); }
Image of page 57
58 } Now, our BitBucketWebHookHandler class looks neat and clean, and we can easily go to an enum and read its description. In every case, we can also use our EnumHelper class for various operations. Before going any further, let us draft out what we want to do with the data. Our data is nothing but some information that our WebHook received via WebHookHandlerContext . Entity framework: Using code-first approach We have already added Entity Framework support to our project. Now the only thing remaining is to add few models, mapping, and persistence classes. Under the folder Models , add a new class with the name ActivityModel . The model class looks like Code Listing 17: Code Listing 17 public class ActivityModel { [ Key ] public int ActivityId { get ; set ; } [ MaxLength (10)] public string Activity { get ; set ; } [ MaxLength (25)] public string Action { get ; set ; } [ MaxLength (65)] public string Description { get ; set ; } public string Data { get ; set ; } } Note: The validation attributes may cause validation errors in API method calls if the data passed in is longer than specified by the attributes, but the attributes are used to create the data model. Setting up the repository
Image of page 58