diff --git a/src/main/java/jp/openstandia/connector/github/GitHubClient.java b/src/main/java/jp/openstandia/connector/github/GitHubClient.java index eb61a13..ed1f363 100644 --- a/src/main/java/jp/openstandia/connector/github/GitHubClient.java +++ b/src/main/java/jp/openstandia/connector/github/GitHubClient.java @@ -24,6 +24,7 @@ import org.kohsuke.github.SCIMUser; import java.util.Collection; +import java.util.List; import java.util.Set; /** @@ -50,6 +51,8 @@ public interface GitHubClient { void getUser(GitHubSchema schema, Name name, ResultsHandler handler, OperationOptions options, Set attributesToGet, boolean allowPartialAttributeValues, int queryPageSize); + List getTeamIdsByUsername(String userLogin, int pageSize); + boolean isOrganizationMember(String userLogin); void assignOrganizationRole(String userLogin, String organizationRole); diff --git a/src/main/java/jp/openstandia/connector/github/GitHubUserHandler.java b/src/main/java/jp/openstandia/connector/github/GitHubUserHandler.java index 73da3f6..b167f53 100644 --- a/src/main/java/jp/openstandia/connector/github/GitHubUserHandler.java +++ b/src/main/java/jp/openstandia/connector/github/GitHubUserHandler.java @@ -22,6 +22,7 @@ import org.kohsuke.github.SCIMUser; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -305,6 +306,16 @@ private String resolveUserLogin(Uid oldUid, String newNameValue) { @Override public void delete(Uid uid, OperationOptions options) { + String userLogin = getUserLogin(uid); + if (!userLogin.equals(UNKNOWN_USER_NAME)) { + // Fix https://github.com/openstandia/connector-github/issues/6 + // GitHub maintains the user's team association after deletion + // So, we need to remove the association first + List teamIds = client.getTeamIdsByUsername(userLogin, configuration.getQueryPageSize()); + client.unassignTeams(userLogin, teamIds); + } + + // Finally, do delete the user client.deleteUser(schema, uid, options); } diff --git a/src/main/java/jp/openstandia/connector/github/rest/GitHubRESTClient.java b/src/main/java/jp/openstandia/connector/github/rest/GitHubRESTClient.java index 5152377..f6698a9 100644 --- a/src/main/java/jp/openstandia/connector/github/rest/GitHubRESTClient.java +++ b/src/main/java/jp/openstandia/connector/github/rest/GitHubRESTClient.java @@ -334,6 +334,17 @@ public void getUser(GitHubSchema schema, Name name, ResultsHandler handler, Oper }); } + @Override + public List getTeamIdsByUsername(String userLogin, int pageSize) { + return withAuth(() -> { + return orgApiClient.listTeams(userLogin, pageSize) + .toList().stream() + .filter(t -> t.node.members.totalCount == 1) + .map(GitHubUtils::toTeamUid) + .collect(Collectors.toList()); + }); + } + private ConnectorObject toConnectorObject(GitHubSchema schema, String queryLogin, SCIMUser user, Set attributesToGet, boolean allowPartialAttributeValues, int queryPageSize) { diff --git a/src/test/java/jp/openstandia/connector/github/testutil/MockClient.java b/src/test/java/jp/openstandia/connector/github/testutil/MockClient.java index 06fd2bc..3a09dd8 100644 --- a/src/test/java/jp/openstandia/connector/github/testutil/MockClient.java +++ b/src/test/java/jp/openstandia/connector/github/testutil/MockClient.java @@ -11,6 +11,7 @@ import org.kohsuke.github.SCIMUser; import java.util.Collection; +import java.util.List; import java.util.Set; public class MockClient implements GitHubClient { @@ -67,6 +68,11 @@ public void getUser(GitHubSchema schema, Name name, ResultsHandler handler, Oper } + @Override + public List getTeamIdsByUsername(String userLogin, int pageSize) { + return null; + } + @Override public boolean isOrganizationMember(String userLogin) { return false;