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 7fee1fc

Browse filesBrowse files
author
Rob Rodrigues
committed
Add affiliation filter for collaborators
1 parent 8ababb6 commit 7fee1fc
Copy full SHA for 7fee1fc

File tree

Expand file treeCollapse file tree

7 files changed

+88
-19
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+88
-19
lines changed

‎src/main/java/org/kohsuke/github/GHRepository.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHRepository.java
+45-2Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,13 @@ public int getSize() {
814814
return size;
815815
}
816816

817+
/**
818+
* Affiliation of a repository collaborator
819+
*/
820+
public enum CollaboratorAffiliation {
821+
ALL, DIRECT, OUTSIDE
822+
}
823+
817824
/**
818825
* Gets the collaborators on this repository. This set always appear to include the owner.
819826
*
@@ -823,7 +830,7 @@ public int getSize() {
823830
*/
824831
@WithBridgeMethods(Set.class)
825832
public GHPersonSet<GHUser> getCollaborators() throws IOException {
826-
return new GHPersonSet<GHUser>(listCollaborators().toList());
833+
return new GHPersonSet<GHUser>(listCollaborators(CollaboratorAffiliation.ALL).toList());
827834
}
828835

829836
/**
@@ -834,7 +841,22 @@ public GHPersonSet<GHUser> getCollaborators() throws IOException {
834841
* the io exception
835842
*/
836843
public PagedIterable<GHUser> listCollaborators() throws IOException {
837-
return listUsers("collaborators");
844+
return listCollaborators(CollaboratorAffiliation.ALL);
845+
}
846+
847+
/**
848+
* Lists up the collaborators on this repository.
849+
*
850+
* @param affiliation
851+
* Filter users by affiliation
852+
* @return Users paged iterable
853+
* @throws IOException
854+
* the io exception
855+
*/
856+
public PagedIterable<GHUser> listCollaborators(CollaboratorAffiliation affiliation) throws IOException {
857+
Map<String, Object> args = new HashMap<>();
858+
args.put("affiliation", affiliation.toString().toLowerCase());
859+
return listUsers("collaborators", args);
838860
}
839861

