Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add affiliation filter for collaborators #981

Merged
merged 7 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Example:

This the default behavior.

Example for a single test case:

`mvn install -Dtest=WireMockStatusReporterTest#user_whenProxying_AuthCorrectlyConfigured`


### Setting up credential

Expand Down
51 changes: 48 additions & 3 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,13 @@ public int getSize() {
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.
*
Expand All @@ -841,6 +848,19 @@ public PagedIterable<GHUser> listCollaborators() throws IOException {
return listUsers("collaborators");
}

/**
* Lists up the collaborators on this repository.
*
* @param affiliation
* Filter users by affiliation
* @return Users paged iterable
* @throws IOException
* the io exception
*/
public PagedIterable<GHUser> listCollaborators(CollaboratorAffiliation affiliation) throws IOException {
return listUsers(root.createRequest().with("affiliation", affiliation), "collaborators");
}

/**
* Lists all
* <a href="https://help.github.com/articles/assigning-issues-and-pull-requests-to-other-github-users/">the
Expand Down Expand Up @@ -888,6 +908,29 @@ public Set<String> getCollaboratorNames() throws IOException {
return r;
}

/**
* 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<String> getCollaboratorNames(CollaboratorAffiliation affiliation) throws IOException {
Set<String> r = new HashSet<>();
// no initializer - we just want to the logins
PagedIterable<GHUser> users = root.createRequest()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably also use listUsers(...) right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listUsers does a few extra things, so that would negatively impact the speed of what is ostensibly the "fast" alternative. If that's a desirable change, I'm happy to make it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the fast alternative, just a convenient one. Go ahead and change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the javadoc in error and I should update that while I'm in here?

    /**
     * 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()}.
     *
     * @return the collaborator names
     * @throws IOException
     *             the io exception
     */
    public Set<String> getCollaboratorNames() throws IOException```

.withUrlPath(getApiTailUrl("collaborators"))
.with("affiliation", affiliation)
.toIterable(GHUser[].class, null);
for (GHUser u : users.toArray()) {
r.add(u.login);
}
return r;
}

/**
* Obtain permission for a given user in this repository.
*
Expand Down Expand Up @@ -2092,9 +2135,11 @@ public PagedIterable<GHStargazer> listStargazers2() {
}

private PagedIterable<GHUser> listUsers(final String suffix) {
return root.createRequest()
.withUrlPath(getApiTailUrl(suffix))
.toIterable(GHUser[].class, item -> item.wrapUp(root));
return listUsers(root.createRequest(), suffix);
}

private PagedIterable<GHUser> listUsers(Requester requester, final String suffix) {
return requester.withUrlPath(getApiTailUrl(suffix)).toIterable(GHUser[].class, item -> item.wrapUp(root));
}

/**
Expand Down
33 changes: 24 additions & 9 deletions src/main/java/org/kohsuke/github/GitHubRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private GitHubRequest(@Nonnull List<Entry> args,

/**
* Create a new {@link Builder}.
*
*
Irialad marked this conversation as resolved.
Show resolved Hide resolved
* @return a new {@link Builder}.
*/
public static Builder<?> newBuilder() {
Expand Down Expand Up @@ -165,7 +165,7 @@ public Map<String, Object> injectedMappingValues() {

/**
* The base GitHub API URL for this request represented as a {@link String}
*
*
* @return the url string
*/
@Nonnull
Expand All @@ -176,7 +176,7 @@ public String apiUrl() {
/**
* 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
Expand All @@ -186,7 +186,7 @@ public String urlPath() {

/**
* The content type to to be sent by this request.
*
*
* @return the content type.
*/
@Nonnull
Expand All @@ -196,7 +196,7 @@ public String contentType() {

/**
* The {@link InputStream} to be sent as the body of this request.
*
*
* @return the {@link InputStream}.
*/
@CheckForNull
Expand All @@ -206,7 +206,7 @@ public InputStream body() {

/**
* 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
Expand All @@ -216,7 +216,7 @@ public URL url() {

/**
* 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() {
Expand All @@ -226,7 +226,7 @@ public boolean inBody() {
/**
* 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() {
Expand Down Expand Up @@ -346,7 +346,7 @@ private Builder(@Nonnull List<Entry> args,

/**
* Builds a {@link GitHubRequest} from this builder.
*
*
* @return a {@link GitHubRequest}
* @throws MalformedURLException
* if the GitHub API URL cannot be constructed
Expand Down Expand Up @@ -437,6 +437,21 @@ public B withPreview(String name) {
return withHeader("Accept", name);
}

/**
* With requester.
*
* @param Map
* map of key value pairs to add
* @return the request builder
*/
public B with(Map<String, Object> map) {
Irialad marked this conversation as resolved.
Show resolved Hide resolved
for (Map.Entry<String, Object> entry : map.entrySet()) {
with(entry.getKey(), entry.getValue());
}

return (B) this;
}

/**
* With requester.
*
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/kohsuke/github/GHRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,15 @@ public void listCollaborators() throws Exception {
assertThat(collaborators.size(), greaterThan(10));
}

@Test
public void listCollaboratorsFiltered() throws Exception {
GHRepository repo = getRepository();
List<GHUser> allCollaborators = repo.listCollaborators().toList();
List<GHUser> filteredCollaborators = repo.listCollaborators(GHRepository.CollaboratorAffiliation.OUTSIDE)
.toList();
assertThat(filteredCollaborators.size(), lessThan(allCollaborators.size()));
}

@Test
public void getCheckRuns() throws Exception {
final int expectedCount = 8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
"uuid": "bce97482-6a11-44e5-a112-29230b142636",
"persistent": true,
"insertionIndex": 4
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@
"uuid": "ddaa8229-c0ae-4df6-90ed-08425bfe71f2",
"persistent": true,
"insertionIndex": 5
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
"uuid": "2b8badfb-52b8-4304-a9a5-66b80274e93d",
"persistent": true,
"insertionIndex": 4
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
"uuid": "b0680d17-cd3b-4ec0-a857-d352c7167e94",
"persistent": true,
"insertionIndex": 4
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"url": "https://api.github.com/orgs/hub4j-test-org",
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
"description": "Hub4j Test Org Description (this could be null or blank too)",
"name": "Hub4j Test Org Name (this could be null or blank too)",
"company": null,
"blog": "https://hub4j.url.io/could/be/null",
"location": "Hub4j Test Org Location (this could be null or blank too)",
"email": "[email protected]",
"twitter_username": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 13,
"public_gists": 0,
"followers": 0,
"following": 0,
"html_url": "https://github.com/hub4j-test-org",
"created_at": "2014-05-10T19:39:11Z",
"updated_at": "2020-06-04T05:56:10Z",
"type": "Organization",
"total_private_repos": 2,
"owned_private_repos": 2,
"private_gists": 0,
"disk_usage": 152,
"collaborators": 0,
"billing_email": "[email protected]",
"default_repository_permission": "none",
"members_can_create_repositories": false,
"two_factor_requirement_enabled": false,
"members_can_create_pages": true,
"plan": {
"name": "free",
"space": 976562499,
"private_repos": 10000,
"filled_seats": 21,
"seats": 3
}
}
Loading