GitHubPullRequestSender { public Multimap<String, GitHubContentToProcess> forkRepositoriesFoundAndGetPathToDockerfiles( PagedSearchIterable<GHContent> contentsWithImage, GitForkBranch gitForkBranch) { log.info("Forking repositories..."); Multimap<String, GitHubContentToProcess> pathToDockerfilesInParentRepo = HashMultimap.create(); GHRepository parent; String parentRepoName; int totalContentsFound = 0; int contentsShouldFork = 0; for (GHContent ghContent : contentsWithImage) { totalContentsFound++; parent = ghContent.getOwner(); parentRepoName = parent.getFullName(); try { parent = dockerfileGitHubUtil.getRepo(parentRepoName); ShouldForkResult shouldForkResult = forkableRepoValidator.shouldFork(parent, ghContent, gitForkBranch); if (shouldForkResult.isForkable()) { contentsShouldFork++; ensureForkedAndAddToListForProcessing(pathToDockerfilesInParentRepo, parent, parentRepoName, ghContent); } else { log.warn("Skipping {} because {}", parentRepoName, shouldForkResult.getReason()); } } catch (IOException exception) { log.warn("Could not refresh details of {}", parentRepoName); } } log.info("Out of {} content search results processed, {} were deemed eligible for forking to yield {} repositories to fork.", totalContentsFound, contentsShouldFork, pathToDockerfilesInParentRepo.keys().size()); log.info("Path to Dockerfiles in repos: {}", pathToDockerfilesInParentRepo); return pathToDockerfilesInParentRepo; } GitHubPullRequestSender(DockerfileGitHubUtil dockerfileGitHubUtil, ForkableRepoValidator forkableRepoValidator); Multimap<String, GitHubContentToProcess> forkRepositoriesFoundAndGetPathToDockerfiles(
            PagedSearchIterable<GHContent> contentsWithImage,
            GitForkBranch gitForkBranch); static final String REPO_IS_FORK; }
