Skip to content

Add polymorphic deserialization for event account. #1522

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/java/org/kohsuke/github/GHAppInstallation.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.kohsuke.github;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.kohsuke.github.internal.EnumUtils;

Expand All @@ -26,7 +28,10 @@
* @see GHApp#getInstallationByUser(String) GHApp#getInstallationByUser(String)
*/
public class GHAppInstallation extends GHObject {
private GHUser account;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({ @JsonSubTypes.Type(value = GHUser.class, name = "User"),
@JsonSubTypes.Type(value = GHOrganization.class, name = "Organization") })
private GHPerson account;

@JsonProperty("access_tokens_url")
private String accessTokenUrl;
Expand Down Expand Up @@ -73,7 +78,7 @@ public void setRoot(GitHub root) {
* @return the account
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GHUser getAccount() {
public GHPerson getAccount() {
return account;
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/GHAppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void createTokenWithRepositories() throws IOException {

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

assertThat(appInstallation.getId(), is((long) 11111111));
assertThat(appAccount.getId(), is((long) 111111111));
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/org/kohsuke/github/GHEventPayloadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThrows;

// TODO: Auto-generated Javadoc
Expand Down Expand Up @@ -999,6 +1000,8 @@ public void InstallationEvent() throws Exception {
assertThat(event.getAction(), is("deleted"));
assertThat(event.getInstallation().getId(), is(2L));
assertThat(event.getInstallation().getAccount().getLogin(), is("octocat"));
assertThat(event.getInstallation().getAccount().getType(), is("User"));
assertThat(event.getInstallation().getAccount(), instanceOf(GHUser.class));

assertThat(event.getRepositories().get(0).getId(), is(1296269L));
assertThat(event.getRepositories().get(0).getNodeId(), is("MDEwOlJlcG9zaXRvcnkxMjk2MjY5"));
Expand All @@ -1016,6 +1019,29 @@ public void InstallationEvent() throws Exception {
* @throws Exception
* the exception
*/
@Test
@Payload("installation_organization")
public void InstallationInOrganizationEvent() throws IOException {
final GHEventPayload.Installation event = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
.build()
.parseEventPayload(payload.asReader(), GHEventPayload.Installation.class);

assertThat(event.getAction(), is("deleted"));
assertThat(event.getInstallation().getId(), is(2L));
assertThat(event.getInstallation().getAccount().getLogin(), is("github"));
assertThat(event.getInstallation().getAccount().getType(), is("Organization"));
assertThat(event.getInstallation().getAccount(), instanceOf(GHOrganization.class));

assertThat(event.getRepositories().get(0).getId(), is(1296269L));
assertThat(event.getRepositories().get(0).getNodeId(), is("MDEwOlJlcG9zaXRvcnkxMjk2MjY5"));
assertThat(event.getRepositories().get(0).getName(), is("Hello-World"));
assertThat(event.getRepositories().get(0).getFullName(), is("github/Hello-World"));
assertThat(event.getRepositories().get(0).isPrivate(), is(false));
assertThat(event.getRepositories().get(0).getOwner().getLogin(), is("github"));

assertThat(event.getSender().getLogin(), is("octocat"));
}

@Test
public void workflow_dispatch() throws Exception {
final GHEventPayload.WorkflowDispatch workflowDispatchPayload = GitHub.offline()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"action": "deleted",
"installation": {
"id": 2,
"account": {
"login": "github",
"id": 9919,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjE=",
"avatar_url": "https://avatars.githubusercontent.com/u/9919?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/github",
"html_url": "https://github.com/github",
"followers_url": "https://api.github.com/users/github/followers",
"following_url": "https://api.github.com/users/github/following{/other_user}",
"gists_url": "https://api.github.com/users/github/gists{/gist_id}",
"starred_url": "https://api.github.com/users/github/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/github/subscriptions",
"organizations_url": "https://api.github.com/users/github/orgs",
"repos_url": "https://api.github.com/users/github/repos",
"events_url": "https://api.github.com/users/github/events{/privacy}",
"received_events_url": "https://api.github.com/users/github/received_events",
"type": "Organization",
"site_admin": false
},
"repository_selection": "selected",
"access_tokens_url": "https://api.github.com/installations/2/access_tokens",
"repositories_url": "https://api.github.com/installation/repositories",
"html_url": "https://github.com/settings/installations/2",
"app_id": 5725,
"target_id": 3880403,
"target_type": "User",
"permissions": {
"metadata": "read",
"contents": "read",
"issues": "write"
},
"events": [
"push",
"pull_request"
],
"created_at": 1525109898,
"updated_at": 1525109899,
"single_file_name": "config.yml"
},
"repositories": [
{
"id": 1296269,
"node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5",
"name": "Hello-World",
"full_name": "octocat/Hello-World",
"private": false
}
],
"sender": {
"login": "octocat",
"id": 1,
"node_id": "MDQ6VXNlcjE=",
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"login": "github",
"id": 9919,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjE=",
"url": "https://api.github.com/orgs/github",
"repos_url": "https://api.github.com/orgs/github/repos",
"events_url": "https://api.github.com/orgs/github/events",
"hooks_url": "https://api.github.com/orgs/github/hooks",
"issues_url": "https://api.github.com/orgs/github/issues",
"members_url": "https://api.github.com/orgs/github/members{/member}",
"public_members_url": "https://api.github.com/orgs/github/public_members{/member}",
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"description": "A great organization",
"name": "github",
"company": "GitHub",
"blog": "https://github.com/blog",
"location": "San Francisco",
"email": "octocat@github.com",
"twitter_username": "github",
"is_verified": true,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 2,
"public_gists": 1,
"followers": 20,
"following": 0,
"html_url": "https://github.com/octocat",
"created_at": "2008-01-14T04:33:35Z",
"updated_at": "2014-03-03T18:58:10Z",
"type": "Organization",
"total_private_repos": 100,
"owned_private_repos": 100,
"private_gists": 81,
"disk_usage": 10000,
"collaborators": 8,
"billing_email": "mona@github.com",
"plan": {
"name": "Medium",
"space": 400,
"private_repos": 20,
"filled_seats": 4,
"seats": 5
},
"default_repository_permission": "read",
"members_can_create_repositories": true,
"two_factor_requirement_enabled": true,
"members_allowed_repository_creation_type": "all",
"members_can_create_public_repositories": false,
"members_can_create_private_repositories": false,
"members_can_create_internal_repositories": false,
"members_can_create_pages": true,
"members_can_fork_private_repositories": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"id": 1296269,
"node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5",
"name": "Hello-World",
"full_name": "github/Hello-World",
"private": false,
"owner": {
"login": "github",
"id": 9919,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjE=",
"avatar_url": "https://avatars.githubusercontent.com/u/9919?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/github",
"html_url": "https://github.com/github",
"followers_url": "https://api.github.com/users/github/followers",
"following_url": "https://api.github.com/users/github/following{/other_user}",
"gists_url": "https://api.github.com/users/github/gists{/gist_id}",
"starred_url": "https://api.github.com/users/github/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/github/subscriptions",
"organizations_url": "https://api.github.com/users/github/orgs",
"repos_url": "https://api.github.com/users/github/repos",
"events_url": "https://api.github.com/users/github/events{/privacy}",
"received_events_url": "https://api.github.com/users/github/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/github/Hello-World",
"description": "My first repository on GitHub!",
"fork": false,
"url": "https://api.github.com/repos/github/Hello-World",
"forks_url": "https://api.github.com/repos/github/Hello-World/forks",
"keys_url": "https://api.github.com/repos/github/Hello-World/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/github/Hello-World/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/github/Hello-World/teams",
"hooks_url": "https://api.github.com/repos/github/Hello-World/hooks",
"issue_events_url": "https://api.github.com/repos/github/Hello-World/issues/events{/number}",
"events_url": "https://api.github.com/repos/github/Hello-World/events",
"assignees_url": "https://api.github.com/repos/github/Hello-World/assignees{/user}",
"branches_url": "https://api.github.com/repos/github/Hello-World/branches{/branch}",
"tags_url": "https://api.github.com/repos/github/Hello-World/tags",
"blobs_url": "https://api.github.com/repos/github/Hello-World/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/github/Hello-World/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/github/Hello-World/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/github/Hello-World/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/github/Hello-World/statuses/{sha}",
"languages_url": "https://api.github.com/repos/github/Hello-World/languages",
"stargazers_url": "https://api.github.com/repos/github/Hello-World/stargazers",
"contributors_url": "https://api.github.com/repos/github/Hello-World/contributors",
"subscribers_url": "https://api.github.com/repos/github/Hello-World/subscribers",
"subscription_url": "https://api.github.com/repos/github/Hello-World/subscription",
"commits_url": "https://api.github.com/repos/github/Hello-World/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/github/Hello-World/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/github/Hello-World/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/github/Hello-World/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/github/Hello-World/contents/{+path}",
"compare_url": "https://api.github.com/repos/github/Hello-World/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/github/Hello-World/merges",
"archive_url": "https://api.github.com/repos/github/Hello-World/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/github/Hello-World/downloads",
"issues_url": "https://api.github.com/repos/github/Hello-World/issues{/number}",
"pulls_url": "https://api.github.com/repos/github/Hello-World/pulls{/number}",
"milestones_url": "https://api.github.com/repos/github/Hello-World/milestones{/number}",
"notifications_url": "https://api.github.com/repos/github/Hello-World/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/github/Hello-World/labels{/name}",
"releases_url": "https://api.github.com/repos/github/Hello-World/releases{/id}",
"deployments_url": "https://api.github.com/repos/github/Hello-World/deployments",
"created_at": "2011-01-26T19:01:12Z",
"updated_at": "2022-02-03T00:06:57Z",
"pushed_at": "2022-01-30T18:13:40Z",
"git_url": "git://github.com/github/Hello-World.git",
"ssh_url": "git@github.com:github/Hello-World.git",
"clone_url": "https://github.com/github/Hello-World.git",
"svn_url": "https://github.com/github/Hello-World",
"homepage": "",
"size": 1,
"stargazers_count": 1764,
"watchers_count": 1764,
"language": null,
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 1682,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 802,
"license": null,
"allow_forking": true,
"is_template": false,
"topics": [],
"visibility": "public",
"forks": 1682,
"open_issues": 802,
"watchers": 1764,
"default_branch": "master",
"permissions": {
"admin": false,
"maintain": false,
"push": false,
"triage": false,
"pull": true
},
"temp_clone_token": "",
"network_count": 1682,
"subscribers_count": 1731
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"login": "github",
"id": 583231,
"node_id": "MDQ6VXNlcjU4MzIzMQ==",
"avatar_url": "https://avatars.githubusercontent.com/u/583231?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/github",
"html_url": "https://github.com/github",
"followers_url": "https://api.github.com/users/github/followers",
"following_url": "https://api.github.com/users/github/following{/other_user}",
"gists_url": "https://api.github.com/users/github/gists{/gist_id}",
"starred_url": "https://api.github.com/users/github/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/github/subscriptions",
"organizations_url": "https://api.github.com/users/github/orgs",
"repos_url": "https://api.github.com/users/github/repos",
"events_url": "https://api.github.com/users/github/events{/privacy}",
"received_events_url": "https://api.github.com/users/github/received_events",
"type": "User",
"site_admin": false,
"name": "github",
"company": "@github",
"blog": "https://github.blog",
"location": "San Francisco",
"email": "info@github.com",
"hireable": null,
"bio": null,
"twitter_username": null,
"public_repos": 8,
"public_gists": 8,
"followers": 4752,
"following": 9,
"created_at": "2011-01-25T18:44:36Z",
"updated_at": "2022-01-24T15:08:43Z"
}
Loading