Skip to content

Commit

Permalink
Support for github search of user by username exact match or partial.…
Browse files Browse the repository at this point in the history
… As well as by email. when using name_like
  • Loading branch information
James Harris committed Jul 31, 2015
1 parent 1692e60 commit f841510
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,61 @@ public List<Identity> searchIdentities(String name, String scope, boolean exactM
if (!isConfigured()){
return new ArrayList<>();
}
switch (scope){
case GithubConstants.USER_SCOPE:
return searchUsers(name, exactMatch);
case GithubConstants.ORG_SCOPE:
return searchGroups(name, exactMatch);
case GithubConstants.TEAM_SCOPE:
return searchTeams(name, exactMatch);
default:
return new ArrayList<>();
}
}

private List<Identity> searchTeams(String teamName, boolean exactMatch) {
return new ArrayList<>();
}

private List<Identity> searchGroups(String groupName, boolean exactMatch) {
return new ArrayList<>();
}

@SuppressWarnings("unchecked")
private List<Identity> searchUsers(String userName, boolean exactMatch) {
List<Identity> identities = new ArrayList<>();
if (exactMatch) {
GithubAccountInfo user;
try {
user = githubClient.getUserIdByName(userName);
} catch (ClientVisibleException e) {
return identities;
}
Identity identity = user.toIdentity(GithubConstants.USER_SCOPE);
identities.add(identity);
return identities;
}
HttpResponse res;
List<Map<String, Object>> x;
try {
res = Request.Get(githubClient.getURL(GithubClientEndpoints.USER_SEARCH) + name)
res = Request.Get(githubClient.getURL(GithubClientEndpoints.USER_SEARCH) + userName)
.addHeader("Authorization", "token " + githubUtils.getAccessToken()).addHeader
("Accept", "application/json").execute().returnResponse();
("Accept", "application/json").execute().returnResponse();
int statusCode = res.getStatusLine().getStatusCode();
if (statusCode != 200) {
githubClient.noGithub(statusCode);
}
//TODO:Finish implementing search.
Map<String, Object> jsonData = jsonMapper.readValue(res.getEntity().getContent());
jsonData.toString();
x = (List<Map<String, Object>>) jsonData.get("items");
} catch (IOException e) {
//TODO: Propper Error Handling.
return null;
return identities;
}
for (Map<String, Object> user: x){
identities.add(new Identity(GithubConstants.USER_SCOPE, String.valueOf(user.get("id")),
(String) user.get(LOGIN), (String) user.get("html_url"), (String) user.get("avatar_url")));
}
List<Identity> identities = new ArrayList<>();
identities.add(new Identity(scope, res.toString()));
return identities;
}

Expand All @@ -100,12 +137,10 @@ public Identity getIdentity(String id, String scope) {
switch (scope) {
case GithubConstants.USER_SCOPE:
GithubAccountInfo user = getUserOrgById(id);
return new Identity(GithubConstants.USER_SCOPE, user.getAccountId(),
user.getAccountName(), user.getProfileUrl(), user.getProfilePicture());
return user == null ? null : user.toIdentity(GithubConstants.USER_SCOPE);
case GithubConstants.ORG_SCOPE:
GithubAccountInfo org = getUserOrgById(id);
return new Identity(GithubConstants.ORG_SCOPE, org.getAccountId(),
org.getAccountName(), org.getProfileUrl(), org.getProfilePicture());
return org == null ? null : org.toIdentity(GithubConstants.ORG_SCOPE);
case GithubConstants.TEAM_SCOPE:
return getTeamById(id);
default:
Expand Down Expand Up @@ -155,8 +190,7 @@ private GithubAccountInfo getUserOrgById(String id) {
GithubConstants.AUTHORIZATION, "token " + githubAccessToken).execute().returnResponse();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
throw new ClientVisibleException(ResponseCodes.SERVICE_UNAVAILABLE, GithubConstants.GITHUB_ERROR,
"Non-200 Response from Github", "Status code from Github: " + Integer.toString(statusCode));
githubClient.noGithub(statusCode);
}
Map<String, Object> jsonData = CollectionUtils.toMap(jsonMapper.readValue(response.getEntity().getContent(), Map.class));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.cattle.platform.iaas.api.auth.integration.github.resource;

import io.cattle.platform.api.auth.Identity;

public class GithubAccountInfo {
private final String accountId;
private final String accountName;
Expand Down Expand Up @@ -34,4 +36,8 @@ public String getProfilePicture() {
public String getProfileUrl() {
return profileUrl;
}

public Identity toIdentity(String scope) {
return new Identity(scope, accountId, accountName, profileUrl, profilePicture);
}
}

0 comments on commit f841510

Please sign in to comment.