Skip to content

Commit

Permalink
Merge pull request #1438 from gsmet/fix-team-remove
Browse files Browse the repository at this point in the history
Fix GHTeam#remove(GHUser) and add tests
  • Loading branch information
bitwiseman authored Apr 21, 2022
2 parents 474da8a + d870d21 commit fab5137
Show file tree
Hide file tree
Showing 20 changed files with 875 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/main/java/org/kohsuke/github/GHTeam.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import javax.annotation.Nonnull;

import static org.kohsuke.github.GitHubRequest.transformEnum;

/**
* A team in GitHub organization.
*
Expand Down Expand Up @@ -151,6 +153,19 @@ public PagedIterable<GHUser> listMembers(String role) throws IOException {
return root().createRequest().withUrlPath(api("/members")).with("role", role).toIterable(GHUser[].class, null);
}

/**
* List members with specified role paged iterable.
*
* @param role
* the role
* @return the paged iterable
* @throws IOException
* the io exception
*/
public PagedIterable<GHUser> listMembers(Role role) throws IOException {
return listMembers(transformEnum(role));
}

/**
* Gets a single discussion by ID.
*
Expand Down Expand Up @@ -288,7 +303,7 @@ public void add(GHUser user, Role role) throws IOException {
* the io exception
*/
public void remove(GHUser u) throws IOException {
root().createRequest().method("DELETE").withUrlPath(api("/members/" + u.getLogin())).send();
root().createRequest().method("DELETE").withUrlPath(api("/memberships/" + u.getLogin())).send();
}

