Skip to content

Commit

Permalink
Merge pull request #7 from openstandia/dev
Browse files Browse the repository at this point in the history
fix: unassign team(s) when the user is removed(#6)
  • Loading branch information
wadahiro authored Mar 10, 2021
2 parents fd49786 + eda3691 commit 685596d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.kohsuke.github.SCIMUser;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
Expand All @@ -50,6 +51,8 @@ public interface GitHubClient {

void getUser(GitHubSchema schema, Name name, ResultsHandler handler, OperationOptions options, Set<String> attributesToGet, boolean allowPartialAttributeValues, int queryPageSize);

List<String> getTeamIdsByUsername(String userLogin, int pageSize);

boolean isOrganizationMember(String userLogin);

void assignOrganizationRole(String userLogin, String organizationRole);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> teamIds = client.getTeamIdsByUsername(userLogin, configuration.getQueryPageSize());
client.unassignTeams(userLogin, teamIds);
}

// Finally, do delete the user
client.deleteUser(schema, uid, options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,17 @@ public void getUser(GitHubSchema schema, Name name, ResultsHandler handler, Oper
});
}

@Override
public List<String> 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<String> attributesToGet, boolean allowPartialAttributeValues, int queryPageSize) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -67,6 +68,11 @@ public void getUser(GitHubSchema schema, Name name, ResultsHandler handler, Oper

}

@Override
public List<String> getTeamIdsByUsername(String userLogin, int pageSize) {
return null;
}

@Override
public boolean isOrganizationMember(String userLogin) {
return false;
Expand Down

0 comments on commit 685596d

Please sign in to comment.