840862
/**
@@ -873,10 +895,25 @@ public boolean hasAssignee(GHUser u) throws IOException {
873895
* the io exception
874896
*/
875897
public Set<String> getCollaboratorNames() throws IOException {
898+
return getCollaboratorNames(CollaboratorAffiliation.ALL);
899+
}
900+
901+
/**
902+
* Gets the names of the collaborators on this repository. This method deviates from the principle of this library
903+
* but it works a lot faster than {@link #getCollaborators()}.
904+
*
905+
* @param affiliation
906+
* Filter users by affiliation
907+
* @return the collaborator names
908+
* @throws IOException
909+
* the io exception
910+
*/
911+
public Set<String> getCollaboratorNames(CollaboratorAffiliation affiliation) throws IOException {
876912
Set<String> r = new HashSet<>();
877913
// no initializer - we just want to the logins
878914
PagedIterable<GHUser> users = root.createRequest()
879915
.withUrlPath(getApiTailUrl("collaborators"))
916+
.with("affiliation", affiliation.toString().toLowerCase())
880917
.toIterable(GHUser[].class, null);
881918
for (GHUser u : users.toArray()) {
882919
r.add(u.login);
@@ -2075,8 +2112,14 @@ public PagedIterable<GHStargazer> listStargazers2() {
20752112
}
20762113

20772114
private PagedIterable<GHUser> listUsers(final String suffix) {
2115+
Map<String, Object> defaultArgs = Collections.EMPTY_MAP;
2116+
return listUsers(suffix, defaultArgs);
2117+
}
2118+
2119+
private PagedIterable<GHUser> listUsers(final String suffix, Map<String, Object> args) {
20782120
return root.createRequest()
20792121
.withUrlPath(getApiTailUrl(suffix))
2122+
.with(args)
20802123
.toIterable(GHUser[].class, item -> item.wrapUp(root));
20812124
}
20822125

‎src/main/java/org/kohsuke/github/GitHubRequest.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GitHubRequest.java
+24-9Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private GitHubRequest(@Nonnull List<Entry> args,
7575

7676
/**
7777
* Create a new {@link Builder}.
78-
*
78+
*
7979
* @return a new {@link Builder}.
8080
*/
8181
public static Builder<?> newBuilder() {
@@ -165,7 +165,7 @@ public Map<String, Object> injectedMappingValues() {
165165

166166
/**
167167
* The base GitHub API URL for this request represented as a {@link String}
168-
*
168+
*
169169
* @return the url string
170170
*/
171171
@Nonnull
@@ -176,7 +176,7 @@ public String apiUrl() {
176176
/**
177177
* The url path to be added to the {@link #apiUrl()} for this request. If this does not start with a "/", it instead
178178
* represents the full url string for this request.
179-
*
179+
*
180180
* @return a url path or full url string
181181
*/
182182
@Nonnull
@@ -186,7 +186,7 @@ public String urlPath() {
186186

187187
/**
188188
* The content type to to be sent by this request.
189-
*
189+
*
190190
* @return the content type.
191191
*/
192192
@Nonnull
@@ -196,7 +196,7 @@ public String contentType() {
196196

197197
/**
198198
* The {@link InputStream} to be sent as the body of this request.
199-
*
199+
*
200200
* @return the {@link InputStream}.
201201
*/
202202
@CheckForNull
@@ -206,7 +206,7 @@ public InputStream body() {
206206

207207
/**
208208
* The {@link URL} for this request. This is the actual URL the {@link GitHubClient} will send this request to.
209-
*
209+
*
210210
* @return the request {@link URL}
211211
*/
212212
@Nonnull
@@ -216,7 +216,7 @@ public URL url() {
216216

217217
/**
218218
* Whether arguments for this request should be included in the URL or in the body of the request.
219-
*
219+
*
220220
* @return true if the arguements should be sent in the body of the request.
221221
*/
222222
public boolean inBody() {
@@ -226,7 +226,7 @@ public boolean inBody() {
226226
/**
227227
* Create a {@link Builder} from this request. Initial values of the builder will be the same as this
228228
* {@link GitHubRequest}.
229-
*
229+
*
230230
* @return a {@link Builder} based on this request.
231231
*/
232232
public Builder<?> toBuilder() {
@@ -346,7 +346,7 @@ private Builder(@Nonnull List<Entry> args,
346346

347347
/**
348348
* Builds a {@link GitHubRequest} from this builder.
349-
*
349+
*
350350
* @return a {@link GitHubRequest}
351351
* @throws MalformedURLException
352352
* if the GitHub API URL cannot be constructed
@@ -437,6 +437,21 @@ public B withPreview(String name) {
437437
return withHeader("Accept", name);
438438
}
439439

440+
/**
441+
* With requester.
442+
*
443+
* @param Map
444+
* map of key value pairs to add
445+
* @return the request builder
446+
*/
447+
public B with(Map<String, Object> map) {
448+
for (Map.Entry<String, Object> entry : map.entrySet()) {
449+
with(entry.getKey(), entry.getValue());
450+
}
451+
452+
return (B) this;
453+
}
454+
440455
/**
441456
* With requester.
442457
*

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

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/GHRepositoryTest.java
+11Lines changed: 11 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 org.apache.commons.io.IOUtils;
5+
import org.junit.Ignore;
56
import org.junit.Test;
67

78
import java.io.FileNotFoundException;
@@ -645,6 +646,16 @@ public void listCollaborators() throws Exception {
645646
assertThat(collaborators.size(), greaterThan(10));
646647
}
647648

649+
@Test
650+
@Ignore("Data not cached")
651+
public void listCollaboratorsFiltered() throws Exception {
652+
GHRepository repo = getRepository();
653+
List<GHUser> allCollaborators = repo.listCollaborators().toList();
654+
List<GHUser> filteredCollaborators = repo.listCollaborators(GHRepository.CollaboratorAffiliation.OUTSIDE)
655+
.toList();
656+
assertThat(allCollaborators.size(), greaterThan(filteredCollaborators.size()));
657+
}
658+
648659
@Test
649660
public void getCheckRuns() throws Exception {
650661
final int expectedCount = 8;

‎src/test/resources/org/kohsuke/github/AppTest/wiremock/testMembership/mappings/repos_hub4j-test-org_jenkins_collaborators-4.json

Copy file name to clipboardExpand all lines: src/test/resources/org/kohsuke/github/AppTest/wiremock/testMembership/mappings/repos_hub4j-test-org_jenkins_collaborators-4.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "bce97482-6a11-44e5-a112-29230b142636",
33
"name": "repos_hub4j-test-org_jenkins_collaborators",
44
"request": {
5-
"url": "/repos/hub4j-test-org/jenkins/collaborators",
5+
"url": "/repos/hub4j-test-org/jenkins/collaborators?affiliation=all",
66
"method": "GET",
77
"headers": {
88
"Accept": {
@@ -44,4 +44,4 @@
4444
"uuid": "bce97482-6a11-44e5-a112-29230b142636",
4545
"persistent": true,
4646
"insertionIndex": 4
47-
}
47+
}

‎src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/addCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-5-ddaa82.json

Copy file name to clipboardExpand all lines: src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/addCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-5-ddaa82.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "ddaa8229-c0ae-4df6-90ed-08425bfe71f2",
33
"name": "repos_hub4j-test-org_github-api_collaborators",
44
"request": {
5-
"url": "/repos/hub4j-test-org/github-api/collaborators",
5+
"url": "/repos/hub4j-test-org/github-api/collaborators?affiliation=all",
66
"method": "GET",
77
"headers": {
88
"Accept": {
@@ -38,4 +38,4 @@
3838
"uuid": "ddaa8229-c0ae-4df6-90ed-08425bfe71f2",
3939
"persistent": true,
4040
"insertionIndex": 5
41-
}
41+
}

‎src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/getCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-4.json

Copy file name to clipboardExpand all lines: src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/getCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-4.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "2b8badfb-52b8-4304-a9a5-66b80274e93d",
33
"name": "repos_hub4j-test-org_github-api_collaborators",
44
"request": {
5-
"url": "/repos/hub4j-test-org/github-api/collaborators",
5+
"url": "/repos/hub4j-test-org/github-api/collaborators?affiliation=all",
66
"method": "GET",
77
"headers": {
88
"Accept": {
@@ -44,4 +44,4 @@
4444
"uuid": "2b8badfb-52b8-4304-a9a5-66b80274e93d",
4545
"persistent": true,
4646
"insertionIndex": 4
47-
}
47+
}

‎src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/listCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-4.json

Copy file name to clipboardExpand all lines: src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/listCollaborators/mappings/repos_hub4j-test-org_github-api_collaborators-4.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "b0680d17-cd3b-4ec0-a857-d352c7167e94",
33
"name": "repos_hub4j-test-org_github-api_collaborators",
44
"request": {
5-
"url": "/repos/hub4j-test-org/github-api/collaborators",
5+
"url": "/repos/hub4j-test-org/github-api/collaborators?affiliation=all",
66
"method": "GET",
77
"headers": {
88
"Accept": {
@@ -44,4 +44,4 @@
4444
"uuid": "b0680d17-cd3b-4ec0-a857-d352c7167e94",
4545
"persistent": true,
4646
"insertionIndex": 4
47-
}
47+
}

0 commit comments

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