Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consume Atlassian Webhook with ASP.NET WebAPI?

I've been tasked with writing a service that will accept the HTTP POST data from an Atlassian Webhook sent from JIRA. I'd like to consume the service using WebAPI in the 4.0 .NET framework and I'm wondering how to do this. I found this resource which uncovers the format of the POST data: JIRA Webhooks Overview.

I think that I can write a WebAPI "Post" method that accepts the input as a Dictionary<string, object>, but I was hoping to map to a well-defined object instead of a Dictionary of goo.

I see that there is a NuGet package called Atlassian.SDK which "Contains utilities for interacting with Atlassian JIRA". However, it looks as though the API is geared more toward reading JIRA tickets, creating and updating them.

Does the Atlassian.SDK NuGet package allow for consuming the HTTP POST sent by a Webhook in a strongly-typed manner instead of parsing through the JSON data using a .NET Dictionary?

like image 540
Andy Avatar asked Sep 30 '22 02:09

Andy


1 Answers

AFAIK the Atlassian.SDK doesn't support the Webhook scenario. It supports REST API calls (query and update issues) via Linq to Jira extension and classes, as you wrote.

However, I don't think that you should manually parse the JSON using .NET Dictionary. I have made some very easy steps to produce a strongly-typed webhook result in an ApiController.

  1. I set up a temporary request container in Requestb.in (http://requestb.in/1g9lg8y1)

  2. I created a new ASP.NET MVC Application with Web API, when I started it in the browser, it was accessible on http://localhost:18177/

  3. I set up 2 webhooks in Jira pointing to these addresses Webhooks in Jira

  4. I created a new issue in Jira, and in the Requestb.in page I copied the whole JSON content to the clipboard found in raw body section.

  5. The main point is here: I created custom classes from this JSON content. (You can do this with any tool you want, e.g. JSON2Sharp), personally I did it with the Visual Studio's Web Essentials extension. (Edit -> Paste special -> Paste JSON as Classes)

  6. I renamed the root to JiraWebhookObject, and I modified the signature of the ValuesController's Post method to

    public void Post([FromBody]JiraWebhookObject value)
    
  7. Finally I modified the issue's summary. As a result of the updated webhook, I got the strongly-typed result in my Web API Controller. Strongly-typed webhook

like image 111
luviktor Avatar answered Oct 05 '22 23:10

luviktor