Skip to content

Commit 49982a2

Browse files
authored
Merge pull request #1431 from 0xacx/Issue-576-collaborator-by-username
Fix Issue #576 collaborator by username
2 parents f540664 + be1aaa5 commit 49982a2

13 files changed

+1864
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,21 @@ public Set<String> getCollaboratorNames(CollaboratorAffiliation affiliation) thr
10221022
return r;
10231023
}
10241024

1025+
/**
1026+
* Checks if the given user is a collaborator for this repository.
1027+
*
1028+
* @param user
1029+
* a {@link GHUser}
1030+
* @return true if the user is a collaborator for this repository
1031+
* @throws IOException
1032+
* the io exception
1033+
*/
1034+
public boolean isCollaborator(GHUser user) throws IOException {
1035+
return root().createRequest()
1036+
.withUrlPath(getApiTailUrl("collaborators/" + user.getLogin()))
1037+
.fetchHttpStatusCode() == 204;
1038+
}
1039+
10251040
/**
10261041
* Obtain permission for a given user in this repository.
10271042
*

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,13 @@ public void listCollaboratorsFiltered() throws Exception {
943943
assertThat(filteredCollaborators.size(), lessThan(allCollaborators.size()));
944944
}
945945

946+
@Test
947+
public void userIsCollaborator() throws Exception {
948+
GHRepository repo = getRepository();
949+
GHUser collaborator = repo.listCollaborators().toList().get(0);
950+
assertThat(repo.isCollaborator(collaborator), is(true));
951+
}
952+
946953
@Test
947954
public void getCheckRuns() throws Exception {
948955
final int expectedCount = 8;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"login": "hub4j-test-org",
3+
"id": 7544739,
4+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
5+
"url": "https://api.github.com/orgs/hub4j-test-org",
6+
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
7+
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
8+
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
9+
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
10+
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
11+
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
12+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
13+
"description": "Hub4j Test Org Description (this could be null or blank too)",
14+
"name": "Hub4j Test Org Name (this could be null or blank too)",
15+
"company": null,
16+
"blog": "https://hub4j.url.io/could/be/null",
17+
"location": "Hub4j Test Org Location (this could be null or blank too)",
18+
"email": "[email protected]",
19+
"twitter_username": null,
20+
"is_verified": false,
21+
"has_organization_projects": true,
22+
"has_repository_projects": true,
23+
"public_repos": 50,
24+
"public_gists": 0,
25+
"followers": 0,
26+
"following": 0,
27+
"html_url": "https://github.com/hub4j-test-org",
28+
"created_at": "2014-05-10T19:39:11Z",
29+
"updated_at": "2020-06-04T05:56:10Z",
30+
"type": "Organization",
31+
"total_private_repos": 4,
32+
"owned_private_repos": 4,
33+
"private_gists": 0,
34+
"disk_usage": 11980,
35+
"collaborators": 0,
36+
"billing_email": "[email protected]",
37+
"default_repository_permission": "none",
38+
"members_can_create_repositories": false,
39+
"two_factor_requirement_enabled": false,
40+
"members_allowed_repository_creation_type": "none",
41+
"members_can_create_public_repositories": false,
42+
"members_can_create_private_repositories": false,
43+
"members_can_create_internal_repositories": false,
44+
"members_can_create_pages": true,
45+
"members_can_fork_private_repositories": false,
46+
"members_can_create_public_pages": true,
47+
"members_can_create_private_pages": true,
48+
"plan": {
49+
"name": "free",
50+
"space": 976562499,
51+
"private_repos": 10000,
52+
"filled_seats": 38,
53+
"seats": 3
54+
}
55+
}

src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/userIsCollaborator/__files/repos_hub4j-test-org_github-api-3.json

Lines changed: 365 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)