Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AlmirJNR opened this issue Jun 7, 2024 · 3 comments
Open

Comments

@AlmirJNR
Copy link

AlmirJNR commented Jun 7, 2024

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:

  • var eventPayload = serializer.Deserialize<PullRequestEventPayload>(json);
    var activityPayload = eventPayload as ActivityPayload ?? (ActivityPayload)eventPayload;
  • var activityPayload = serializer.Deserialize<ActivityPayload>(json);

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

  • PullRequest
  • PullRequestMinimal
  • PullRequestWebhook

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.

Copy link

github-actions bot commented Jun 7, 2024

👋 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 AlmirJNR changed the title How do i deserialize a event payload from github app webhook json? How do i deserialize an event payload from github app webhook json? Jun 7, 2024
@AlmirJNR
Copy link
Author

AlmirJNR commented Jun 7, 2024

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:

  • GitHub.Octokit.SDK: 0.0.16
  • Octokit.Webhooks.AspNetCore: 2.2.1

@kfcampbell
Copy link
Member

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🔥 Backlog
Development

No branches or pull requests

2 participants