@Test public void testForkRepositoriesFound() throws Exception { DockerfileGitHubUtil dockerfileGitHubUtil = mock(DockerfileGitHubUtil.class); GHRepository contentRepo1 = mock(GHRepository.class); when(contentRepo1.getFullName()).thenReturn("1"); GHRepository contentRepo2 = mock(GHRepository.class); when(contentRepo2.getFullName()).thenReturn("2"); GHRepository contentRepo3 = mock(GHRepository.class); when(contentRepo3.getFullName()).thenReturn("3"); GHContent content1 = mock(GHContent.class); when(content1.getOwner()).thenReturn(contentRepo1); GHContent content2 = mock(GHContent.class); when(content2.getOwner()).thenReturn(contentRepo2); GHContent content3 = mock(GHContent.class); when(content3.getOwner()).thenReturn(contentRepo3); PagedSearchIterable<GHContent> contentsWithImage = mock(PagedSearchIterable.class); PagedIterator<GHContent> contentsWithImageIterator = mock(PagedIterator.class); when(contentsWithImageIterator.hasNext()).thenReturn(true, true, true, false); when(contentsWithImageIterator.next()).thenReturn(content1, content2, content3, null); when(contentsWithImage.iterator()).thenReturn(contentsWithImageIterator); when(dockerfileGitHubUtil.getOrCreateFork(Mockito.any())).thenReturn(new GHRepository()); when(dockerfileGitHubUtil.getRepo(any())).thenReturn(mock(GHRepository.class)); ForkableRepoValidator forkableRepoValidator = mock(ForkableRepoValidator.class); when(forkableRepoValidator.shouldFork(any(), any(), any())).thenReturn(ShouldForkResult.shouldForkResult()); GitHubPullRequestSender pullRequestSender = new GitHubPullRequestSender(dockerfileGitHubUtil, forkableRepoValidator); Multimap<String, GitHubContentToProcess> repoMap = pullRequestSender.forkRepositoriesFoundAndGetPathToDockerfiles(contentsWithImage, null); verify(dockerfileGitHubUtil, times(3)).getOrCreateFork(any()); assertEquals(repoMap.size(), 3); }
@Test public void forkRepositoriesFoundAndGetPathToDockerfiles_unableToforkRepo() throws Exception { DockerfileGitHubUtil dockerfileGitHubUtil = mock(DockerfileGitHubUtil.class); GHRepository contentRepo1 = mock(GHRepository.class); when(contentRepo1.getFullName()).thenReturn("1"); GHRepository duplicateContentRepo1 = mock(GHRepository.class); when(duplicateContentRepo1.getFullName()).thenReturn("1"); GHRepository contentRepo2 = mock(GHRepository.class); when(contentRepo2.getFullName()).thenReturn("2"); GHContent content1 = mock(GHContent.class); when(content1.getOwner()).thenReturn(contentRepo1); when(content1.getPath()).thenReturn("1"); GHContent content2 = mock(GHContent.class); when(content2.getOwner()).thenReturn(duplicateContentRepo1); when(content2.getPath()).thenReturn("2"); GHContent content3 = mock(GHContent.class); when(content3.getOwner()).thenReturn(contentRepo2); when(content3.getPath()).thenReturn("3"); PagedSearchIterable<GHContent> contentsWithImage = mock(PagedSearchIterable.class); PagedIterator<GHContent> contentsWithImageIterator = mock(PagedIterator.class); when(contentsWithImageIterator.hasNext()).thenReturn(true, true, true, false); when(contentsWithImageIterator.next()).thenReturn(content1, content2, content3, null); when(contentsWithImage.iterator()).thenReturn(contentsWithImageIterator); when(dockerfileGitHubUtil.getOrCreateFork(contentRepo1)).thenReturn(null); when(dockerfileGitHubUtil.getOrCreateFork(duplicateContentRepo1)).thenReturn(null); when(dockerfileGitHubUtil.getOrCreateFork(contentRepo2)).thenReturn(mock(GHRepository.class)); when(dockerfileGitHubUtil.getRepo(contentRepo1.getFullName())).thenReturn(contentRepo1); when(dockerfileGitHubUtil.getRepo(duplicateContentRepo1.getFullName())).thenReturn(duplicateContentRepo1); when(dockerfileGitHubUtil.getRepo(contentRepo2.getFullName())).thenReturn(contentRepo2); ForkableRepoValidator forkableRepoValidator = mock(ForkableRepoValidator.class); when(forkableRepoValidator.shouldFork(any(), any(), any())).thenReturn(ShouldForkResult.shouldForkResult()); GitHubPullRequestSender pullRequestSender = new GitHubPullRequestSender(dockerfileGitHubUtil, forkableRepoValidator); Multimap<String, GitHubContentToProcess> repoMap = pullRequestSender.forkRepositoriesFoundAndGetPathToDockerfiles(contentsWithImage, null); verify(dockerfileGitHubUtil, times(3)).getOrCreateFork(any()); assertEquals(repoMap.size(), 1); }
@Test public void testDoNotForkReposWhichDoNotQualify() throws Exception { DockerfileGitHubUtil dockerfileGitHubUtil = mock(DockerfileGitHubUtil.class); GHRepository contentRepo1 = mock(GHRepository.class); when(contentRepo1.getFullName()).thenReturn("1"); GHContent content1 = mock(GHContent.class); when(content1.getOwner()).thenReturn(contentRepo1); when(contentRepo1.isFork()).thenReturn(true); PagedSearchIterable<GHContent> contentsWithImage = mock(PagedSearchIterable.class); PagedIterator<GHContent> contentsWithImageIterator = mock(PagedIterator.class); when(contentsWithImageIterator.hasNext()).thenReturn(true, false); when(contentsWithImageIterator.next()).thenReturn(content1, null); when(contentsWithImage.iterator()).thenReturn(contentsWithImageIterator); when(dockerfileGitHubUtil.getRepo(any())).thenReturn(contentRepo1); ForkableRepoValidator forkableRepoValidator = mock(ForkableRepoValidator.class); when(forkableRepoValidator.shouldFork(any(), any(), any())).thenReturn(ShouldForkResult.shouldNotForkResult("")); GitHubPullRequestSender pullRequestSender = new GitHubPullRequestSender(dockerfileGitHubUtil, forkableRepoValidator); Multimap<String, GitHubContentToProcess> repoMap = pullRequestSender.forkRepositoriesFoundAndGetPathToDockerfiles(contentsWithImage, null); verify(dockerfileGitHubUtil, never()).getOrCreateFork(any()); assertEquals(repoMap.size(), 0); }