From b8682615bb0c44e7d2fe657c79fb7537c6979e04 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Sun, 15 Sep 2024 00:59:08 -0700 Subject: [PATCH] Exercise GHHooks --- .../org/kohsuke/github/GHRepositoryTest.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/test/java/org/kohsuke/github/GHRepositoryTest.java b/src/test/java/org/kohsuke/github/GHRepositoryTest.java index ba0034464b..645f3e44cb 100644 --- a/src/test/java/org/kohsuke/github/GHRepositoryTest.java +++ b/src/test/java/org/kohsuke/github/GHRepositoryTest.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.JsonMappingException; import com.google.common.collect.Sets; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import org.apache.commons.io.IOUtils; import org.junit.Assert; import org.junit.Test; @@ -13,6 +14,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.net.URL; import java.time.LocalDate; import java.util.*; import java.util.stream.Collectors; @@ -1051,6 +1053,75 @@ public void getCollaborators() throws Exception { assertThat(collaborators.size(), greaterThan(0)); } + /** + * Gets the post commit hooks. + * + * @throws Exception + * the exception + */ + @Test + public void getPostCommitHooks() throws Exception { + GHRepository repo = getRepository(gitHub); + Set postcommitHooks = setupPostCommitHooks(repo); + assertThat(postcommitHooks, is(empty())); + } + + @SuppressFBWarnings(value = "DMI_COLLECTION_OF_URLS", + justification = "It causes a performance degradation, but we have already exposed it to the API") + private Set setupPostCommitHooks(final GHRepository repo) { + return new AbstractSet() { + private List getPostCommitHooks() { + try { + List r = new ArrayList<>(); + for (GHHook h : repo.getHooks()) { + if (h.getName().equals("web")) { + r.add(new URL(h.getConfig().get("url"))); + } + } + return r; + } catch (IOException e) { + throw new GHException("Failed to retrieve post-commit hooks", e); + } + } + + @Override + public Iterator iterator() { + return getPostCommitHooks().iterator(); + } + + @Override + public int size() { + return getPostCommitHooks().size(); + } + + @Override + public boolean add(URL url) { + try { + repo.createWebHook(url); + return true; + } catch (IOException e) { + throw new GHException("Failed to update post-commit hooks", e); + } + } + + @Override + public boolean remove(Object url) { + try { + String _url = ((URL) url).toExternalForm(); + for (GHHook h : repo.getHooks()) { + if (h.getName().equals("web") && h.getConfig().get("url").equals(_url)) { + h.delete(); + return true; + } + } + return false; + } catch (IOException e) { + throw new GHException("Failed to update post-commit hooks", e); + } + } + }; + } + /** * Gets the refs. *