octokit / dotnet-sdk

MIT License
55 stars 8 forks source link

How do i deserialize an event payload from github app webhook json? #83

Open AlmirJNR opened 3 months ago

AlmirJNR commented 3 months ago

Reusable code block

// Imagine that this comes from the json body in a controller
JsonObject json;

// octokit serializer
var serializer = new SimpleJsonSerializer();

In octokit.net i used to do the following:

var eventPayload = serializer.Deserialize<PullRequestEventPayload>(json);

or even when i just want the ActivityPayload i used to do something like:

I noticed that there are a lot of types in the namespace GitHub.Models, like:

But i don't really know which one i should use. So i would like to know how to do something like this using this new library.

github-actions[bot] commented 3 months ago

👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

AlmirJNR commented 3 months ago

I recently found about the lib Octokit.Webhooks.AspNetCore, is it supposed to be used together with this GitHub.Octokit.SDK? It has all the tools needed to handle the SDK variant, except with some types that i found that mismatched (or i am just using the SDK wrong).

Example of type mismatch:

// take in consideration both libs installed

public sealed partial class PullRequestEventProcessor : WebhookEventProcessor
{
...
    protected override async Task ProcessPullRequestWebhookAsync(
        WebhookHeaders headers,
        PullRequestEvent pullRequestEvent,
        PullRequestAction action
    )
    {
...
        // client is of type GitHubClient from the GitHub.Octokit.SDK package 
        var comments = await client
            .Repos[pullRequestEvent.Repository?.Owner.Name][pullRequestEvent.Repository?.Name]
            // type missmatch
            .Issues[Convert.ToInt32(pullRequestEvent.Number)]
            .Comments
            .GetAsync();
...
    }

As i said, i don't really know if this is the right prop to be using from the pullRequestEvent object

versions that i'm using:

kfcampbell commented 3 months ago

Hi @AlmirJNR! There are a couple good questions here.

In general with this library, I don't think you should be deserializing anything yourself; the tool should do it for you.

It's definitely confusing that there are a lot of models! Our OpenAPI specification is giant and not always perfectly normalized. In general, I've had pretty good luck by searching the API documentation for what I need, then searching the SDK for that documentation to find the model.

So if I'm looking at this API endpoint to "List public events for a network of repositories", I'd search the dotnet-sdk repo for that, which would lead me to two references in the EventsRequestBuilder.cs file.

Then I can take this information and mirror the pattern given in the example CLI or in the README.md file, and end up with working code that looks like this:

var events = await gitHubClient.Networks["octokit"]["dotnet-sdk"].Events.GetAsync();
foreach (var @event in events)
{
    Console.WriteLine(@event.Id);
    Console.WriteLine(@event.Type);
    Console.WriteLine(@event.Actor.Login);
}

Does that make sense?

I recently found about the lib Octokit.Webhooks.AspNetCore, is it supposed to be used together with this GitHub.Octokit.SDK?

We haven't designed them this way, no. This SDK is generated from the OpenAPI specification as I mentioned above, and webhooks.net is hand-maintained (which means it gets less frequent updates).