github-api icon indicating copy to clipboard operation
github-api copied to clipboard

GHUser even when event comes from Organization type

Open Jalmeida1994 opened this issue 3 years ago • 3 comments

Describe the bug A GHUser type is returned even when in the payload the account type is Organization.

More precisely in the event installation.created when the installation.account.type is set to Organization (app installed in organization) the installation.account in the @Installation.Created payload returns a GHUser.

{
  "action": "created",
  "installation": {
    "id": 12345,
    "account": {
      "login": "org-name",
      <...>
      "type": "Organization",
      "site_admin": false
    },
<...>
fun onInstall(@Installation.Created installationPayload: GHEventPayload.Installation) {
  val tenantGHOrganization = installationPayload.installation.account // <-- GHUser instance
}

Also tried to use the installationPayload.organization object but it always returned null (probably it's used in other events):

fun onInstall(@Installation.Created installationPayload: GHEventPayload.Installation) {
  LOG.info("org: " + installationPayload.organization.name) // <--  Cannot invoke "org.kohsuke.github.GHOrganization.getName()" because the return value of "org.kohsuke.github.GHEventPayload$Installation.getOrganization()" is null
}

To Reproduce Steps to reproduce the behavior:

  1. Create an event with installation.created in an organization (install app in an organization)
  2. Catch the event
  3. Retrieve the installationPayload.installation.account object
  4. Or retrieve the installationPayload.organization object

Expected behavior If the payload has the installation.account.type set to Organization the installationPayload.installation.accountshould return a GHOrganization object or the installationPayload.organization should not return null.

Desktop (please complete the following information):

  • OS: macOS Monterey v12.4 (Apple M1)

Additional context Using Kotlin and Quarkus GitHub App

Jalmeida1994 avatar Aug 02 '22 09:08 Jalmeida1994

I checked the GHAppInstallation.java file and checked that the field is indeed a GHUser:

public class GHAppInstallation extends GHObject {
    private GHUser account;

<...>

    /**
     * Gets account.
     *
     * @return the account
     */
    @SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
    public GHUser getAccount() {
        return account;
    }

As this is Jackson binding, I don't think we can do a simple if statement. Probably change the account to GHPerson and add the code to change to GHUser or GHOrganization later?

Jalmeida1994 avatar Aug 02 '22 09:08 Jalmeida1994

Probably a similar problem to https://github.com/hub4j/github-api/issues/126 .

As this is Jackson binding, I don't think we can do a simple if statement.

We don't want to deserialize aa GHPerson because we'd end up losing data - GHUser and GHIOrganization have fields not present on GHPerson.

Jackson has functionality to allow you to switch types based on a field, but I've never used it. The SO answer below has an example of how to do this.

https://stackoverflow.com/questions/30362446/deserialize-json-with-jackson-into-polymorphic-types-a-complete-example-is-giv

bitwiseman avatar Aug 08 '22 16:08 bitwiseman

You're completely right. Hmm I may have a look at it. Thanks @bitwiseman!

Jalmeida1994 avatar Aug 16 '22 08:08 Jalmeida1994