Skip to content

Commit

Permalink
Exercise GHHooks
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Sep 15, 2024
1 parent 46deac2 commit cd5d951
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/test/java/org/kohsuke/github/GHRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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;
Expand All @@ -13,6 +16,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;
Expand Down Expand Up @@ -1051,6 +1055,76 @@ 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<URL> 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<URL> setupPostCommitHooks(final GHRepository repo) {
return new AbstractSet<URL>() {
private List<URL> getPostCommitHooks() {
try {
List<URL> 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<URL> 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.
*
Expand Down

0 comments on commit cd5d951

Please sign in to comment.