Skip to content

Commit 1cd1dbe

Browse files
committed
Add polymorphic deserialization for the GHAppInstallation account field
1 parent 362faaf commit 1cd1dbe

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

src/main/java/org/kohsuke/github/GHAppInstallation.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.kohsuke.github;
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.fasterxml.jackson.annotation.JsonSubTypes;
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
46
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
57
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
68
import org.kohsuke.github.internal.EnumUtils;
@@ -39,7 +41,10 @@ GHRepository[] getItems(GitHub root) {
3941
@JsonProperty("access_tokens_url")
4042
private String accessTokenUrl;
4143

42-
private GHUser account;
44+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
45+
@JsonSubTypes({ @JsonSubTypes.Type(value = GHUser.class, name = "User"),
46+
@JsonSubTypes.Type(value = GHOrganization.class, name = "Organization") })
47+
private GHPerson account;
4348
@JsonProperty("app_id")
4449
private long appId;
4550
private List<String> events;
@@ -122,7 +127,7 @@ public String getAccessTokenUrl() {
122127
* @return the account
123128
*/
124129
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
125-
public GHUser getAccount() {
130+
public GHPerson getAccount() {
126131
return account;
127132
}
128133

src/test/java/org/kohsuke/github/AppTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ public void testGetAppInstallations() throws Exception {
737737
final GHAppInstallation ghAppInstallation = appInstallation.toList().get(0);
738738
assertThat(ghAppInstallation.getAppId(), is(122478L));
739739
assertThat(ghAppInstallation.getAccount().getLogin(), is("t0m4uk1991"));
740+
assertThat(ghAppInstallation.getAccount(), instanceOf(GHUser.class));
740741
}
741742

742743
/**

src/test/java/org/kohsuke/github/GHAppTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,12 @@ public void listInstallationsSince() throws IOException, ParseException {
264264

265265
private void testAppInstallation(GHAppInstallation appInstallation) throws IOException {
266266
Map<String, GHPermissionType> appPermissions = appInstallation.getPermissions();
267-
GHUser appAccount = appInstallation.getAccount();
267+
GHPerson appAccount = appInstallation.getAccount();
268268

269269
assertThat(appInstallation.getId(), is((long) 11111111));
270270
assertThat(appAccount.getId(), is((long) 111111111));
271271
assertThat(appAccount.login, is("bogus"));
272+
assertThat(appAccount, instanceOf(GHOrganization.class));
272273
assertThat(appAccount.getType(), is("Organization"));
273274
assertThat(appInstallation.getRepositorySelection(), is(GHRepositorySelection.SELECTED));
274275
assertThat(appInstallation.getAccessTokenUrl(), endsWith("/app/installations/11111111/access_tokens"));

src/test/java/org/kohsuke/github/GHEventPayloadTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.hamcrest.Matchers.equalTo;
1919
import static org.hamcrest.Matchers.hasProperty;
2020
import static org.hamcrest.Matchers.hasToString;
21+
import static org.hamcrest.Matchers.instanceOf;
2122
import static org.hamcrest.Matchers.is;
2223
import static org.hamcrest.Matchers.lessThanOrEqualTo;
2324
import static org.hamcrest.Matchers.notNullValue;
@@ -59,6 +60,8 @@ public void InstallationCreatedEvent() throws Exception {
5960
assertThat(event.getAction(), is("created"));
6061
assertThat(event.getInstallation().getId(), is(43898337L));
6162
assertThat(event.getInstallation().getAccount().getLogin(), is("CronFire"));
63+
assertThat(event.getInstallation().getAccount(), instanceOf(GHOrganization.class));
64+
assertThat(event.getInstallation().getAccount().getType(), is("Organization"));
6265

6366
assertThat(event.getRepositories().isEmpty(), is(false));
6467
assertThat(event.getRepositories().get(0).getId(), is(1296269L));
@@ -84,6 +87,8 @@ public void InstallationDeletedEvent() throws Exception {
8487
assertThat(event.getAction(), is("deleted"));
8588
assertThat(event.getInstallation().getId(), is(2L));
8689
assertThat(event.getInstallation().getAccount().getLogin(), is("octocat"));
90+
assertThat(event.getInstallation().getAccount(), instanceOf(GHUser.class));
91+
assertThat(event.getInstallation().getAccount().getType(), is("User"));
8792

8893
assertThrows(IllegalStateException.class, () -> event.getRepositories().isEmpty());
8994
assertThat(event.getRawRepositories().isEmpty(), is(false));

0 commit comments

Comments
 (0)