Created
April 20, 2020 20:30
-
-
Save gamerson/12ff1cc6b7c7a0db090bcb47d46eed4e to your computer and use it in GitHub Desktop.
GroupBy example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void _processRepositoryInvitations() throws IOException { | |
| RepositoryInvitationService repositoryInvitationService = new RepositoryInvitationService(_gitHubClient); | |
| List<RepositoryInvitation> repositoryInvitations = repositoryInvitationService.getUserRepositoryInvitations(); | |
| Map<String, List<RepositoryInvitation>> invitesByType = repositoryInvitations.stream( | |
| ).collect( | |
| Collectors.groupingBy( | |
| repoInvite -> { | |
| InviteUser inviter = repoInvite.getInviter(); | |
| UserService userService = new UserService(_gitHubClient); | |
| try { | |
| User user = userService.getUser(inviter.getLogin()); | |
| OrganizationService organizationService = new OrganizationService(_gitHubClient); | |
| List<User> organizations = organizationService.getOrganizations(user.getLogin()); | |
| boolean isLiferayOrg = organizations.stream( | |
| ).map( | |
| User::getLogin | |
| ).filter( | |
| login -> login.equals("liferay") | |
| ).findAny( | |
| ).isPresent(); | |
| if (isLiferayOrg) { | |
| return "accept"; | |
| } | |
| else { | |
| return "decline"; | |
| } | |
| } | |
| catch (IOException e) { | |
| _logError(e, "Unable to filter repository invitation {}", repoInvite.getHtmlUrl()); | |
| } | |
| return "NOP"; | |
| } | |
| ) | |
| ); | |
| invitesByType.get("accept").stream().forEach(repoInvite -> { | |
| try { | |
| repositoryInvitationService.acceptRepositoryInvitation(repoInvite.getId()); | |
| _logger.info("Accepted invitation to collaborate on repository: {}", repoInvite.getRepository()); | |
| } | |
| catch (IOException e) { | |
| _logError(e, "Unable to accept invitation to collaborate on repository: {}", repoInvite.getRepository()); | |
| } | |
| }); | |
| invitesByType.get("decline").stream().forEach(repoInvite -> { | |
| try { | |
| repositoryInvitationService.declineRepositoryInvitation(repoInvite.getId()); | |
| _logger.info("Declined invitation to collaborate on repository: {}", repoInvite.getRepository()); | |
| } | |
| catch (IOException e) { | |
| _logError(e, "Unable to decline invitation to collaborate on repository: {}", repoInvite.getRepository()); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment