Skip to content

Commit

Permalink
Merge pull request #1237 from bloslo/feature/#1231
Browse files Browse the repository at this point in the history
Add delete webhook via id functionality.
  • Loading branch information
bitwiseman committed Sep 24, 2021
2 parents 1b926cc + 1e580da commit dae92fc
Show file tree
Hide file tree
Showing 32 changed files with 635 additions and 258 deletions.
12 changes: 12 additions & 0 deletions src/main/java/org/kohsuke/github/GHHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ public GHHook createHook(String name, Map<String, String> config, Collection<GHE
return wrap(hook);
}

/**
* Deletes hook.
*
* @param id
* the id
* @throws IOException
* the io exception
*/
public void deleteHook(int id) throws IOException {
root().createRequest().method("DELETE").withUrlPath(collection() + "/" + id).send();
}

abstract String collection();

abstract Class<? extends GHHook[]> collectionClass();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/kohsuke/github/GHOrganization.java
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,16 @@ public GHHook getHook(int id) throws IOException {
return GHHooks.orgContext(this).getHook(id);
}

/**
* Deletes hook.
*
* @param id
* @throws IOException
*/
public void deleteHook(int id) throws IOException {
GHHooks.orgContext(this).deleteHook(id);
}

/**
* See https://api.github.com/hooks for possible names and their configuration scheme. TODO: produce type-safe
* binding
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,18 @@ public GHHook getHook(int id) throws IOException {
return GHHooks.repoContext(this, owner).getHook(id);
}

/**
* Deletes hook.
*
* @param id
* the id
* @throws IOException
* the io exception
*/
public void deleteHook(int id) throws IOException {
GHHooks.repoContext(this, owner).deleteHook(id);
}

/**
* Sets {@link #getCompare(String, String)} to return a {@link GHCompare} that uses a paginated commit list instead
* of limiting to 250 results.
Expand Down
28 changes: 24 additions & 4 deletions src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.collect.Iterables;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -620,8 +621,8 @@ public void testCreateCommitComment() throws Exception {

@Test
public void tryHook() throws Exception {
GHOrganization o = gitHub.getOrganization(GITHUB_API_TEST_ORG);
GHRepository r = o.getRepository("github-api");
final GHOrganization o = gitHub.getOrganization(GITHUB_API_TEST_ORG);
final GHRepository r = o.getRepository("github-api");
try {
GHHook hook = r.createWebHook(new URL("http://www.google.com/"));
assertThat(hook.getName(), equalTo("web"));
Expand All @@ -638,6 +639,15 @@ public void tryHook() throws Exception {
assertThat(hook2.isActive(), equalTo(true));
hook2.ping();
hook2.delete();
final GHHook finalRepoHook = hook;
GHFileNotFoundException e = Assert.assertThrows(GHFileNotFoundException.class,
() -> r.getHook((int) finalRepoHook.getId()));
assertThat(e.getMessage(),
containsString("repos/hub4j-test-org/github-api/hooks/" + finalRepoHook.getId()));
assertThat(e.getMessage(), containsString("rest/reference/repos#get-a-repository-webhook"));

hook = r.createWebHook(new URL("http://www.google.com/"));
r.deleteHook((int) hook.getId());

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

final GHHook finalOrgHook = hook;
GHFileNotFoundException e2 = Assert.assertThrows(GHFileNotFoundException.class,
() -> o.getHook((int) finalOrgHook.getId()));
assertThat(e2.getMessage(), containsString("orgs/hub4j-test-org/hooks/" + finalOrgHook.getId()));
assertThat(e2.getMessage(), containsString("rest/reference/orgs#get-an-organization-webhook"));

hook = o.createWebHook(new URL("http://www.google.com/"));
o.deleteHook((int) hook.getId());

// System.out.println(hook);
} finally {
if (mockGitHub.isUseProxy()) {
r = getNonRecordingGitHub().getOrganization(GITHUB_API_TEST_ORG).getRepository("github-api");
for (GHHook h : r.getHooks()) {
GHRepository cleanupRepo = getNonRecordingGitHub().getOrganization(GITHUB_API_TEST_ORG)
.getRepository("github-api");
for (GHHook h : cleanupRepo.getHooks()) {
h.delete();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 13,
"public_repos": 19,
"public_gists": 0,
"followers": 0,
"following": 0,
Expand All @@ -31,7 +31,7 @@
"total_private_repos": 2,
"owned_private_repos": 2,
"private_gists": 0,
"disk_usage": 152,
"disk_usage": 11979,
"collaborators": 0,
"billing_email": "kk@kohsuke.org",
"default_repository_permission": "none",
Expand All @@ -44,7 +44,7 @@
"name": "free",
"space": 976562499,
"private_repos": 10000,
"filled_seats": 22,
"filled_seats": 26,
"seats": 3
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "Organization",
"id": 319833954,
"name": "web",
"active": true,
"events": [
"push"
],
"config": {
"url": "http://www.google.com/",
"insecure_ssl": "0",
"content_type": "form"
},
"updated_at": "2021-09-24T05:58:39Z",
"created_at": "2021-09-24T05:58:39Z",
"url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833954",
"ping_url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833954/pings",
"deliveries_url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833954/deliveries"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "Organization",
"id": 319833957,
"name": "web",
"active": true,
"events": [
"push"
],
"config": {
"url": "http://www.google.com/",
"insecure_ssl": "0",
"content_type": "form"
},
"updated_at": "2021-09-24T05:58:40Z",
"created_at": "2021-09-24T05:58:40Z",
"url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833957",
"ping_url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833957/pings",
"deliveries_url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833957/deliveries"
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "Organization",
"id": 319833954,
"name": "web",
"active": true,
"events": [
"push"
],
"config": {
"url": "http://www.google.com/",
"insecure_ssl": "0",
"content_type": "form"
},
"updated_at": "2021-09-24T05:58:39Z",
"created_at": "2021-09-24T05:58:39Z",
"url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833954",
"ping_url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833954/pings",
"deliveries_url": "https://api.github.com/orgs/hub4j-test-org/hooks/319833954/deliveries"
}
Loading

0 comments on commit dae92fc

Please sign in to comment.