ForkableRepoValidator { protected ShouldForkResult contentHasChangesInDefaultBranch(GHRepository parentRepo, GHContent searchResultContent, GitForkBranch gitForkBranch) { try { String searchContentPath = searchResultContent.getPath(); GHContent content = dockerfileGitHubUtil.tryRetrievingContent(parentRepo, searchContentPath, parentRepo.getDefaultBranch()); if (content == null) { return shouldNotForkResult( String.format(CONTENT_PATH_NOT_IN_DEFAULT_BRANCH_TEMPLATE, searchContentPath)); } else { if (hasNoChanges(content, gitForkBranch)) { return shouldNotForkResult( String.format(COULD_NOT_FIND_IMAGE_TO_UPDATE_TEMPLATE, gitForkBranch.getImageName(), searchContentPath)); } } } catch (InterruptedException e) { log.warn("Couldn't get parent content to check for some reason for {}. Trying to proceed... exception: {}", parentRepo.getFullName(), e.getMessage()); } return shouldForkResult(); } ForkableRepoValidator(DockerfileGitHubUtil dockerfileGitHubUtil); ShouldForkResult shouldFork(GHRepository parentRepo,
                                       GHContent searchResultContent,
                                       GitForkBranch gitForkBranch); static final String REPO_IS_FORK; static final String REPO_IS_ARCHIVED; static final String REPO_IS_OWNED_BY_THIS_USER; static final String COULD_NOT_CHECK_THIS_USER; static final String CONTENT_PATH_NOT_IN_DEFAULT_BRANCH_TEMPLATE; static final String COULD_NOT_FIND_IMAGE_TO_UPDATE_TEMPLATE; }
@Test public void testContentHasChangesInDefaultBranch() throws InterruptedException, IOException { DockerfileGitHubUtil dockerfileGitHubUtil = mock(DockerfileGitHubUtil.class); GHRepository repo = mock(GHRepository.class); ForkableRepoValidator validator = new ForkableRepoValidator(dockerfileGitHubUtil); GHContent content = mock(GHContent.class); when(content.getPath()).thenReturn("/Dockerfile"); GitForkBranch gitForkBranch = new GitForkBranch("someImage", "someTag", null); when(dockerfileGitHubUtil.tryRetrievingContent(eq(repo), any(), any())).thenReturn(content); InputStream inputStream = new ByteArrayInputStream("FROM someImage".getBytes()); when(content.read()).thenReturn(inputStream); assertEquals(validator.contentHasChangesInDefaultBranch(repo, content, gitForkBranch), shouldForkResult()); }
@Test public void testInterruptedExceptionWhenRetrievingContentInDefaultBranch() throws InterruptedException { DockerfileGitHubUtil dockerfileGitHubUtil = mock(DockerfileGitHubUtil.class); GHRepository repo = mock(GHRepository.class); ForkableRepoValidator validator = new ForkableRepoValidator(dockerfileGitHubUtil); GHContent content = mock(GHContent.class); when(content.getPath()).thenReturn("/Dockerfile"); GitForkBranch gitForkBranch = new GitForkBranch("someImage", "someTag", null); when(dockerfileGitHubUtil.tryRetrievingContent(eq(repo), any(), any())).thenThrow(new InterruptedException("some exception")); assertEquals(validator.contentHasChangesInDefaultBranch(repo, content, gitForkBranch), shouldForkResult()); }
@Test public void testContentHasNoChangesInDefaultBranch() throws InterruptedException, IOException { DockerfileGitHubUtil dockerfileGitHubUtil = mock(DockerfileGitHubUtil.class); GHRepository repo = mock(GHRepository.class); ForkableRepoValidator validator = new ForkableRepoValidator(dockerfileGitHubUtil); GHContent content = mock(GHContent.class); String searchContentPath = "/Dockerfile"; when(content.getPath()).thenReturn(searchContentPath); String imageName = "someImage"; GitForkBranch gitForkBranch = new GitForkBranch(imageName, "someTag", null); when(dockerfileGitHubUtil.tryRetrievingContent(eq(repo), any(), any())).thenReturn(content); InputStream inputStream = new ByteArrayInputStream("nochanges".getBytes()); when(content.read()).thenReturn(inputStream); assertEquals(validator.contentHasChangesInDefaultBranch(repo, content, gitForkBranch), shouldNotForkResult(String.format(COULD_NOT_FIND_IMAGE_TO_UPDATE_TEMPLATE, imageName, searchContentPath))); }
@Test public void testCouldNotFindContentInDefaultBranch() throws InterruptedException { DockerfileGitHubUtil dockerfileGitHubUtil = mock(DockerfileGitHubUtil.class); GHRepository repo = mock(GHRepository.class); ForkableRepoValidator validator = new ForkableRepoValidator(dockerfileGitHubUtil); GHContent content = mock(GHContent.class); String searchContentPath = "/Dockerfile"; when(content.getPath()).thenReturn(searchContentPath); GitForkBranch gitForkBranch = new GitForkBranch("someImage", "someTag", null); when(dockerfileGitHubUtil.tryRetrievingContent(eq(repo), any(), any())).thenReturn(null); InputStream inputStream = new ByteArrayInputStream("FROM someImage".getBytes()); assertEquals(validator.contentHasChangesInDefaultBranch(repo, content, gitForkBranch), shouldNotForkResult(String.format(CONTENT_PATH_NOT_IN_DEFAULT_BRANCH_TEMPLATE, searchContentPath))); }