Skip to content

Commit 42bbb09

Browse files
committed
Creating PR only when files modified
1 parent 2f4f946 commit 42bbb09

File tree

2 files changed

+165
-12
lines changed

2 files changed

+165
-12
lines changed

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,10 @@ public GHBlob tryRetrievingBlob(GHRepository repo, String path, String branch)
277277
return gitHubUtil.tryRetrievingBlob(repo, path, branch);
278278
}
279279

280-
public Boolean modifyOnGithub(GHContent content,
280+
public boolean modifyOnGithub(GHContent content,
281281
String branch, String img, String tag,
282282
String customMessage, String ignoreImageString) throws IOException {
283-
Boolean modified = false;
283+
boolean modified;
284284
try (InputStream stream = content.read();
285285
InputStreamReader streamR = new InputStreamReader(stream);
286286
BufferedReader reader = new BufferedReader(streamR)) {
@@ -290,7 +290,7 @@ public Boolean modifyOnGithub(GHContent content,
290290
return modified;
291291
}
292292

293-
protected Boolean findImagesAndFix(GHContent content, String branch, String img,
293+
protected boolean findImagesAndFix(GHContent content, String branch, String img,
294294
String tag, String customMessage, BufferedReader reader,
295295
String ignoreImageString) throws IOException {
296296
StringBuilder strB = new StringBuilder();
@@ -546,9 +546,12 @@ public void changeDockerfiles(Namespace ns,
546546
if (content == null) {
547547
log.info("No Dockerfile found at path: '{}'", pathToDockerfile);
548548
} else {
549-
isContentModified = modifyOnGithub(content, gitForkBranch.getBranchName(),
549+
if(modifyOnGithub(content, gitForkBranch.getBranchName(),
550550
gitForkBranch.getImageName(), gitForkBranch.getImageTag(),
551-
ns.get(Constants.GIT_ADDITIONAL_COMMIT_MESSAGE), ns.get(Constants.IGNORE_IMAGE_STRING));
551+
ns.get(Constants.GIT_ADDITIONAL_COMMIT_MESSAGE),
552+
ns.get(Constants.IGNORE_IMAGE_STRING))) {
553+
isContentModified = true;
554+
}
552555
isRepoSkipped = false;
553556
}
554557
}

dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtilTest.java

Lines changed: 157 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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);
@@ -843,9 +841,6 @@ public void testOnePullRequestForMultipleDockerfilesInSameRepo() throws Exceptio
843841

844842
gitHubUtil = mock(GitHubUtil.class);
845843
dockerfileGitHubUtil = Mockito.spy(new DockerfileGitHubUtil(gitHubUtil));
846-
//when(dockerfileGitHubUtil.getPullRequestForImageBranch(any(), any())).thenReturn
847-
// (Optional.empty());
848-
//when(dockerfileGitHubUtil.getRepo(forkedRepo.getFullName())).thenReturn(forkedRepo);
849844
InputStream stream = new ByteArrayInputStream("someContent".getBytes(StandardCharsets.UTF_8));
850845
GHContent forkedRepoContent1 = mock(GHContent.class);
851846
when(forkedRepoContent1.read()).thenReturn(stream);
@@ -857,8 +852,21 @@ public void testOnePullRequestForMultipleDockerfilesInSameRepo() throws Exceptio
857852
when(forkedRepoContent2.read()).thenReturn(stream);
858853
when(gitHubUtil.tryRetrievingContent(eq(forkedRepo),
859854
eq("df12"), eq("image-tag"))).thenReturn(forkedRepoContent2);
860-
doNothing().when(dockerfileGitHubUtil).modifyOnGithub(any(), eq("image-tag"), eq("image")
861-
, eq("tag"), anyString(), anyString());
855+
856+
//Both Dockerfiles modified
857+
doReturn(true).when(dockerfileGitHubUtil).modifyOnGithub(eq(forkedRepoContent1),
858+
eq("image-tag"),
859+
eq("image"),
860+
eq("tag"),
861+
eq(null),
862+
eq(null));
863+
doReturn(true).when(dockerfileGitHubUtil).modifyOnGithub(eq(forkedRepoContent2),
864+
eq("image-tag"),
865+
eq("image"),
866+
eq("tag"),
867+
eq(null),
868+
eq(null));
869+
862870
doNothing().when(rateLimiter).consume();
863871

864872
dockerfileGitHubUtil.changeDockerfiles(ns, pathToDockerfilesInParentRepo,
@@ -880,6 +888,148 @@ public void testOnePullRequestForMultipleDockerfilesInSameRepo() throws Exceptio
880888
eq("image-tag"), eq(forkedRepo), any(), eq(rateLimiter));
881889
}
882890

891+
@Test
892+
public void testPullRequestBlockNotExecutedWhenDockerfileIsNotModified() throws Exception {
893+
Map<String, Object> nsMap = ImmutableMap.of(Constants.IMG,
894+
"image", Constants.TAG,
895+
"tag", Constants.STORE,
896+
"store");
897+
Namespace ns = new Namespace(nsMap);
898+
GitForkBranch gitForkBranch = new GitForkBranch("image", "tag", null, "");
899+
Multimap<String, GitHubContentToProcess> pathToDockerfilesInParentRepo = HashMultimap.create();
900+
pathToDockerfilesInParentRepo.put("repo", new GitHubContentToProcess(null, null, "df11"));
901+
902+
GHRepository forkedRepo = mock(GHRepository.class);
903+
when(forkedRepo.isFork()).thenReturn(true);
904+
when(forkedRepo.getFullName()).thenReturn("forkedrepo");
905+
906+
GHRepository parentRepo = mock(GHRepository.class);
907+
when(parentRepo.getFullName()).thenReturn("repo");
908+
when(forkedRepo.getParent()).thenReturn(parentRepo);
909+
910+
//GHRepository parent = mock(GHRepository.class);
911+
String defaultBranch = "default";
912+
when(parentRepo.getDefaultBranch()).thenReturn(defaultBranch);
913+
GHBranch parentBranch = mock(GHBranch.class);
914+
String sha = "abcdef";
915+
when(parentBranch.getSHA1()).thenReturn(sha);
916+
when(parentRepo.getBranch(defaultBranch)).thenReturn(parentBranch);
917+
918+
gitHubUtil = mock(GitHubUtil.class);
919+
dockerfileGitHubUtil = Mockito.spy(new DockerfileGitHubUtil(gitHubUtil));
920+
InputStream stream = new ByteArrayInputStream("someContent".getBytes(StandardCharsets.UTF_8));
921+
GHContent forkedRepoContent = mock(GHContent.class);
922+
when(forkedRepoContent.read()).thenReturn(stream);
923+
924+
RateLimiter rateLimiter = mock(RateLimiter.class);
925+
when(gitHubUtil.tryRetrievingContent(eq(forkedRepo),
926+
eq("df11"), eq("image-tag"))).thenReturn(forkedRepoContent);
927+
928+
//Dockerfile not modified
929+
doReturn(false).when(dockerfileGitHubUtil).modifyOnGithub(eq(forkedRepoContent),
930+
eq("image-tag"),
931+
eq("image"),
932+
eq("tag"),
933+
eq(null),
934+
eq(null));
935+
936+
doNothing().when(rateLimiter).consume();
937+
938+
dockerfileGitHubUtil.changeDockerfiles(ns, pathToDockerfilesInParentRepo,
939+
new GitHubContentToProcess(forkedRepo, parentRepo, ""), new ArrayList<>(),
940+
gitForkBranch, rateLimiter);
941+
942+
// Dockerfile retrieved from the repo
943+
verify(dockerfileGitHubUtil).tryRetrievingContent(eq(forkedRepo),
944+
eq("df11"), eq("image-tag"));
945+
946+
// Trying to modify Dockerfile
947+
verify(dockerfileGitHubUtil).modifyOnGithub(any(), eq("image-tag"), eq("image"), eq("tag"), any(), any());
948+
949+
// PR creation block not executed
950+
verify(dockerfileGitHubUtil, times(0)).createPullReq(eq(parentRepo),
951+
eq("image-tag"), eq(forkedRepo), any(), eq(rateLimiter));
952+
}
953+
954+
@Test
955+
public void testPullRequestCreatedWhenAnyDockerfileIsModified() throws Exception {
956+
Map<String, Object> nsMap = ImmutableMap.of(Constants.IMG,
957+
"image", Constants.TAG,
958+
"tag", Constants.STORE,
959+
"store");
960+
Namespace ns = new Namespace(nsMap);
961+
GitForkBranch gitForkBranch = new GitForkBranch("image", "tag", null, "");
962+
Multimap<String, GitHubContentToProcess> pathToDockerfilesInParentRepo = HashMultimap.create();
963+
pathToDockerfilesInParentRepo.put("repo", new GitHubContentToProcess(null, null, "df11"));
964+
pathToDockerfilesInParentRepo.put("repo", new GitHubContentToProcess(null, null, "df12"));
965+
966+
GHRepository forkedRepo = mock(GHRepository.class);
967+
when(forkedRepo.isFork()).thenReturn(true);
968+
when(forkedRepo.getFullName()).thenReturn("forkedrepo");
969+
970+
GHRepository parentRepo = mock(GHRepository.class);
971+
when(parentRepo.getFullName()).thenReturn("repo");
972+
when(forkedRepo.getParent()).thenReturn(parentRepo);
973+
974+
//GHRepository parent = mock(GHRepository.class);
975+
String defaultBranch = "default";
976+
when(parentRepo.getDefaultBranch()).thenReturn(defaultBranch);
977+
GHBranch parentBranch = mock(GHBranch.class);
978+
String sha = "abcdef";
979+
when(parentBranch.getSHA1()).thenReturn(sha);
980+
when(parentRepo.getBranch(defaultBranch)).thenReturn(parentBranch);
981+
982+
gitHubUtil = mock(GitHubUtil.class);
983+
dockerfileGitHubUtil = Mockito.spy(new DockerfileGitHubUtil(gitHubUtil));
984+
InputStream stream = new ByteArrayInputStream("someContent".getBytes(StandardCharsets.UTF_8));
985+
GHContent forkedRepoContent = mock(GHContent.class);
986+
when(forkedRepoContent.read()).thenReturn(stream);
987+
988+
RateLimiter rateLimiter = mock(RateLimiter.class);
989+
when(gitHubUtil.tryRetrievingContent(eq(forkedRepo),
990+
eq("df11"), eq("image-tag"))).thenReturn(forkedRepoContent);
991+
GHContent forkedRepoContent2 = mock(GHContent.class);
992+
when(forkedRepoContent2.read()).thenReturn(stream);
993+
when(gitHubUtil.tryRetrievingContent(eq(forkedRepo),
994+
eq("df12"), eq("image-tag"))).thenReturn(forkedRepoContent2);
995+
996+
//First dockerfile not modified
997+
doReturn(false).when(dockerfileGitHubUtil).modifyOnGithub(eq(forkedRepoContent),
998+
eq("image-tag"),
999+
eq("image"),
1000+
eq("tag"),
1001+
eq(null),
1002+
eq(null));
1003+
1004+
//Second dockerfile modified
1005+
doReturn(true).when(dockerfileGitHubUtil).modifyOnGithub(eq(forkedRepoContent2),
1006+
eq("image-tag"),
1007+
eq("image"),
1008+
eq("tag"),
1009+
eq(null),
1010+
eq(null));
1011+
1012+
doNothing().when(rateLimiter).consume();
1013+
1014+
dockerfileGitHubUtil.changeDockerfiles(ns, pathToDockerfilesInParentRepo,
1015+
new GitHubContentToProcess(forkedRepo, parentRepo, ""), new ArrayList<>(),
1016+
gitForkBranch, rateLimiter);
1017+
1018+
// Both Dockerfiles retrieved from the same repo
1019+
verify(dockerfileGitHubUtil).tryRetrievingContent(eq(forkedRepo),
1020+
eq("df11"), eq("image-tag"));
1021+
verify(dockerfileGitHubUtil).tryRetrievingContent(eq(forkedRepo),
1022+
eq("df12"), eq("image-tag"));
1023+
1024+
// Trying to modify both Dockerfiles
1025+
verify(dockerfileGitHubUtil, times(2)).modifyOnGithub(any(), eq("image-tag"), eq("image")
1026+
, eq("tag"), any(), any());
1027+
1028+
// PR created
1029+
verify(dockerfileGitHubUtil).createPullReq(eq(parentRepo),
1030+
eq("image-tag"), eq(forkedRepo), any(), eq(rateLimiter));
1031+
}
1032+
8831033
@DataProvider
8841034
public Object[][] fromInstructionWithIgnoreStringData() {
8851035
return new Object[][] {

0 commit comments

Comments
 (0)