From a4b06eb46a951c821ba69fdec48ec4c5196cb718 Mon Sep 17 00:00:00 2001 From: mcmathews Date: Mon, 3 Nov 2014 14:33:39 -0700 Subject: Implemented the GitHub Team Membership API Github recently rolled out the Team Membership API, and subsequently deprecated the Team Members API. Currently, using the Team Members API to add a member to a team who is not already part of the org, will result in an error. The changes made are to implement the new Team Memberships API. Bug: 447419 Change-Id: I6773e31113688ac027cf8b061107f54a35ab10ff Signed-off-by: Michael Mathews --- .../egit/github/core/tests/TeamServiceTest.java | 55 ++++++++++++++++++- .../eclipse/egit/github/core/TeamMembership.java | 61 ++++++++++++++++++++++ .../egit/github/core/client/IGitHubConstants.java | 2 + .../egit/github/core/service/TeamService.java | 49 ++++++++++++++++- 4 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/TeamMembership.java diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/TeamServiceTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/TeamServiceTest.java index 10b7c7f0..737c4742 100644 --- a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/TeamServiceTest.java +++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/TeamServiceTest.java @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2011 GitHub Inc. + * Copyright (c) 2011, 2015 GitHub Inc. and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,6 +7,8 @@ * * Contributors: * Kevin Sawicki (GitHub Inc.) - initial API and implementation + * Michael Mathews (Arizona Board of Regents) - (Bug: 447419) + * Team Membership API implementation *****************************************************************************/ package org.eclipse.egit.github.core.tests; @@ -21,6 +23,7 @@ import java.util.Collections; import org.eclipse.egit.github.core.RepositoryId; import org.eclipse.egit.github.core.Team; +import org.eclipse.egit.github.core.TeamMembership; import org.eclipse.egit.github.core.client.GitHubClient; import org.eclipse.egit.github.core.client.GitHubRequest; import org.eclipse.egit.github.core.client.GitHubResponse; @@ -296,6 +299,56 @@ public class TeamServiceTest { service.removeMember(3, ""); } + @Test(expected = IllegalArgumentException.class) + public void getMembershipNullName() throws IOException { + service.getMembership(6, null); + } + + @Test(expected = IllegalArgumentException.class) + public void getMembershipEmptyName() throws IOException { + service.getMembership(6, ""); + } + + @Test + public void getMembership() throws IOException { + service.getMembership(6, "tt"); + GitHubRequest request = new GitHubRequest(); + request.setUri("/teams/6/memberships/tt"); + verify(client).get(request); + } + + @Test(expected = IllegalArgumentException.class) + public void addMembershipNullName() throws IOException { + service.addMembership(6, null); + } + + @Test(expected = IllegalArgumentException.class) + public void addMembershipEmptyName() throws IOException { + service.addMembership(6, ""); + } + + @Test + public void addMembership() throws IOException { + service.addMembership(6, "tt"); + verify(client).put("/teams/6/memberships/tt", null, TeamMembership.class); + } + + @Test(expected = IllegalArgumentException.class) + public void removeMembershipNullName() throws IOException { + service.removeMembership(6, null); + } + + @Test(expected = IllegalArgumentException.class) + public void removeMembershipEmptyName() throws IOException { + service.removeMembership(6, ""); + } + + @Test + public void removeMembership() throws IOException { + service.removeMembership(6, "tt"); + verify(client).delete("/teams/6/memberships/tt"); + } + /** * Remove member * diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/TeamMembership.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/TeamMembership.java new file mode 100644 index 00000000..cbc3ad8b --- /dev/null +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/TeamMembership.java @@ -0,0 +1,61 @@ +/****************************************************************************** + * Copyright (c) 2014, 2015 Arizona Board of Regents + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Mathews (Arizona Board of Regents) - (Bug: 447419) + * Team Membership API implementation + *****************************************************************************/ +package org.eclipse.egit.github.core; + +import java.io.Serializable; + +/** + * Team Membership model class. + */ +public class TeamMembership implements Serializable { + + private static final long serialVersionUID = -8207728181588115431L; + + /** + * The possible states of a Team Membership + */ + public static enum TeamMembershipState { + ACTIVE, PENDING; + } + + private TeamMembershipState state; + + private String url; + + /** + * @return state + */ + public TeamMembershipState getState() { + return state; + } + + /** + * @param state + */ + public void setState(TeamMembershipState state) { + this.state = state; + } + + /** + * @return url + */ + public String getUrl() { + return url; + } + + /** + * @param url + */ + public void setUrl(String url) { + this.url = url; + } +} diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java index 83dc0206..eb0518b9 100644 --- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java @@ -137,6 +137,8 @@ public interface IGitHubConstants { /** */ String SEGMENT_ORGANIZATIONS = "/organizations"; //$NON-NLS-1$ /** */ + String SEGMENT_MEMBERSHIPS = "/memberships"; //$NON-NLS-1$ + /** */ String SEGMENT_ORGS = "/orgs"; //$NON-NLS-1$ /** */ String SEGMENT_PUBLIC = "/public"; //$NON-NLS-1$ diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/TeamService.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/TeamService.java index f015af19..7b439e58 100644 --- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/TeamService.java +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/TeamService.java @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) 2011 GitHub Inc. + * Copyright (c) 2011, 2014 GitHub Inc. and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,10 +7,13 @@ * * Contributors: * Kevin Sawicki (GitHub Inc.) - initial API and implementation + * Michael Mathews (Arizona Board of Regents) - (Bug: 447419) + * Team Membership API implementation *****************************************************************************/ package org.eclipse.egit.github.core.service; import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_MEMBERS; +import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_MEMBERSHIPS; import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_ORGS; import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_REPOS; import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_TEAMS; @@ -25,6 +28,7 @@ import java.util.Map; import org.eclipse.egit.github.core.IRepositoryIdProvider; import org.eclipse.egit.github.core.Repository; import org.eclipse.egit.github.core.Team; +import org.eclipse.egit.github.core.TeamMembership; import org.eclipse.egit.github.core.User; import org.eclipse.egit.github.core.client.GitHubClient; import org.eclipse.egit.github.core.client.GitHubRequest; @@ -242,6 +246,49 @@ public class TeamService extends GitHubService { client.delete(uri.toString()); } + public TeamMembership getMembership(int id, String user) throws IOException { + if (user == null) + throw new IllegalArgumentException("User cannot be null"); //$NON-NLS-1$ + if (user.length() == 0) + throw new IllegalArgumentException("User cannot be empty"); //$NON-NLS-1$ + + StringBuilder uri = new StringBuilder(SEGMENT_TEAMS); + uri.append('/').append(id); + uri.append(SEGMENT_MEMBERSHIPS); + uri.append('/').append(user); + + GitHubRequest request = createRequest(); + request.setUri(uri); + request.setType(TeamMembership.class); + return (TeamMembership) client.get(request).getBody(); + } + + public TeamMembership addMembership(int id, String user) throws IOException { + if (user == null) + throw new IllegalArgumentException("User cannot be null"); //$NON-NLS-1$ + if (user.length() == 0) + throw new IllegalArgumentException("User cannot be empty"); //$NON-NLS-1$ + + StringBuilder uri = new StringBuilder(SEGMENT_TEAMS); + uri.append('/').append(id); + uri.append(SEGMENT_MEMBERSHIPS); + uri.append('/').append(user); + return client.put(uri.toString(), null, TeamMembership.class); + } + + public void removeMembership(int id, String user) throws IOException { + if (user == null) + throw new IllegalArgumentException("User cannot be null"); //$NON-NLS-1$ + if (user.length() == 0) + throw new IllegalArgumentException("User cannot be empty"); //$NON-NLS-1$ + + StringBuilder uri = new StringBuilder(SEGMENT_TEAMS); + uri.append('/').append(id); + uri.append(SEGMENT_MEMBERSHIPS); + uri.append('/').append(user); + client.delete(uri.toString()); + } + /** * Get all repositories for given team * -- cgit v1.2.3