TakeSnapshotTask implements Callable<Snapshot> { Statistics collectStatistics(final OrganizationWrapper organization) throws IOException { logger.info("Started collecting statistics for organization '{}'.", organisationName); Statistics statistics = new Statistics(organization.getId(), snapshotDate); statistics.setPublicProjectCount(organization.listRepositories().size()); statistics.setMembersCount(organization.listMembers().size()); statistics.setTeamsCount(organization.listTeams().size()); statistics.setAllContributorsCount((int) organization.listRepositories().stream() .map(RepositoryWrapper::listContributors) .flatMap(List::stream) .map(GHRepository.Contributor::getId) .distinct() .count()); statistics.setExternalContributorsCount((int) organization.listRepositories().stream() .map(RepositoryWrapper::listContributors) .flatMap(List::stream) .filter(contributor -> !organization.contributorIsMember(contributor)) .map(GHRepository.Contributor::getId) .distinct() .count()); statistics.setAllStarsCount(organization.listRepositories().stream() .map(RepositoryWrapper::getStarsCount) .reduce(0, Integer::sum)); statistics.setAllForksCount(organization.listRepositories().stream() .map(RepositoryWrapper::getForksCount) .reduce(0, Integer::sum)); statistics.setAllSizeCount(organization.listRepositories().stream() .map(RepositoryWrapper::getSize) .reduce(0, Integer::sum)); statistics.setProgramLanguagesCount((int) organization.listRepositories().stream() .map(RepositoryWrapper::getPrimaryLanguage) .distinct() .count()); statistics.setTagsCount((int) organization.listRepositories().stream() .map(RepositoryWrapper::listTags) .flatMap(List::stream) .count()); statistics.setOrganizationName(organization.getLogin()); logger.info("Finished collecting statistics for organization '{}'.", organisationName); return statistics; } TakeSnapshotTask(final GitHub gitHub, final String organisationName, Scorer scorer, Date snapshotDate); @Override Snapshot call();  }
@Test public void testCollectStatistics() throws Exception { OrganizationWrapper org = mock(OrganizationWrapper.class); when(org.listMembers()).thenReturn(mockList(GHUser.class, 5)); when(org.listTeams()).thenReturn(mockList(GHTeam.class, 4)); when(org.listRepositories()).thenReturn(emptyList()); when(org.getLogin()).thenReturn("myLogin"); Statistics statistics = task.collectStatistics(org); assertThat(statistics.getPublicProjectCount(), equalTo(0)); assertThat(statistics.getMembersCount(), equalTo(5)); assertThat(statistics.getTeamsCount(), equalTo(4)); assertThat(statistics.getOrganizationName(), equalTo("myLogin")); }
@Test public void testCountExternalContributors() throws Exception { GHRepository.Contributor internal1 = new TestGHContributor("internal1", 1); GHRepository.Contributor internal2 = new TestGHContributor("internal2", 2); List<GHUser> members = Lists.newArrayList(internal1, internal2); List<GHRepository.Contributor> contributors = Lists.newArrayList(internal1, internal2, new TestGHContributor("external", 3)); RepositoryWrapper repo = mock(RepositoryWrapper.class); when(repo.listContributors()).thenReturn(contributors); OrganizationWrapper org = mock(OrganizationWrapper.class); when(org.listMembers()).thenReturn(members); when(org.listTeams()).thenReturn(mockList(GHTeam.class, 4)); when(org.listRepositories()).thenReturn(Lists.newArrayList(repo)); when(org.contributorIsMember(any(Contributor.class))).thenCallRealMethod(); Statistics statistics = task.collectStatistics(org); assertThat(statistics.getExternalContributorsCount(), equalTo(1)); assertThat(statistics.getAllContributorsCount(), equalTo(3)); }