|
20 | 20 | import java.io.UnsupportedEncodingException; |
21 | 21 | import java.util.List; |
22 | 22 | import java.util.Map; |
| 23 | +import java.util.NoSuchElementException; |
23 | 24 | import javax.xml.bind.DatatypeConverter; |
24 | 25 | import org.eclipse.che.api.core.rest.HttpJsonRequestFactory; |
25 | 26 | import org.eclipse.che.api.core.rest.HttpJsonResponse; |
@@ -203,4 +204,39 @@ private String createBasicAuthHeader(String username, String password) |
203 | 204 | String base64 = DatatypeConverter.printBase64Binary(nameAndPass); |
204 | 205 | return "Basic " + base64; |
205 | 206 | } |
| 207 | + |
| 208 | + public String getUserPublicPrimaryEmail(String username, String password) throws Exception { |
| 209 | + String url = "https://api.github.com/user/public_emails"; |
| 210 | + HttpJsonResponse response = |
| 211 | + requestFactory |
| 212 | + .fromUrl(url) |
| 213 | + .useGetMethod() |
| 214 | + .setAuthorizationHeader(createBasicAuthHeader(username, password)) |
| 215 | + .request(); |
| 216 | + |
| 217 | + @SuppressWarnings("unchecked") |
| 218 | + List<Map<String, String>> properties = |
| 219 | + response.as(List.class, new TypeToken<List<Map<String, String>>>() {}.getType()); |
| 220 | + |
| 221 | + if (properties.isEmpty()) { |
| 222 | + throw new NoSuchElementException("The list with github emails is empty"); |
| 223 | + } |
| 224 | + |
| 225 | + return filterPropertiesAndGetGithubPrimaryEmail(properties); |
| 226 | + } |
| 227 | + |
| 228 | + private String filterPropertiesAndGetGithubPrimaryEmail(List<Map<String, String>> properties) { |
| 229 | + List<Map<String, String>> primaryPublicGithubEmails = |
| 230 | + properties |
| 231 | + .stream() |
| 232 | + .filter( |
| 233 | + map -> map.get("primary").equals("true") && map.get("visibility").equals("public")) |
| 234 | + .collect(toList()); |
| 235 | + |
| 236 | + if (primaryPublicGithubEmails.isEmpty()) { |
| 237 | + throw new NoSuchElementException("The list with github primary, public emails is empty"); |
| 238 | + } |
| 239 | + |
| 240 | + return primaryPublicGithubEmails.get(0).get("email"); |
| 241 | + } |
206 | 242 | } |
0 commit comments