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

Fix Issue #576 collaborator by username #1431

Merged
merged 8 commits into from Aug 18, 2022
15 changes: 15 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Expand Up @@ -1022,6 +1022,21 @@ public Set<String> getCollaboratorNames(CollaboratorAffiliation affiliation) thr
return r;
}

/**
* Checks if the given user is a collaborator for this repository.
*
* @param user
* a {@link GHUser}
* @return true if the user is a collaborator for this repository
* @throws IOException
* the io exception
*/
public boolean isCollaborator(GHUser user) throws IOException {
return root().createRequest()
.withUrlPath(getApiTailUrl("collaborators/" + user.getLogin()))
.fetchHttpStatusCode() == 204;
}

/**
* Obtain permission for a given user in this repository.
*
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/kohsuke/github/GHRepositoryTest.java
Expand Up @@ -943,6 +943,13 @@ public void listCollaboratorsFiltered() throws Exception {
assertThat(filteredCollaborators.size(), lessThan(allCollaborators.size()));
}

@Test
public void userIsCollaborator() throws Exception {
GHRepository repo = getRepository();
GHUser collaborator = repo.listCollaborators().toList().get(0);
assertThat(repo.isCollaborator(collaborator), is(true));
}

@Test
public void getCheckRuns() throws Exception {
final int expectedCount = 8;
Expand Down
@@ -0,0 +1,55 @@
{
"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://avatars.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": "hub4jtestorgemail@could.be.null.com",
"twitter_username": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 50,
"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": 4,
"owned_private_repos": 4,
"private_gists": 0,
"disk_usage": 11980,
"collaborators": 0,
"billing_email": "kk@kohsuke.org",
"default_repository_permission": "none",
"members_can_create_repositories": false,
"two_factor_requirement_enabled": false,
"members_allowed_repository_creation_type": "none",
"members_can_create_public_repositories": false,
"members_can_create_private_repositories": false,
"members_can_create_internal_repositories": false,
"members_can_create_pages": true,
"members_can_fork_private_repositories": false,
"members_can_create_public_pages": true,
"members_can_create_private_pages": true,
"plan": {
"name": "free",
"space": 976562499,
"private_repos": 10000,
"filled_seats": 38,
"seats": 3
}
}

Large diffs are not rendered by default.