Skip to content

Commit 4adf88d

Browse files
authored
Merge pull request #767 from XiongKezhi/installation-repository-event
Installation repository event
2 parents 410bac2 + 955690b commit 4adf88d

File tree

5 files changed

+372
-2
lines changed

5 files changed

+372
-2
lines changed

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

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.annotation.JsonSetter;
44
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
55

6+
import java.io.IOException;
67
import java.io.Reader;
78
import java.util.List;
89

@@ -207,6 +208,153 @@ void wrapUp(GitHub root) {
207208
}
208209
}
209210

211+
/**
212+
* An installation has been installed, uninstalled, or its permissions have been changed.
213+
*
214+
* @see <a href="https://developer.github.com/v3/activity/events/types/#installationevent">authoritative source</a>
215+
*/
216+
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
217+
public static class Installation extends GHEventPayload {
218+
private String action;
219+
private GHAppInstallation installation;
220+
private List<GHRepository> repositories;
221+
222+
/**
223+
* Gets action
224+
*
225+
* @return the action
226+
*/
227+
public String getAction() {
228+
return action;
229+
}
230+
231+
/**
232+
* Gets installation
233+
*
234+
* @return the installation
235+
*/
236+
public GHAppInstallation getInstallation() {
237+
return installation;
238+
}
239+
240+
/**
241+
* Gets repositories
242+
*
243+
* @return the repositories
244+
*/
245+
public List<GHRepository> getRepositories() {
246+
return repositories;
247+
};
248+
249+
@Override
250+
void wrapUp(GitHub root) {
251+
super.wrapUp(root);
252+
if (installation == null)
253+
throw new IllegalStateException(
254+
"Expected check_suite payload, but got something else. Maybe we've got another type of event?");
255+
else
256+
installation.wrapUp(root);
257+
258+
if (repositories != null && !repositories.isEmpty()) {
259+
try {
260+
for (GHRepository singleRepo : repositories) { // warp each of the repository
261+
singleRepo.wrap(root);
262+
singleRepo.populate();
263+
}
264+
} catch (IOException e) {
265+
throw new GHException("Failed to refresh repositories", e);
266+
}
267+
}
268+
}
269+
}
270+
271+
/**
272+
* A repository has been added or removed from an installation.
273+
*
274+
* @see <a href="https://developer.github.com/v3/activity/events/types/#installationrepositoriesevent">authoritative
275+
* source</a>
276+
*/
277+
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
278+
public static class InstallationRepositories extends GHEventPayload {
279+
private String action;
280+
private GHAppInstallation installation;
281+
private String repositorySelection;
282+
private List<GHRepository> repositoriesAdded;
283+
private List<GHRepository> repositoriesRemoved;
284+
285+
/**
286+
* Gets action
287+
*
288+
* @return the action
289+
*/
290+
public String getAction() {
291+
return action;
292+
}
293+
294+
/**
295+
* Gets installation
296+
*
297+
* @return the installation
298+
*/
299+
public GHAppInstallation getInstallation() {
300+
return installation;
301+
}
302+
303+
/**
304+
* Gets installation selection
305+
*
306+
* @return the installation selection
307+
*/
308+
public String getRepositorySelection() {
309+
return repositorySelection;
310+
}
311+
312+
/**
313+
* Gets repositories added
314+
*
315+
* @return the repositories
316+
*/
317+
public List<GHRepository> getRepositoriesAdded() {
318+
return repositoriesAdded;
319+
}
320+
321+
/**
322+
* Gets repositories removed
323+
*
324+
* @return the repositories
325+
*/
326+
public List<GHRepository> getRepositoriesRemoved() {
327+
return repositoriesRemoved;
328+
}
329+
330+
@Override
331+
void wrapUp(GitHub root) {
332+
super.wrapUp(root);
333+
if (installation == null)
334+
throw new IllegalStateException(
335+
"Expected check_suite payload, but got something else. Maybe we've got another type of event?");
336+
else
337+
installation.wrapUp(root);
338+
339+
List<GHRepository> repositories;
340+
if (action == "added")
341+
repositories = repositoriesAdded;
342+
else // action == "removed"
343+
repositories = repositoriesRemoved;
344+
345+
if (repositories != null && !repositories.isEmpty()) {
346+
try {
347+
for (GHRepository singleRepo : repositories) { // warp each of the repository
348+
singleRepo.wrap(root);
349+
singleRepo.populate();
350+
}
351+
} catch (IOException e) {
352+
throw new GHException("Failed to refresh repositories", e);
353+
}
354+
}
355+
}
356+
}
357+
210358
/**
211359
* A pull request status has changed.
212360
*

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
public class GHRepository extends GHObject {
6666
/* package almost final */ GitHub root;
6767

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

188+
/**
189+
* Gets node id
190+
*
191+
* @return the node id
192+
*/
193+
public String getNodeId() {
194+
return nodeId;
195+
}
196+
188197
/**
189198
* Gets description.
190199
*
@@ -2130,7 +2139,7 @@ public boolean remove(Object url) {
21302139

21312140
GHRepository wrap(GitHub root) {
21322141
this.root = root;
2133-
if (root.isOffline()) {
2142+
if (root.isOffline() && owner != null) {
21342143
owner.wrapUp(root);
21352144
}
21362145
return this;
@@ -2792,4 +2801,17 @@ public GHTagObject createTag(String tag, String message, String object, String t
27922801
.fetch(GHTagObject.class)
27932802
.wrap(this);
27942803
}
2804+
2805+
/**
2806+
* Populate this object.
2807+
*
2808+
* @throws java.io.IOException
2809+
* The IO exception
2810+
*/
2811+
void populate() throws IOException {
2812+
if (root.isOffline())
2813+
return; // can't populate if the root is offline
2814+
2815+
root.createRequest().withApiUrl(root.getApiUrl() + full_name).fetchInto(this).wrap(root);
2816+
}
27952817
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.junit.Test;
55