/**
Expand Down
70 changes: 69 additions & 1 deletion src/test/java/org/kohsuke/github/GHTeamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import org.junit.Test;
import org.kohsuke.github.GHTeam.Privacy;
import org.kohsuke.github.GHTeam.Role;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

public class GHTeamTest extends AbstractGitHubWireMockTest {

Expand Down Expand Up @@ -137,4 +143,66 @@ public void testFetchEmptyChildTeams() throws IOException {
assertThat(result, is(empty()));
}

@Test
public void addRemoveMember() throws IOException {
String teamSlug = "dummy-team";

GHTeam team = gitHub.getOrganization(GITHUB_API_TEST_ORG).getTeamBySlug(teamSlug);

List<GHUser> members = team.listMembers().toList();

assertThat(members, notNullValue());
assertThat("One admin in dummy team", members.size(), equalTo(1));
assertThat("Specific user in admin team",
members.stream().anyMatch(ghUser -> ghUser.getLogin().equals("bitwiseman")));

GHUser user = gitHub.getUser("gsmet");

try {
team.add(user, Role.MAINTAINER);

// test all
members = team.listMembers().toList();

assertThat(members, notNullValue());
assertThat("Two members for all roles in dummy team", members.size(), equalTo(2));
assertThat("Specific users in team",
members,
containsInAnyOrder(hasProperty("login", equalTo("bitwiseman")),
hasProperty("login", equalTo("gsmet"))));

// test maintainer role filter
members = team.listMembers(Role.MAINTAINER).toList();

assertThat(members, notNullValue());
assertThat("Two members for all roles in dummy team", members.size(), equalTo(2));
assertThat("Specific users in team",
members,
containsInAnyOrder(hasProperty("login", equalTo("bitwiseman")),
hasProperty("login", equalTo("gsmet"))));

// test member role filter
// it's hard to test this as owner of the org are automatically made maintainer
// so let's just test that we don't have any members around
members = team.listMembers(Role.MEMBER).toList();

assertThat(members, notNullValue());
assertThat("No members in dummy team", members.size(), equalTo(0));

// test removing the user has effect
team.remove(user);

members = team.listMembers().toList();

assertThat(members, notNullValue());
assertThat("One member for all roles in dummy team", members.size(), equalTo(1));
assertThat("Specific user in team",
members,
containsInAnyOrder(hasProperty("login", equalTo("bitwiseman"))));
} finally {
if (team.hasMember(user)) {
team.remove(user);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"login": "bitwiseman",
"id": 1958953,
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
"avatar_url": "https://avatars.githubusercontent.com/u/1958953?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bitwiseman",
"html_url": "https://github.com/bitwiseman",
"followers_url": "https://api.github.com/users/bitwiseman/followers",
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
"repos_url": "https://api.github.com/users/bitwiseman/repos",
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
"type": "User",
"site_admin": false
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"login": "bitwiseman",
"id": 1958953,
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
"avatar_url": "https://avatars.githubusercontent.com/u/1958953?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bitwiseman",
"html_url": "https://github.com/bitwiseman",
"followers_url": "https://api.github.com/users/bitwiseman/followers",
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
"repos_url": "https://api.github.com/users/bitwiseman/repos",
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
"type": "User",
"site_admin": false
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"login": "gsmet",
"id": 1279749,
"node_id": "MDQ6VXNlcjEyNzk3NDk=",
"avatar_url": "https://avatars.githubusercontent.com/u/1279749?u=e462a6165ea17647aed446ca31fae604338ae18c&v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/gsmet",
"html_url": "https://github.com/gsmet",
"followers_url": "https://api.github.com/users/gsmet/followers",
"following_url": "https://api.github.com/users/gsmet/following{/other_user}",
"gists_url": "https://api.github.com/users/gsmet/gists{/gist_id}",
"starred_url": "https://api.github.com/users/gsmet/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/gsmet/subscriptions",
"organizations_url": "https://api.github.com/users/gsmet/orgs",
"repos_url": "https://api.github.com/users/gsmet/repos",
"events_url": "https://api.github.com/users/gsmet/events{/privacy}",
"received_events_url": "https://api.github.com/users/gsmet/received_events",
"type": "User",
"site_admin": false
},
{
"login": "bitwiseman",
"id": 1958953,
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
"avatar_url": "https://avatars.githubusercontent.com/u/1958953?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bitwiseman",
"html_url": "https://github.com/bitwiseman",
"followers_url": "https://api.github.com/users/bitwiseman/followers",
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
"repos_url": "https://api.github.com/users/bitwiseman/repos",
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
"type": "User",
"site_admin": false
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"login": "gsmet",
"id": 1279749,
"node_id": "MDQ6VXNlcjEyNzk3NDk=",
"avatar_url": "https://avatars.githubusercontent.com/u/1279749?u=e462a6165ea17647aed446ca31fae604338ae18c&v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/gsmet",
"html_url": "https://github.com/gsmet",
"followers_url": "https://api.github.com/users/gsmet/followers",
"following_url": "https://api.github.com/users/gsmet/following{/other_user}",
"gists_url": "https://api.github.com/users/gsmet/gists{/gist_id}",
"starred_url": "https://api.github.com/users/gsmet/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/gsmet/subscriptions",
"organizations_url": "https://api.github.com/users/gsmet/orgs",
"repos_url": "https://api.github.com/users/gsmet/repos",
"events_url": "https://api.github.com/users/gsmet/events{/privacy}",
"received_events_url": "https://api.github.com/users/gsmet/received_events",
"type": "User",
"site_admin": false
},
{
"login": "bitwiseman",
"id": 1958953,
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
"avatar_url": "https://avatars.githubusercontent.com/u/1958953?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bitwiseman",
"html_url": "https://github.com/bitwiseman",
"followers_url": "https://api.github.com/users/bitwiseman/followers",
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
"repos_url": "https://api.github.com/users/bitwiseman/repos",
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
"type": "User",
"site_admin": false
}
]
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"twitter_username": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 49,
"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": 11979,
"collaborators": 0,
"billing_email": "[email protected]",
"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": 37,
"seats": 3
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "dummy-team",
"id": 3451996,
"node_id": "MDQ6VGVhbTM0NTE5OTY=",
"slug": "dummy-team",
"description": "Updated by API TestModified",
"privacy": "closed",
"url": "https://api.github.com/organizations/7544739/team/3451996",
"html_url": "https://github.com/orgs/hub4j-test-org/teams/dummy-team",
"members_url": "https://api.github.com/organizations/7544739/team/3451996/members{/member}",
"repositories_url": "https://api.github.com/organizations/7544739/team/3451996/repos",
"permission": "pull",
"created_at": "2019-10-03T21:46:12Z",
"updated_at": "2022-03-04T10:36:59Z",
"members_count": 1,
"repos_count": 1,
"organization": {
"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": "[email protected]",
"twitter_username": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 49,
"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"
},
"parent": null
}
Loading

0 comments on commit fab5137

Please sign in to comment.