Skip to content

Commit 49f5cda

Browse files
Add method 'getUserPublicEmail' to TestGitHubServiceClient (eclipse-che#6866)
1 parent 1e87b85 commit 49f5cda

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

selenium/che-selenium-core/src/main/java/org/eclipse/che/selenium/core/client/TestGitHubServiceClient.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.UnsupportedEncodingException;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.NoSuchElementException;
2324
import javax.xml.bind.DatatypeConverter;
2425
import org.eclipse.che.api.core.rest.HttpJsonRequestFactory;
2526
import org.eclipse.che.api.core.rest.HttpJsonResponse;
@@ -203,4 +204,39 @@ private String createBasicAuthHeader(String username, String password)
203204
String base64 = DatatypeConverter.printBase64Binary(nameAndPass);
204205
return "Basic " + base64;
205206
}
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+
}
206242
}

0 commit comments

Comments
 (0)