@@ -822,8 +822,6 @@ public void testOnePullRequestForMultipleDockerfilesInSameRepo() throws Exceptio
822822 Multimap <String , GitHubContentToProcess > pathToDockerfilesInParentRepo = HashMultimap .create ();
823823 pathToDockerfilesInParentRepo .put ("repo1" , new GitHubContentToProcess (null , null , "df11" ));
824824 pathToDockerfilesInParentRepo .put ("repo1" , new GitHubContentToProcess (null , null , "df12" ));
825- pathToDockerfilesInParentRepo .put ("repo3" , new GitHubContentToProcess (null , null , "df3" ));
826- pathToDockerfilesInParentRepo .put ("repo4" , new GitHubContentToProcess (null , null , "df4" ));
827825
828826 GHRepository forkedRepo = mock (GHRepository .class );
829827 when (forkedRepo .isFork ()).thenReturn (true );
@@ -833,7 +831,6 @@ public void testOnePullRequestForMultipleDockerfilesInSameRepo() throws Exceptio
833831 when (parentRepo .getFullName ()).thenReturn ("repo1" );
834832 when (forkedRepo .getParent ()).thenReturn (parentRepo );
835833
836- //GHRepository parent = mock(GHRepository.class);
837834 String defaultBranch = "default" ;
838835 when (parentRepo .getDefaultBranch ()).thenReturn (defaultBranch );
839836 GHBranch parentBranch = mock (GHBranch .class );
@@ -843,9 +840,6 @@ public void testOnePullRequestForMultipleDockerfilesInSameRepo() throws Exceptio
843840
844841 gitHubUtil = mock (GitHubUtil .class );
845842 dockerfileGitHubUtil = Mockito .spy (new DockerfileGitHubUtil (gitHubUtil ));
846- //when(dockerfileGitHubUtil.getPullRequestForImageBranch(any(), any())).thenReturn
847- // (Optional.empty());
848- //when(dockerfileGitHubUtil.getRepo(forkedRepo.getFullName())).thenReturn(forkedRepo);
849843 InputStream stream = new ByteArrayInputStream ("someContent" .getBytes (StandardCharsets .UTF_8 ));
850844 GHContent forkedRepoContent1 = mock (GHContent .class );
851845 when (forkedRepoContent1 .read ()).thenReturn (stream );
@@ -857,8 +851,21 @@ public void testOnePullRequestForMultipleDockerfilesInSameRepo() throws Exceptio
857851 when (forkedRepoContent2 .read ()).thenReturn (stream );
858852 when (gitHubUtil .tryRetrievingContent (eq (forkedRepo ),
859853 eq ("df12" ), eq ("image-tag" ))).thenReturn (forkedRepoContent2 );
860- doNothing ().when (dockerfileGitHubUtil ).modifyOnGithub (any (), eq ("image-tag" ), eq ("image" )
861- , eq ("tag" ), anyString (), anyString ());
854+
855+ //Both Dockerfiles modified
856+ doReturn (true ).when (dockerfileGitHubUtil ).modifyContentOnGithub (eq (forkedRepoContent1 ),
857+ eq ("image-tag" ),
858+ eq ("image" ),
859+ eq ("tag" ),
860+ eq (null ),
861+ eq (null ));
862+ doReturn (true ).when (dockerfileGitHubUtil ).modifyContentOnGithub (eq (forkedRepoContent2 ),
863+ eq ("image-tag" ),
864+ eq ("image" ),
865+ eq ("tag" ),
866+ eq (null ),
867+ eq (null ));
868+
862869 doNothing ().when (rateLimiter ).consume ();
863870
864871 dockerfileGitHubUtil .changeDockerfiles (ns , pathToDockerfilesInParentRepo ,
@@ -873,13 +880,153 @@ public void testOnePullRequestForMultipleDockerfilesInSameRepo() throws Exceptio
873880
874881 // Both Dockerfiles modified
875882 verify (dockerfileGitHubUtil , times (2 ))
876- .modifyOnGithub (any (), eq ("image-tag" ), eq ("image" ), eq ("tag" ), any (), any ());
883+ .modifyContentOnGithub (any (), eq ("image-tag" ), eq ("image" ), eq ("tag" ), any (), any ());
877884
878885 // Only one PR created on the repo with changes to both Dockerfiles.
879886 verify (dockerfileGitHubUtil ).createPullReq (eq (parentRepo ),
880887 eq ("image-tag" ), eq (forkedRepo ), any (), eq (rateLimiter ));
881888 }
882889
890+ @ Test
891+ public void testPullRequestBlockNotExecutedWhenDockerfileIsNotModified () throws Exception {
892+ Map <String , Object > nsMap = ImmutableMap .of (Constants .IMG ,
893+ "image" , Constants .TAG ,
894+ "tag" , Constants .STORE ,
895+ "store" );
896+ Namespace ns = new Namespace (nsMap );
897+ GitForkBranch gitForkBranch = new GitForkBranch ("image" , "tag" , null , "" );
898+ Multimap <String , GitHubContentToProcess > pathToDockerfilesInParentRepo = HashMultimap .create ();
899+ pathToDockerfilesInParentRepo .put ("repo" , new GitHubContentToProcess (null , null , "df11" ));
900+
901+ GHRepository forkedRepo = mock (GHRepository .class );
902+ when (forkedRepo .isFork ()).thenReturn (true );
903+ when (forkedRepo .getFullName ()).thenReturn ("forkedrepo" );
904+
905+ GHRepository parentRepo = mock (GHRepository .class );
906+ when (parentRepo .getFullName ()).thenReturn ("repo" );
907+ when (forkedRepo .getParent ()).thenReturn (parentRepo );
908+
909+ String defaultBranch = "default" ;
910+ when (parentRepo .getDefaultBranch ()).thenReturn (defaultBranch );
911+ GHBranch parentBranch = mock (GHBranch .class );
912+ String sha = "abcdef" ;
913+ when (parentBranch .getSHA1 ()).thenReturn (sha );
914+ when (parentRepo .getBranch (defaultBranch )).thenReturn (parentBranch );
915+
916+ gitHubUtil = mock (GitHubUtil .class );
917+ dockerfileGitHubUtil = Mockito .spy (new DockerfileGitHubUtil (gitHubUtil ));
918+ InputStream stream = new ByteArrayInputStream ("someContent" .getBytes (StandardCharsets .UTF_8 ));
919+ GHContent forkedRepoContent = mock (GHContent .class );
920+ when (forkedRepoContent .read ()).thenReturn (stream );
921+
922+ RateLimiter rateLimiter = mock (RateLimiter .class );
923+ when (gitHubUtil .tryRetrievingContent (eq (forkedRepo ),
924+ eq ("df11" ), eq ("image-tag" ))).thenReturn (forkedRepoContent );
925+
926+ //Dockerfile not modified
927+ doReturn (false ).when (dockerfileGitHubUtil ).modifyContentOnGithub (eq (forkedRepoContent ),
928+ eq ("image-tag" ),
929+ eq ("image" ),
930+ eq ("tag" ),
931+ eq (null ),
932+ eq (null ));
933+
934+ doNothing ().when (rateLimiter ).consume ();
935+
936+ dockerfileGitHubUtil .changeDockerfiles (ns , pathToDockerfilesInParentRepo ,
937+ new GitHubContentToProcess (forkedRepo , parentRepo , "" ), new ArrayList <>(),
938+ gitForkBranch , rateLimiter );
939+
940+ // Dockerfile retrieved from the repo
941+ verify (dockerfileGitHubUtil ).tryRetrievingContent (eq (forkedRepo ),
942+ eq ("df11" ), eq ("image-tag" ));
943+
944+ // Trying to modify Dockerfile
945+ verify (dockerfileGitHubUtil ).modifyContentOnGithub (any (), eq ("image-tag" ), eq ("image" ), eq ("tag" ), any (), any ());
946+
947+ // PR creation block not executed
948+ verify (dockerfileGitHubUtil , never ()).createPullReq (eq (parentRepo ),
949+ eq ("image-tag" ), eq (forkedRepo ), any (), eq (rateLimiter ));
950+ }
951+
952+ @ Test
953+ public void testPullRequestCreatedWhenAnyDockerfileIsModified () throws Exception {
954+ Map <String , Object > nsMap = ImmutableMap .of (Constants .IMG ,
955+ "image" , Constants .TAG ,
956+ "tag" , Constants .STORE ,
957+ "store" );
958+ Namespace ns = new Namespace (nsMap );
959+ GitForkBranch gitForkBranch = new GitForkBranch ("image" , "tag" , null , "" );
960+ Multimap <String , GitHubContentToProcess > pathToDockerfilesInParentRepo = HashMultimap .create ();
961+ pathToDockerfilesInParentRepo .put ("repo" , new GitHubContentToProcess (null , null , "df11" ));
962+ pathToDockerfilesInParentRepo .put ("repo" , new GitHubContentToProcess (null , null , "df12" ));
963+
964+ GHRepository forkedRepo = mock (GHRepository .class );
965+ when (forkedRepo .isFork ()).thenReturn (true );
966+ when (forkedRepo .getFullName ()).thenReturn ("forkedrepo" );
967+
968+ GHRepository parentRepo = mock (GHRepository .class );
969+ when (parentRepo .getFullName ()).thenReturn ("repo" );
970+ when (forkedRepo .getParent ()).thenReturn (parentRepo );
971+
972+ String defaultBranch = "default" ;
973+ when (parentRepo .getDefaultBranch ()).thenReturn (defaultBranch );
974+ GHBranch parentBranch = mock (GHBranch .class );
975+ String sha = "abcdef" ;
976+ when (parentBranch .getSHA1 ()).thenReturn (sha );
977+ when (parentRepo .getBranch (defaultBranch )).thenReturn (parentBranch );
978+
979+ gitHubUtil = mock (GitHubUtil .class );
980+ dockerfileGitHubUtil = Mockito .spy (new DockerfileGitHubUtil (gitHubUtil ));
981+ InputStream stream = new ByteArrayInputStream ("someContent" .getBytes (StandardCharsets .UTF_8 ));
982+ GHContent forkedRepoContent = mock (GHContent .class );
983+ when (forkedRepoContent .read ()).thenReturn (stream );
984+
985+ RateLimiter rateLimiter = mock (RateLimiter .class );
986+ when (gitHubUtil .tryRetrievingContent (eq (forkedRepo ),
987+ eq ("df11" ), eq ("image-tag" ))).thenReturn (forkedRepoContent );
988+ GHContent forkedRepoContent2 = mock (GHContent .class );
989+ when (forkedRepoContent2 .read ()).thenReturn (stream );
990+ when (gitHubUtil .tryRetrievingContent (eq (forkedRepo ),
991+ eq ("df12" ), eq ("image-tag" ))).thenReturn (forkedRepoContent2 );
992+
993+ //First dockerfile not modified
994+ doReturn (false ).when (dockerfileGitHubUtil ).modifyContentOnGithub (eq (forkedRepoContent ),
995+ eq ("image-tag" ),
996+ eq ("image" ),
997+ eq ("tag" ),
998+ eq (null ),
999+ eq (null ));
1000+
1001+ //Second dockerfile modified
1002+ doReturn (true ).when (dockerfileGitHubUtil ).modifyContentOnGithub (eq (forkedRepoContent2 ),
1003+ eq ("image-tag" ),
1004+ eq ("image" ),
1005+ eq ("tag" ),
1006+ eq (null ),
1007+ eq (null ));
1008+
1009+ doNothing ().when (rateLimiter ).consume ();
1010+
1011+ dockerfileGitHubUtil .changeDockerfiles (ns , pathToDockerfilesInParentRepo ,
1012+ new GitHubContentToProcess (forkedRepo , parentRepo , "" ), new ArrayList <>(),
1013+ gitForkBranch , rateLimiter );
1014+
1015+ // Both Dockerfiles retrieved from the same repo
1016+ verify (dockerfileGitHubUtil ).tryRetrievingContent (eq (forkedRepo ),
1017+ eq ("df11" ), eq ("image-tag" ));
1018+ verify (dockerfileGitHubUtil ).tryRetrievingContent (eq (forkedRepo ),
1019+ eq ("df12" ), eq ("image-tag" ));
1020+
1021+ // Trying to modify both Dockerfiles
1022+ verify (dockerfileGitHubUtil , times (2 )).modifyContentOnGithub (any (), eq ("image-tag" ), eq ("image" )
1023+ , eq ("tag" ), any (), any ());
1024+
1025+ // PR created
1026+ verify (dockerfileGitHubUtil ).createPullReq (eq (parentRepo ),
1027+ eq ("image-tag" ), eq (forkedRepo ), any (), eq (rateLimiter ));
1028+ }
1029+
8831030 @ DataProvider
8841031 public Object [][] fromInstructionWithIgnoreStringData () {
8851032 return new Object [][] {
0 commit comments