Skip to content

Commit dae92fc

Browse files
authored
Merge pull request #1237 from bloslo/feature/#1231
Add delete webhook via id functionality.
2 parents 1b926cc + 1e580da commit dae92fc

32 files changed

+635
-258
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ public GHHook createHook(String name, Map<String, String> config, Collection<GHE
8787
return wrap(hook);
8888
}
8989

90+
/**
91+
* Deletes hook.
92+
*
93+
* @param id
94+
* the id
95+
* @throws IOException
96+
* the io exception
97+
*/
98+
public void deleteHook(int id) throws IOException {
99+
root().createRequest().method("DELETE").withUrlPath(collection() + "/" + id).send();
100+
}
101+
90102
abstract String collection();
91103

92104
abstract Class<? extends GHHook[]> collectionClass();

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,16 @@ public GHHook getHook(int id) throws IOException {
640640
return GHHooks.orgContext(this).getHook(id);
641641
}
642642

643+
/**
644+
* Deletes hook.
645+
*
646+
* @param id
647+
* @throws IOException
648+
*/
649+
public void deleteHook(int id) throws IOException {
650+
GHHooks.orgContext(this).deleteHook(id);
651+
}
652+
643653
/**
644654
* See https://api.github.com/hooks for possible names and their configuration scheme. TODO: produce type-safe
645655
* binding

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,18 @@ public GHHook getHook(int id) throws IOException {
16481648
return GHHooks.repoContext(this, owner).getHook(id);
16491649
}
16501650

1651+
/**
1652+
* Deletes hook.
1653+
*
1654+
* @param id
1655+
* the id
1656+
* @throws IOException
1657+
* the io exception
1658+
*/
1659+
public void deleteHook(int id) throws IOException {
1660+
GHHooks.repoContext(this, owner).deleteHook(id);
1661+
}
1662+
16511663
/**
16521664
* Sets {@link #getCompare(String, String)} to return a {@link GHCompare} that uses a paginated commit list instead
16531665
* of limiting to 250 results.

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.google.common.collect.Iterables;
55
import org.apache.commons.io.IOUtils;
66
import org.apache.commons.lang3.SystemUtils;
7+
import org.junit.Assert;
78
import org.junit.Assume;
89
import org.junit.Ignore;
910
import org.junit.Test;
@@ -620,8 +621,8 @@ public void testCreateCommitComment() throws Exception {
620621

621622
@Test
622623
public void tryHook() throws Exception {
623-
GHOrganization o = gitHub.getOrganization(GITHUB_API_TEST_ORG);
624-
GHRepository r = o.getRepository("github-api");
624+
final GHOrganization o = gitHub.getOrganization(GITHUB_API_TEST_ORG);
625+
final GHRepository r = o.getRepository("github-api");
625626
try {
626627
GHHook hook = r.createWebHook(new URL("http://www.google.com/"));
627628
assertThat(hook.getName(), equalTo("web"));
@@ -638,6 +639,15 @@ public void tryHook() throws Exception {
638639
assertThat(hook2.isActive(), equalTo(true));
639640
hook2.ping();
640641
hook2.delete();
642+
final GHHook finalRepoHook = hook;
643+
GHFileNotFoundException e = Assert.assertThrows(GHFileNotFoundException.class,
644+
() -> r.getHook((int) finalRepoHook.getId()));
645+
assertThat(e.getMessage(),
646+
containsString("repos/hub4j-test-org/github-api/hooks/" + finalRepoHook.getId()));
647+
assertThat(e.getMessage(), containsString("rest/reference/repos#get-a-repository-webhook"));
648+
649+
hook = r.createWebHook(new URL("http://www.google.com/"));
650+
r.deleteHook((int) hook.getId());
641651

642652
hook = o.createWebHook(new URL("http://www.google.com/"));
643653
assertThat(hook.getName(), equalTo("web"));
@@ -655,11 +665,21 @@ public void tryHook() throws Exception {
655665
hook2.ping();
656666
hook2.delete();
657667

668+
final GHHook finalOrgHook = hook;
669+
GHFileNotFoundException e2 = Assert.assertThrows(GHFileNotFoundException.class,
670+
() -> o.getHook((int) finalOrgHook.getId()));
671+
assertThat(e2.getMessage(), containsString("orgs/hub4j-test-org/hooks/" + finalOrgHook.getId()));
672+
assertThat(e2.getMessage(), containsString("rest/reference/orgs#get-an-organization-webhook"));
673+
674+
hook = o.createWebHook(new URL("http://www.google.com/"));
675+
o.deleteHook((int) hook.getId());
676+
658677
// System.out.println(hook);
659678
} finally {
660679
if (mockGitHub.isUseProxy()) {
661-
r = getNonRecordingGitHub().getOrganization(GITHUB_API_TEST_ORG).getRepository("github-api");
662-
for (GHHook h : r.getHooks()) {
680+
GHRepository cleanupRepo = getNonRecordingGitHub().getOrganization(GITHUB_API_TEST_ORG)
681+
.getRepository("github-api");
682+
for (GHHook h : cleanupRepo.getHooks()) {
663683
h.delete();
664684
}
665685
}

src/test/resources/org/kohsuke/github/AppTest/wiremock/tryHook/__files/orgs_hub4j-test-org-2.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"is_verified": false,
2121
"has_organization_projects": true,
2222
"has_repository_projects": true,
23-
"public_repos": 13,
23+
"public_repos": 19,
2424
"public_gists": 0,
2525
"followers": 0,
2626
"following": 0,
@@ -31,7 +31,7 @@
3131
"total_private_repos": 2,
3232
"owned_private_repos": 2,
3333
"private_gists": 0,
34-
"disk_usage": 152,
34+
"disk_usage": 11979,
3535
"collaborators": 0,
3636
"billing_email": "[email protected]",
3737
"default_repository_permission": "none",
@@ -44,7 +44,7 @@
4444
"name": "free",
4545
"space": 976562499,
4646
"private_repos": 10000,
47-
"filled_seats": 22,
47+
"filled_seats": 26,
4848
"seats": 3
4949
}
5050
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"type": "Organization",
3+
"id": 319833954,
4+
"name": "web",
5+
"active": true,
6+
"events": [
7+
"push"
8+
],
9+
"config": {
10+
"url": "http://www.google.com/",
11+
"insecure_ssl": "0",
12+
"content_type": "form"
13+
},
14+
"updated_at": "2021-09-24T05:58:39Z",
15+
"created_at": "2021-09-24T05:58:39Z",
16+
"url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833954",
17+
"ping_url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833954/pings",
18+
"deliveries_url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833954/deliveries"
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"type": "Organization",
3+
"id": 319833957,
4+
"name": "web",
5+
"active": true,
6+
"events": [
7+
"push"
8+
],
9+
"config": {
10+
"url": "http://www.google.com/",
11+
"insecure_ssl": "0",
12+
"content_type": "form"
13+
},
14+
"updated_at": "2021-09-24T05:58:40Z",
15+
"created_at": "2021-09-24T05:58:40Z",
16+
"url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833957",
17+
"ping_url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833957/pings",
18+
"deliveries_url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833957/deliveries"
19+
}

src/test/resources/org/kohsuke/github/AppTest/wiremock/tryHook/__files/orgs_hub4j-test-org_hooks-8.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/test/resources/org/kohsuke/github/AppTest/wiremock/tryHook/__files/orgs_hub4j-test-org_hooks_276991250-9.json

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"type": "Organization",
3+
"id": 319833954,
4+
"name": "web",
5+
"active": true,
6+
"events": [
7+
"push"
8+
],
9+
"config": {
10+
"url": "http://www.google.com/",
11+
"insecure_ssl": "0",
12+
"content_type": "form"
13+
},
14+
"updated_at": "2021-09-24T05:58:39Z",
15+
"created_at": "2021-09-24T05:58:39Z",
16+
"url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833954",
17+
"ping_url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833954/pings",
18+
"deliveries_url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833954/deliveries"
19+
}

0 commit comments

Comments
 (0)