Skip to content

Commit 89e656b

Browse files
Do not add a repo to parentReposForked if unable to fork
1 parent 887213e commit 89e656b

File tree

4 files changed

+103
-12
lines changed

4 files changed

+103
-12
lines changed

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected void forkRepositoriesFound(Multimap<String, String> pathToDockerfilesI
9595
PagedSearchIterable<GHContent> contentsWithImage,
9696
String image) throws IOException {
9797
log.info("Forking {} repositories...", contentsWithImage.getTotalCount());
98-
List<String> parentReposAlreadyChecked = new ArrayList<>();
98+
List<String> parentReposForked = new ArrayList<>();
9999
GHRepository parent;
100100
String parentRepoName = null;
101101
for (GHContent c : contentsWithImage) {
@@ -115,14 +115,16 @@ protected void forkRepositoriesFound(Multimap<String, String> pathToDockerfilesI
115115
pathToDockerfilesInParentRepo.put(parentRepoName, c.getPath());
116116
imagesFoundInParentRepo.put(parentRepoName, image);
117117

118-
// fork the parent if not already forked or we couldn't fork
119-
if (!parentReposAlreadyChecked.contains(parentRepoName)) {
118+
// fork the parent if not already forked
119+
if (!parentReposForked.contains(parentRepoName)) {
120120
GHRepository fork = dockerfileGitHubUtil.closeOutdatedPullRequestAndFork(parent);
121121
if (fork == null) {
122122
log.info("Could not fork {}", parentRepoName);
123123
pathToDockerfilesInParentRepo.remove(parentRepoName, c.getPath());
124+
imagesFoundInParentRepo.remove(parentRepoName, image);
125+
} else {
126+
parentReposForked.add(parentRepoName);
124127
}
125-
parentReposAlreadyChecked.add(parentRepoName);
126128
}
127129
}
128130
}

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/Parent.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ protected PagedSearchIterable<GHContent> getGHContents(String org, String img)
110110
protected Multimap<String, String> forkRepositoriesFoundAndGetPathToDockerfiles(PagedSearchIterable<GHContent> contentsWithImage) throws IOException {
111111
log.info("Forking repositories...");
112112
Multimap<String, String> pathToDockerfilesInParentRepo = HashMultimap.create();
113-
List<String> parentReposAlreadyChecked = new ArrayList<>();
113+
List<String> parentReposForked = new ArrayList<>();
114114
GHRepository parent;
115115
String parentRepoName = null;
116116
for (GHContent c : contentsWithImage) {
@@ -128,15 +128,16 @@ protected Multimap<String, String> forkRepositoriesFoundAndGetPathToDockerfiles(
128128
parentRepoName);
129129
} else {
130130
pathToDockerfilesInParentRepo.put(parentRepoName, c.getPath());
131-
// fork the parent if not already forked or we couldn't fork
132-
if (!parentReposAlreadyChecked.contains(parentRepoName)) {
131+
// fork the parent if not already forked
132+
if (!parentReposForked.contains(parentRepoName)) {
133133
log.info("Forking {}", parentRepoName);
134134
GHRepository fork = dockerfileGitHubUtil.closeOutdatedPullRequestAndFork(parent);
135135
if (fork == null) {
136136
log.info("Could not fork {}", parentRepoName);
137137
pathToDockerfilesInParentRepo.remove(parentRepoName, c.getPath());
138+
} else {
139+
parentReposForked.add(parentRepoName);
138140
}
139-
parentReposAlreadyChecked.add(parentRepoName);
140141
}
141142
}
142143
}

dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/subcommands/impl/AllTest.java

+49-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@
2525
import java.util.Map;
2626
import java.util.Set;
2727

28-
import static org.mockito.Matchers.any;
29-
import static org.mockito.Matchers.anyString;
30-
import static org.mockito.Matchers.eq;
28+
import static org.mockito.Matchers.*;
3129
import static org.mockito.Mockito.*;
30+
import static org.testng.Assert.assertEquals;
3231
import static org.testng.Assert.assertNotNull;
3332

3433
/**
@@ -61,7 +60,53 @@ public void testForkRepositoriesFound() throws Exception {
6160
all.loadDockerfileGithubUtil(dockerfileGitHubUtil);
6261
all.forkRepositoriesFound(ArrayListMultimap.create(), ArrayListMultimap.create(), contentsWithImage, "image");
6362

64-
Mockito.verify(dockerfileGitHubUtil, times(1)).closeOutdatedPullRequestAndFork(any());
63+
Mockito.verify(dockerfileGitHubUtil, times(3)).closeOutdatedPullRequestAndFork(any());
64+
}
65+
66+
@Test
67+
public void testForkRepositoriesFound_unableToforkRepo() throws Exception {
68+
DockerfileGitHubUtil dockerfileGitHubUtil = mock(DockerfileGitHubUtil.class);
69+
70+
GHRepository contentRepo1 = mock(GHRepository.class);
71+
when(contentRepo1.getFullName()).thenReturn("1");
72+
73+
GHRepository contentRepo2 = mock(GHRepository.class);
74+
when(contentRepo2.getFullName()).thenReturn("1");
75+
76+
GHRepository contentRepo3 = mock(GHRepository.class);
77+
when(contentRepo3.getFullName()).thenReturn("2");
78+
79+
GHContent content1 = mock(GHContent.class);
80+
when(content1.getOwner()).thenReturn(contentRepo1);
81+
when(content1.getPath()).thenReturn("1");
82+
83+
GHContent content2 = mock(GHContent.class);
84+
when(content2.getOwner()).thenReturn(contentRepo2);
85+
when(content2.getPath()).thenReturn("2");
86+
87+
GHContent content3 = mock(GHContent.class);
88+
when(content3.getOwner()).thenReturn(contentRepo3);
89+
when(content3.getPath()).thenReturn("3");
90+
91+
PagedSearchIterable<GHContent> contentsWithImage = mock(PagedSearchIterable.class);
92+
93+
PagedIterator<GHContent> contentsWithImageIterator = mock(PagedIterator.class);
94+
when(contentsWithImageIterator.hasNext()).thenReturn(true, true, true, false);
95+
when(contentsWithImageIterator.next()).thenReturn(content1, content2, content3, null);
96+
when(contentsWithImage.iterator()).thenReturn(contentsWithImageIterator);
97+
when(dockerfileGitHubUtil.closeOutdatedPullRequestAndFork(contentRepo1)).thenReturn(null);
98+
when(dockerfileGitHubUtil.closeOutdatedPullRequestAndFork(contentRepo2)).thenReturn(null);
99+
when(dockerfileGitHubUtil.closeOutdatedPullRequestAndFork(contentRepo3)).thenReturn(new GHRepository());
100+
101+
All all = new All();
102+
all.loadDockerfileGithubUtil(dockerfileGitHubUtil);
103+
Multimap<String, String> pathToDockerfilesInParentRepo = ArrayListMultimap.create();
104+
Multimap<String, String> imagesFoundInParentRepo = ArrayListMultimap.create();
105+
all.forkRepositoriesFound(pathToDockerfilesInParentRepo, imagesFoundInParentRepo, contentsWithImage, "image");
106+
107+
assertEquals(pathToDockerfilesInParentRepo.size(), 1);
108+
assertEquals(imagesFoundInParentRepo.size(), 1);
109+
Mockito.verify(dockerfileGitHubUtil, times(3)).closeOutdatedPullRequestAndFork(any());
65110
}
66111

67112
@Test

dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/subcommands/impl/ParentTest.java

+43
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,49 @@ public void testForkRepositoriesFound() throws Exception {
109109
assertEquals(repoMap.size(), 3);
110110
}
111111

112+
@Test
113+
public void testForkRepositoriesFound_unableToforkRepo() throws Exception {
114+
DockerfileGitHubUtil dockerfileGitHubUtil = mock(DockerfileGitHubUtil.class);
115+
116+
GHRepository contentRepo1 = mock(GHRepository.class);
117+
when(contentRepo1.getFullName()).thenReturn("1");
118+
119+
GHRepository contentRepo2 = mock(GHRepository.class);
120+
when(contentRepo2.getFullName()).thenReturn("1");
121+
122+
GHRepository contentRepo3 = mock(GHRepository.class);
123+
when(contentRepo3.getFullName()).thenReturn("2");
124+
125+
GHContent content1 = mock(GHContent.class);
126+
when(content1.getOwner()).thenReturn(contentRepo1);
127+
when(content1.getPath()).thenReturn("1");
128+
129+
GHContent content2 = mock(GHContent.class);
130+
when(content2.getOwner()).thenReturn(contentRepo2);
131+
when(content2.getPath()).thenReturn("2");
132+
133+
GHContent content3 = mock(GHContent.class);
134+
when(content3.getOwner()).thenReturn(contentRepo3);
135+
when(content3.getPath()).thenReturn("3");
136+
137+
PagedSearchIterable<GHContent> contentsWithImage = mock(PagedSearchIterable.class);
138+
139+
PagedIterator<GHContent> contentsWithImageIterator = mock(PagedIterator.class);
140+
when(contentsWithImageIterator.hasNext()).thenReturn(true, true, true, false);
141+
when(contentsWithImageIterator.next()).thenReturn(content1, content2, content3, null);
142+
when(contentsWithImage.iterator()).thenReturn(contentsWithImageIterator);
143+
when(dockerfileGitHubUtil.closeOutdatedPullRequestAndFork(contentRepo1)).thenReturn(null);
144+
when(dockerfileGitHubUtil.closeOutdatedPullRequestAndFork(contentRepo2)).thenReturn(null);
145+
when(dockerfileGitHubUtil.closeOutdatedPullRequestAndFork(contentRepo3)).thenReturn(new GHRepository());
146+
147+
Parent parent = new Parent();
148+
parent.loadDockerfileGithubUtil(dockerfileGitHubUtil);
149+
Multimap<String, String> repoMap = parent.forkRepositoriesFoundAndGetPathToDockerfiles(contentsWithImage);
150+
151+
verify(dockerfileGitHubUtil, times(3)).closeOutdatedPullRequestAndFork(any());
152+
assertEquals(repoMap.size(), 1);
153+
}
154+
112155
@Test
113156
public void testForkRepositoriesFound_forkRepoIsSkipped() throws Exception {
114157
DockerfileGitHubUtil dockerfileGitHubUtil = mock(DockerfileGitHubUtil.class);

0 commit comments

Comments
 (0)