Skip to content

Commit

Permalink
Merge pull request #767 from XiongKezhi/installation-repository-event
Browse files Browse the repository at this point in the history
Installation repository event
  • Loading branch information
bitwiseman authored Apr 5, 2020
2 parents 410bac2 + 955690b commit 4adf88d
Show file tree
Hide file tree
Showing 5 changed files with 372 additions and 2 deletions.
148 changes: 148 additions & 0 deletions src/main/java/org/kohsuke/github/GHEventPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonSetter;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

Expand Down Expand Up @@ -207,6 +208,153 @@ void wrapUp(GitHub root) {
}
}

/**
* An installation has been installed, uninstalled, or its permissions have been changed.
*
* @see <a href="https://developer.github.com/v3/activity/events/types/#installationevent">authoritative source</a>
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
public static class Installation extends GHEventPayload {
private String action;
private GHAppInstallation installation;
private List<GHRepository> repositories;

/**
* Gets action
*
* @return the action
*/
public String getAction() {
return action;
}

/**
* Gets installation
*
* @return the installation
*/
public GHAppInstallation getInstallation() {
return installation;
}

/**
* Gets repositories
*
* @return the repositories
*/
public List<GHRepository> getRepositories() {
return repositories;
};

@Override
void wrapUp(GitHub root) {
super.wrapUp(root);
if (installation == null)
throw new IllegalStateException(
"Expected check_suite payload, but got something else. Maybe we've got another type of event?");
else
installation.wrapUp(root);

if (repositories != null && !repositories.isEmpty()) {
try {
for (GHRepository singleRepo : repositories) { // warp each of the repository
singleRepo.wrap(root);
singleRepo.populate();
}
} catch (IOException e) {
throw new GHException("Failed to refresh repositories", e);
}
}
}
}

/**
* A repository has been added or removed from an installation.
*
* @see <a href="https://developer.github.com/v3/activity/events/types/#installationrepositoriesevent">authoritative
* source</a>
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
public static class InstallationRepositories extends GHEventPayload {
private String action;
private GHAppInstallation installation;
private String repositorySelection;
private List<GHRepository> repositoriesAdded;
private List<GHRepository> repositoriesRemoved;

/**
* Gets action
*
* @return the action
*/
public String getAction() {
return action;
}

/**
* Gets installation
*
* @return the installation
*/
public GHAppInstallation getInstallation() {
return installation;
}

/**
* Gets installation selection
*
* @return the installation selection
*/
public String getRepositorySelection() {
return repositorySelection;
}

/**
* Gets repositories added
*
* @return the repositories
*/
public List<GHRepository> getRepositoriesAdded() {
return repositoriesAdded;
}

/**
* Gets repositories removed
*
* @return the repositories
*/
public List<GHRepository> getRepositoriesRemoved() {
return repositoriesRemoved;
}

@Override
void wrapUp(GitHub root) {
super.wrapUp(root);
if (installation == null)
throw new IllegalStateException(
"Expected check_suite payload, but got something else. Maybe we've got another type of event?");
else
installation.wrapUp(root);

List<GHRepository> repositories;
if (action == "added")
repositories = repositoriesAdded;
else // action == "removed"
repositories = repositoriesRemoved;

if (repositories != null && !repositories.isEmpty()) {
try {
for (GHRepository singleRepo : repositories) { // warp each of the repository
singleRepo.wrap(root);
singleRepo.populate();
}
} catch (IOException e) {
throw new GHException("Failed to refresh repositories", e);
}
}
}
}

/**
* A pull request status has changed.
*
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
public class GHRepository extends GHObject {
/* package almost final */ GitHub root;

private String description, homepage, name, full_name;
private String nodeId, description, homepage, name, full_name;
private String html_url; // this is the UI
/*
* The license information makes use of the preview API.
Expand Down Expand Up @@ -185,6 +185,15 @@ private static class GHRepoPermission {
boolean pull, push, admin;
}

/**
* Gets node id
*
* @return the node id
*/
public String getNodeId() {
return nodeId;
}

/**
* Gets description.
*
Expand Down Expand Up @@ -2130,7 +2139,7 @@ public boolean remove(Object url) {

GHRepository wrap(GitHub root) {
this.root = root;
if (root.isOffline()) {
if (root.isOffline() && owner != null) {
owner.wrapUp(root);
}
return this;
Expand Down Expand Up @@ -2792,4 +2801,17 @@ public GHTagObject createTag(String tag, String message, String object, String t
.fetch(GHTagObject.class)
.wrap(this);
}

/**
* Populate this object.
*
* @throws java.io.IOException
* The IO exception
*/
void populate() throws IOException {
if (root.isOffline())
return; // can't populate if the root is offline

root.createRequest().withApiUrl(root.getApiUrl() + full_name).fetchInto(this).wrap(root);
}
}
40 changes: 40 additions & 0 deletions src/test/java/org/kohsuke/github/GHEventPayloadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.TimeZone;

import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -392,4 +393,43 @@ public void checkSuiteEvent() throws Exception {
assertThat(checkSuite.getApp().getId(), is(29310L));
}

@Test
@Payload("installation_repositories")
public void InstallationRepositoriesEvent() throws Exception {
GHEventPayload.InstallationRepositories event = GitHub.offline()
.parseEventPayload(payload.asReader(), GHEventPayload.InstallationRepositories.class);

assertThat(event.getAction(), is("added"));
assertThat(event.getInstallation().getId(), is(957387L));
assertThat(event.getInstallation().getAccount().getLogin(), is("Codertocat"));
assertThat(event.getRepositorySelection(), is("selected"));

assertThat(event.getRepositoriesAdded().get(0).getId(), is(186853007L));
assertThat(event.getRepositoriesAdded().get(0).getNodeId(), is("MDEwOlJlcG9zaXRvcnkxODY4NTMwMDc="));
assertThat(event.getRepositoriesAdded().get(0).getName(), is("Space"));
assertThat(event.getRepositoriesAdded().get(0).getFullName(), is("Codertocat/Space"));
assertThat(event.getRepositoriesAdded().get(0).isPrivate(), is(false));

assertThat(event.getRepositoriesRemoved(), is(Collections.emptyList()));
assertThat(event.getSender().getLogin(), is("Codertocat"));
}

@Test
@Payload("installation")
public void InstallationEvent() throws Exception {
GHEventPayload.Installation event = GitHub.offline()
.parseEventPayload(payload.asReader(), GHEventPayload.Installation.class);

assertThat(event.getAction(), is("deleted"));
assertThat(event.getInstallation().getId(), is(2L));
assertThat(event.getInstallation().getAccount().getLogin(), is("octocat"));

assertThat(event.getRepositories().get(0).getId(), is(1296269L));
assertThat(event.getRepositories().get(0).getNodeId(), is("MDEwOlJlcG9zaXRvcnkxODY4NTMwMDc="));
assertThat(event.getRepositories().get(0).getName(), is("Hello-World"));
assertThat(event.getRepositories().get(0).getFullName(), is("octocat/Hello-World"));
assertThat(event.getRepositories().get(0).isPrivate(), is(false));

assertThat(event.getSender().getLogin(), is("octocat"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"action": "deleted",
"installation": {
"id": 2,
"account": {
"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
},
"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": "MDEwOlJlcG9zaXRvcnkxODY4NTMwMDc=",
"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
}
}
Loading

0 comments on commit 4adf88d

Please sign in to comment.