Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b868261

Browse filesBrowse files
committed
Exercise GHHooks
1 parent 46deac2 commit b868261
Copy full SHA for b868261

File tree

Expand file treeCollapse file tree

1 file changed

+71
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+71
-0
lines changed

‎src/test/java/org/kohsuke/github/GHRepositoryTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/GHRepositoryTest.java
+71Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.JsonMappingException;
44
import com.google.common.collect.Sets;
5+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
56
import org.apache.commons.io.IOUtils;
67
import org.junit.Assert;
78
import org.junit.Test;
@@ -13,6 +14,7 @@
1314
import java.io.FileNotFoundException;
1415
import java.io.IOException;
1516
import java.io.InputStream;
17+
import java.net.URL;
1618
import java.time.LocalDate;
1719
import java.util.*;
1820
import java.util.stream.Collectors;
@@ -1051,6 +1053,75 @@ public void getCollaborators() throws Exception {
10511053
assertThat(collaborators.size(), greaterThan(0));
10521054
}
10531055

1056+
/**
1057+
* Gets the post commit hooks.
1058+
*
1059+
* @throws Exception
1060+
* the exception
1061+
*/
1062+
@Test
1063+
public void getPostCommitHooks() throws Exception {
1064+
GHRepository repo = getRepository(gitHub);
1065+
Set<URL> postcommitHooks = setupPostCommitHooks(repo);
1066+
assertThat(postcommitHooks, is(empty()));
1067+
}
1068+
1069+
@SuppressFBWarnings(value = "DMI_COLLECTION_OF_URLS",
1070+
justification = "It causes a performance degradation, but we have already exposed it to the API")
1071+
private Set<URL> setupPostCommitHooks(final GHRepository repo) {
1072+
return new AbstractSet<URL>() {
1073+
private List<URL> getPostCommitHooks() {
1074+
try {
1075+
List<URL> r = new ArrayList<>();
1076+
for (GHHook h : repo.getHooks()) {
1077+
if (h.getName().equals("web")) {
1078+
r.add(new URL(h.getConfig().get("url")));
1079+
}
1080+
}
1081+
return r;
1082+
} catch (IOException e) {
1083+
throw new GHException("Failed to retrieve post-commit hooks", e);
1084+
}
1085+
}
1086+
1087+
@Override
1088+
public Iterator<URL> iterator() {
1089+
return getPostCommitHooks().iterator();
1090+
}
1091+
1092+
@Override
1093+
public int size() {
1094+
return getPostCommitHooks().size();
1095+
}
1096+
1097+
@Override
1098+
public boolean add(URL url) {
1099+
try {
1100+
repo.createWebHook(url);
1101+
return true;
1102+
} catch (IOException e) {
1103+
throw new GHException("Failed to update post-commit hooks", e);
1104+
}
1105+
}
1106+
1107+
@Override
1108+
public boolean remove(Object url) {
1109+
try {
1110+
String _url = ((URL) url).toExternalForm();
1111+
for (GHHook h : repo.getHooks()) {
1112+
if (h.getName().equals("web") && h.getConfig().get("url").equals(_url)) {
1113+
h.delete();
1114+
return true;
1115+
}
1116+
}
1117+
return false;
1118+
} catch (IOException e) {
1119+
throw new GHException("Failed to update post-commit hooks", e);
1120+
}
1121+
}
1122+
};
1123+
}
1124+
10541125
/**
10551126
* Gets the refs.
10561127
*

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.