66
import java.text.SimpleDateFormat;
7+
import java.util.Collections;
78
import java.util.TimeZone;
89

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

396+
@Test
397+
@Payload("installation_repositories")
398+
public void InstallationRepositoriesEvent() throws Exception {
399+
GHEventPayload.InstallationRepositories event = GitHub.offline()
400+
.parseEventPayload(payload.asReader(), GHEventPayload.InstallationRepositories.class);
401+
402+
assertThat(event.getAction(), is("added"));
403+
assertThat(event.getInstallation().getId(), is(957387L));
404+
assertThat(event.getInstallation().getAccount().getLogin(), is("Codertocat"));
405+
assertThat(event.getRepositorySelection(), is("selected"));
406+
407+
assertThat(event.getRepositoriesAdded().get(0).getId(), is(186853007L));
408+
assertThat(event.getRepositoriesAdded().get(0).getNodeId(), is("MDEwOlJlcG9zaXRvcnkxODY4NTMwMDc="));
409+
assertThat(event.getRepositoriesAdded().get(0).getName(), is("Space"));
410+
assertThat(event.getRepositoriesAdded().get(0).getFullName(), is("Codertocat/Space"));
411+
assertThat(event.getRepositoriesAdded().get(0).isPrivate(), is(false));
412+
413+
assertThat(event.getRepositoriesRemoved(), is(Collections.emptyList()));
414+
assertThat(event.getSender().getLogin(), is("Codertocat"));
415+
}
416+
417+
@Test
418+
@Payload("installation")
419+
public void InstallationEvent() throws Exception {
420+
GHEventPayload.Installation event = GitHub.offline()
421+
.parseEventPayload(payload.asReader(), GHEventPayload.Installation.class);
422+
423+
assertThat(event.getAction(), is("deleted"));
424+
assertThat(event.getInstallation().getId(), is(2L));
425+
assertThat(event.getInstallation().getAccount().getLogin(), is("octocat"));
426+
427+
assertThat(event.getRepositories().get(0).getId(), is(1296269L));
428+
assertThat(event.getRepositories().get(0).getNodeId(), is("MDEwOlJlcG9zaXRvcnkxODY4NTMwMDc="));
429+
assertThat(event.getRepositories().get(0).getName(), is("Hello-World"));
430+
assertThat(event.getRepositories().get(0).getFullName(), is("octocat/Hello-World"));
431+
assertThat(event.getRepositories().get(0).isPrivate(), is(false));
432+
433+
assertThat(event.getSender().getLogin(), is("octocat"));
434+
}
395435
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"action": "deleted",
3+
"installation": {
4+
"id": 2,
5+
"account": {
6+
"login": "octocat",
7+
"id": 1,
8+
"node_id": "MDQ6VXNlcjE=",
9+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
10+
"gravatar_id": "",
11+
"url": "https://api.github.com/users/octocat",
12+
"html_url": "https://github.com/octocat",
13+
"followers_url": "https://api.github.com/users/octocat/followers",
14+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
15+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
16+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
17+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
18+
"organizations_url": "https://api.github.com/users/octocat/orgs",
19+
"repos_url": "https://api.github.com/users/octocat/repos",
20+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
21+
"received_events_url": "https://api.github.com/users/octocat/received_events",
22+
"type": "User",
23+
"site_admin": false
24+
},
25+
"repository_selection": "selected",
26+
"access_tokens_url": "https://api.github.com/installations/2/access_tokens",
27+
"repositories_url": "https://api.github.com/installation/repositories",
28+
"html_url": "https://github.com/settings/installations/2",
29+
"app_id": 5725,
30+
"target_id": 3880403,
31+
"target_type": "User",
32+
"permissions": {
33+
"metadata": "read",
34+
"contents": "read",
35+
"issues": "write"
36+
},
37+
"events": [
38+
"push",
39+
"pull_request"
40+
],
41+
"created_at": 1525109898,
42+
"updated_at": 1525109899,
43+
"single_file_name": "config.yml"
44+
},
45+
"repositories": [
46+
{
47+
"id": 1296269,
48+
"node_id": "MDEwOlJlcG9zaXRvcnkxODY4NTMwMDc=",
49+
"name": "Hello-World",
50+
"full_name": "octocat/Hello-World",
51+
"private": false
52+
}
53+
],
54+
"sender": {
55+
"login": "octocat",
56+
"id": 1,
57+
"node_id": "MDQ6VXNlcjE=",
58+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
59+
"gravatar_id": "",
60+
"url": "https://api.github.com/users/octocat",
61+
"html_url": "https://github.com/octocat",
62+
"followers_url": "https://api.github.com/users/octocat/followers",
63+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
64+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
65+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
66+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
67+
"organizations_url": "https://api.github.com/users/octocat/orgs",
68+
"repos_url": "https://api.github.com/users/octocat/repos",
69+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
70+
"received_events_url": "https://api.github.com/users/octocat/received_events",
71+
"type": "User",
72+
"site_admin": false
73+
}
74+
}

0 commit comments

Comments
 (0)