diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 8fd467e7e..6d7ed95f2 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -814,6 +814,13 @@ public class GHRepository extends GHObject { return size; } + /** + * Affiliation of a repository collaborator + */ + public enum CollaboratorAffiliation { + ALL, DIRECT, OUTSIDE + } + /** * Gets the collaborators on this repository. This set always appear to include the owner. * @@ -823,7 +830,7 @@ public class GHRepository extends GHObject { */ @WithBridgeMethods(Set.class) public GHPersonSet getCollaborators() throws IOException { - return new GHPersonSet(listCollaborators().toList()); + return new GHPersonSet(listCollaborators(CollaboratorAffiliation.ALL).toList()); } /** @@ -834,7 +841,22 @@ public class GHRepository extends GHObject { * the io exception */ public PagedIterable listCollaborators() throws IOException { - return listUsers("collaborators"); + return listCollaborators(CollaboratorAffiliation.ALL); + } + + /** + * Lists up the collaborators on this repository. + * + * @param affiliation + * Filter users by affiliation + * @return Users paged iterable + * @throws IOException + * the io exception + */ + public PagedIterable listCollaborators(CollaboratorAffiliation affiliation) throws IOException { + Map args = new HashMap<>(); + args.put("affiliation", affiliation.toString().toLowerCase()); + return listUsers("collaborators", args); } /** @@ -873,10 +895,25 @@ public class GHRepository extends GHObject { * the io exception */ public Set getCollaboratorNames() throws IOException { + return getCollaboratorNames(CollaboratorAffiliation.ALL); + } + + /** + * Gets the names of the collaborators on this repository. This method deviates from the principle of this library + * but it works a lot faster than {@link #getCollaborators()}. + * + * @param affiliation + * Filter users by affiliation + * @return the collaborator names + * @throws IOException + * the io exception + */ + public Set getCollaboratorNames(CollaboratorAffiliation affiliation) throws IOException { Set r = new HashSet<>(); // no initializer - we just want to the logins PagedIterable users = root.createRequest() .withUrlPath(getApiTailUrl("collaborators")) + .with("affiliation", affiliation.toString().toLowerCase()) .toIterable(GHUser[].class, null); for (GHUser u : users.toArray()) { r.add(u.login); @@ -2075,8 +2112,14 @@ public class GHRepository extends GHObject { } private PagedIterable listUsers(final String suffix) { + Map defaultArgs = Collections.EMPTY_MAP; + return listUsers(suffix, defaultArgs); + } + + private PagedIterable listUsers(final String suffix, Map args) { return root.createRequest() .withUrlPath(getApiTailUrl(suffix)) + .with(args) .toIterable(GHUser[].class, item -> item.wrapUp(root)); } diff --git a/src/main/java/org/kohsuke/github/GitHubRequest.java b/src/main/java/org/kohsuke/github/GitHubRequest.java index 8661d10a8..61018f865 100644 --- a/src/main/java/org/kohsuke/github/GitHubRequest.java +++ b/src/main/java/org/kohsuke/github/GitHubRequest.java @@ -75,7 +75,7 @@ class GitHubRequest { /** * Create a new {@link Builder}. - * + * * @return a new {@link Builder}. */ public static Builder newBuilder() { @@ -165,7 +165,7 @@ class GitHubRequest { /** * The base GitHub API URL for this request represented as a {@link String} - * + * * @return the url string */ @Nonnull @@ -176,7 +176,7 @@ class GitHubRequest { /** * The url path to be added to the {@link #apiUrl()} for this request. If this does not start with a "/", it instead * represents the full url string for this request. - * + * * @return a url path or full url string */ @Nonnull @@ -186,7 +186,7 @@ class GitHubRequest { /** * The content type to to be sent by this request. - * + * * @return the content type. */ @Nonnull @@ -196,7 +196,7 @@ class GitHubRequest { /** * The {@link InputStream} to be sent as the body of this request. - * + * * @return the {@link InputStream}. */ @CheckForNull @@ -206,7 +206,7 @@ class GitHubRequest { /** * The {@link URL} for this request. This is the actual URL the {@link GitHubClient} will send this request to. - * + * * @return the request {@link URL} */ @Nonnull @@ -216,7 +216,7 @@ class GitHubRequest { /** * Whether arguments for this request should be included in the URL or in the body of the request. - * + * * @return true if the arguements should be sent in the body of the request. */ public boolean inBody() { @@ -226,7 +226,7 @@ class GitHubRequest { /** * Create a {@link Builder} from this request. Initial values of the builder will be the same as this * {@link GitHubRequest}. - * + * * @return a {@link Builder} based on this request. */ public Builder toBuilder() { @@ -346,7 +346,7 @@ class GitHubRequest { /** * Builds a {@link GitHubRequest} from this builder. - * + * * @return a {@link GitHubRequest} * @throws MalformedURLException * if the GitHub API URL cannot be constructed @@ -437,6 +437,21 @@ class GitHubRequest { return withHeader("Accept", name); } + /** + * With requester. + * + * @param Map + * map of key value pairs to add + * @return the request builder + */ + public B with(Map map) { + for (Map.Entry entry : map.entrySet()) { + with(entry.getKey(), entry.getValue()); + } + + return (B) this; + } + /** * With requester. * diff --git a/src/test/java/org/kohsuke/github/GHRepositoryTest.java b/src/test/java/org/kohsuke/github/GHRepositoryTest.java index 2ea784e18..5eb0038ea 100644 --- a/src/test/java/org/kohsuke/github/GHRepositoryTest.java +++ b/src/test/java/org/kohsuke/github/GHRepositoryTest.java @@ -2,6 +2,7 @@ package org.kohsuke.github; import com.fasterxml.jackson.databind.JsonMappingException; import org.apache.commons.io.IOUtils; +import org.junit.Ignore; import org.junit.Test; import java.io.FileNotFoundException; @@ -645,6 +646,16 @@ public class GHRepositoryTest extends AbstractGitHubWireMockTest { assertThat(collaborators.size(), greaterThan(10)); } + @Test + @Ignore("Data not cached") + public void listCollaboratorsFiltered() throws Exception { + GHRepository repo = getRepository(); + List allCollaborators = repo.listCollaborators().toList(); + List filteredCollaborators = repo.listCollaborators(GHRepository.CollaboratorAffiliation.OUTSIDE) + .toList(); + assertThat(allCollaborators.size(), greaterThan(filteredCollaborators.size())); + } + @Test public void getCheckRuns() throws Exception { final int expectedCount = 8; diff --git a/src/test/resources/org/kohsuke/github/AppTest/wiremock/testMembership/mappings/repos_hub4j-test-org_jenkins_collaborators-4.json b/src/test/resources/org/kohsuke/github/AppTest/wiremock/testMembership/mappings/repos_hub4j-test-org_jenkins_collaborators-4.json index 68656bb54..2977a3ac7 100644 --- a/src/test/resources/org/kohsuke/github/AppTest/wiremock/testMembership/mappings/repos_hub4j-test-org_jenkins_collaborators-4.json +++ b/src/test/resources/org/kohsuke/github/AppTest/wiremock/testMembership/mappings/repos_hub4j-test-org_jenkins_collaborators-4.json @@ -2,7 +2,7 @@ "id": "bce97482-6a11-44e5-a112-29230b142636", "name": "repos_hub4j-test-org_jenkins_collaborators", "request": { - "url": "/repos/hub4j-test-org/jenkins/collaborators", + "url": "/repos/hub4j-test-org/jenkins/collaborators?affiliation=all", "method": "GET", "headers": { "Accept": { @@ -44,4 +44,4 @@ "uuid": "bce97482-6a11-44e5-a112-29230b142636", "persistent": true, "insertionIndex": 4 -} \ No newline at end of file +} diff --git a/src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/addCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-5-ddaa82.json b/src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/addCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-5-ddaa82.json index d0c47d67c..abb0751cb 100644 --- a/src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/addCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-5-ddaa82.json +++ b/src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/addCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-5-ddaa82.json @@ -2,7 +2,7 @@ "id": "ddaa8229-c0ae-4df6-90ed-08425bfe71f2", "name": "repos_hub4j-test-org_github-api_collaborators", "request": { - "url": "/repos/hub4j-test-org/github-api/collaborators", + "url": "/repos/hub4j-test-org/github-api/collaborators?affiliation=all", "method": "GET", "headers": { "Accept": { @@ -38,4 +38,4 @@ "uuid": "ddaa8229-c0ae-4df6-90ed-08425bfe71f2", "persistent": true, "insertionIndex": 5 -} \ No newline at end of file +} diff --git a/src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/getCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-4.json b/src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/getCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-4.json index 12be7d0f4..b51323f2e 100644 --- a/src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/getCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-4.json +++ b/src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/getCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-4.json @@ -2,7 +2,7 @@ "id": "2b8badfb-52b8-4304-a9a5-66b80274e93d", "name": "repos_hub4j-test-org_github-api_collaborators", "request": { - "url": "/repos/hub4j-test-org/github-api/collaborators", + "url": "/repos/hub4j-test-org/github-api/collaborators?affiliation=all", "method": "GET", "headers": { "Accept": { @@ -44,4 +44,4 @@ "uuid": "2b8badfb-52b8-4304-a9a5-66b80274e93d", "persistent": true, "insertionIndex": 4 -} \ No newline at end of file +} diff --git a/src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/listCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-4.json b/src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/listCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-4.json index 2a64c7281..3f6b23699 100644 --- a/src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/listCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-4.json +++ b/src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/listCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-4.json @@ -2,7 +2,7 @@ "id": "b0680d17-cd3b-4ec0-a857-d352c7167e94", "name": "repos_hub4j-test-org_github-api_collaborators", "request": { - "url": "/repos/hub4j-test-org/github-api/collaborators", + "url": "/repos/hub4j-test-org/github-api/collaborators?affiliation=all", "method": "GET", "headers": { "Accept": { @@ -44,4 +44,4 @@ "uuid": "b0680d17-cd3b-4ec0-a857-d352c7167e94", "persistent": true, "insertionIndex": 4 -} \ No newline at end of file +}