|
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.databind.JsonMappingException;
|
4 | 4 | import com.google.common.collect.Sets;
|
| 5 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
5 | 6 | import org.apache.commons.io.IOUtils;
|
6 | 7 | import org.junit.Assert;
|
7 | 8 | import org.junit.Test;
|
|
13 | 14 | import java.io.FileNotFoundException;
|
14 | 15 | import java.io.IOException;
|
15 | 16 | import java.io.InputStream;
|
| 17 | +import java.net.URL; |
16 | 18 | import java.time.LocalDate;
|
17 | 19 | import java.util.*;
|
18 | 20 | import java.util.stream.Collectors;
|
@@ -1051,6 +1053,75 @@ public void getCollaborators() throws Exception {
|
1051 | 1053 | assertThat(collaborators.size(), greaterThan(0));
|
1052 | 1054 | }
|
1053 | 1055 |
|
| 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 | + |
1054 | 1125 | /**
|
1055 | 1126 | * Gets the refs.
|
1056 | 1127 | *
|
|
0 commit comments