66import net .sourceforge .argparse4j .inf .*;
77import org .kohsuke .github .*;
88import org .mockito .*;
9+ import org .testng .*;
910import org .testng .annotations .*;
1011
1112import java .io .*;
@@ -21,7 +22,7 @@ public void testPullRequestsPrepareToCreateSuccessful() throws Exception {
2122 "image" , Constants .TAG ,
2223 "tag" , Constants .STORE ,
2324 "store" , Constants .SKIP_PR_CREATION ,
24- false );
25+ false , Constants . CHECK_FOR_RENOVATE , false );
2526 Namespace ns = new Namespace (nsMap );
2627 PullRequests pullRequests = new PullRequests ();
2728 GitHubPullRequestSender pullRequestSender = mock (GitHubPullRequestSender .class );
@@ -51,7 +52,7 @@ public void testPullRequestsPrepareThrowsException() throws Exception {
5152 "image" , Constants .TAG ,
5253 "tag" , Constants .STORE ,
5354 "store" , Constants .SKIP_PR_CREATION ,
54- false );
55+ false , Constants . CHECK_FOR_RENOVATE , false );
5556 Namespace ns = new Namespace (nsMap );
5657 PullRequests pullRequests = new PullRequests ();
5758 GitHubPullRequestSender pullRequestSender = mock (GitHubPullRequestSender .class );
@@ -89,7 +90,7 @@ public void testPullRequestsPrepareToCreateWhenNoDockerfileFound() throws Except
8990 "image" , Constants .TAG ,
9091 "tag" , Constants .STORE ,
9192 "store" , Constants .SKIP_PR_CREATION ,
92- false );
93+ false , Constants . CHECK_FOR_RENOVATE , false );
9394 Namespace ns = new Namespace (nsMap );
9495 PullRequests pullRequests = new PullRequests ();
9596 GitHubPullRequestSender pullRequestSender = mock (GitHubPullRequestSender .class );
@@ -110,4 +111,117 @@ public void testPullRequestsPrepareToCreateWhenNoDockerfileFound() throws Except
110111 eq (pathToDockerfilesInParentRepo ),
111112 eq (gitHubContentToProcess ), anyList (), eq (gitForkBranch ),eq (rateLimiter ));
112113 }
114+
115+ @ Test
116+ public void testPullRequestsPrepareSkipsSendingPRIfRepoOnboardedToRenovate () throws Exception {
117+ Map <String , Object > nsMap = ImmutableMap .of (
118+ Constants .IMG , "image" ,
119+ Constants .TAG , "tag" ,
120+ Constants .STORE ,"store" ,
121+ Constants .SKIP_PR_CREATION ,false ,
122+ Constants .CHECK_FOR_RENOVATE , true );
123+
124+
125+ Namespace ns = new Namespace (nsMap );
126+ PullRequests pullRequests = new PullRequests ();
127+ GitHubPullRequestSender pullRequestSender = mock (GitHubPullRequestSender .class );
128+ PagedSearchIterable <GHContent > contentsFoundWithImage = mock (PagedSearchIterable .class );
129+ GitForkBranch gitForkBranch = mock (GitForkBranch .class );
130+ DockerfileGitHubUtil dockerfileGitHubUtil = mock (DockerfileGitHubUtil .class );
131+ RateLimiter rateLimiter = Mockito .spy (new RateLimiter ());
132+ Multimap <String , GitHubContentToProcess > pathToDockerfilesInParentRepo = ArrayListMultimap .create ();
133+ GitHubContentToProcess gitHubContentToProcess = mock (GitHubContentToProcess .class );
134+ pathToDockerfilesInParentRepo .put ("repo1" , gitHubContentToProcess );
135+ pathToDockerfilesInParentRepo .put ("repo2" , gitHubContentToProcess );
136+ pathToDockerfilesInParentRepo .put ("repo3" , gitHubContentToProcess );
137+ GHContent content = mock (GHContent .class );
138+ InputStream inputStream1 = new ByteArrayInputStream ("{someKey:someValue}" .getBytes ());
139+ InputStream inputStream2 = new ByteArrayInputStream ("{enabled:false}" .getBytes ());
140+ GHRepository ghRepository = mock (GHRepository .class );
141+
142+ when (pullRequestSender .forkRepositoriesFoundAndGetPathToDockerfiles (contentsFoundWithImage , gitForkBranch )).thenReturn (pathToDockerfilesInParentRepo );
143+ when (gitHubContentToProcess .getParent ()).thenReturn (ghRepository );
144+ //Fetch the content of the renovate.json file for the 3 repos.
145+ // The first one returns a file with regular json content.
146+ // The second one returns a file with the key 'enabled' set to 'false' to replicate a repo that has been onboarded to renovate but has it disabled
147+ // The third repo does not have the renovate.json file
148+ when (ghRepository .getFileContent (anyString ())).thenReturn (content ).thenReturn (content ).thenThrow (new FileNotFoundException ());
149+ when (ghRepository .getFullName ()).thenReturn ("org/repo" );
150+ when (content .read ()).thenReturn (inputStream1 ).thenReturn (inputStream2 );
151+
152+ pullRequests .prepareToCreate (ns , pullRequestSender , contentsFoundWithImage ,
153+ gitForkBranch , dockerfileGitHubUtil , rateLimiter );
154+
155+ //Verify that the DFIU PR is skipped for the first repo, but is sent to the other two repos
156+ verify (dockerfileGitHubUtil , times (2 )).changeDockerfiles (eq (ns ),
157+ eq (pathToDockerfilesInParentRepo ),
158+ eq (gitHubContentToProcess ), anyList (), eq (gitForkBranch ),
159+ eq (rateLimiter ));
160+ }
161+
162+ @ Test
163+ public void testisRenovateEnabledReturnsFalseIfRenovateConfigFileNotFound () throws IOException {
164+ PullRequests pullRequests = new PullRequests ();
165+ List <String > filePaths = Collections .singletonList ("renovate.json" );
166+ GitHubContentToProcess gitHubContentToProcess = mock (GitHubContentToProcess .class );
167+ GHRepository ghRepository = mock (GHRepository .class );
168+ when (gitHubContentToProcess .getParent ()).thenReturn (ghRepository );
169+ when (ghRepository .getFileContent (anyString ())).thenThrow (new FileNotFoundException ());
170+ Assert .assertFalse (pullRequests .isRenovateEnabled (filePaths , gitHubContentToProcess ));
171+ }
172+
173+ @ Test
174+ public void testisRenovateEnabledReturnsFalseIfRenovateConfigFileFoundButIsDisabled () throws IOException {
175+ PullRequests pullRequests = new PullRequests ();
176+ List <String > filePaths = Collections .singletonList ("renovate.json" );
177+ GitHubContentToProcess gitHubContentToProcess = mock (GitHubContentToProcess .class );
178+ GHRepository ghRepository = mock (GHRepository .class );
179+ GHContent content = mock (GHContent .class );
180+ InputStream inputStream = new ByteArrayInputStream ("{enabled:false}" .getBytes ());
181+ when (gitHubContentToProcess .getParent ()).thenReturn (ghRepository );
182+ when (ghRepository .getFileContent (anyString ())).thenReturn (content );
183+ when (content .read ()).thenReturn (inputStream );
184+ Assert .assertFalse (pullRequests .isRenovateEnabled (filePaths , gitHubContentToProcess ));
185+ }
186+
187+ @ Test
188+ public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundButEnabledKeyNotFound () throws IOException {
189+ PullRequests pullRequests = new PullRequests ();
190+ List <String > filePaths = Collections .singletonList ("renovate.json" );
191+ GitHubContentToProcess gitHubContentToProcess = mock (GitHubContentToProcess .class );
192+ GHRepository ghRepository = mock (GHRepository .class );
193+ GHContent content = mock (GHContent .class );
194+ InputStream inputStream = new ByteArrayInputStream ("{someKey:someValue}" .getBytes ());
195+ when (gitHubContentToProcess .getParent ()).thenReturn (ghRepository );
196+ when (ghRepository .getFileContent (anyString ())).thenReturn (content );
197+ when (content .read ()).thenReturn (inputStream );
198+ Assert .assertTrue (pullRequests .isRenovateEnabled (filePaths , gitHubContentToProcess ));
199+ }
200+
201+ @ Test
202+ public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndResourcesThrowAnException () throws IOException {
203+ PullRequests pullRequests = new PullRequests ();
204+ List <String > filePaths = Collections .singletonList ("renovate.json" );
205+ GitHubContentToProcess gitHubContentToProcess = mock (GitHubContentToProcess .class );
206+ GHRepository ghRepository = mock (GHRepository .class );
207+ GHContent content = mock (GHContent .class );
208+ when (gitHubContentToProcess .getParent ()).thenReturn (ghRepository );
209+ when (ghRepository .getFileContent (anyString ())).thenReturn (content );
210+ when (content .read ()).thenThrow (new IOException ());
211+ Assert .assertFalse (pullRequests .isRenovateEnabled (filePaths , gitHubContentToProcess ));
212+ }
213+
214+ @ Test
215+ public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndEnabledKeySetToTrue () throws IOException {
216+ PullRequests pullRequests = new PullRequests ();
217+ List <String > filePaths = Collections .singletonList ("renovate.json" );
218+ GitHubContentToProcess gitHubContentToProcess = mock (GitHubContentToProcess .class );
219+ GHRepository ghRepository = mock (GHRepository .class );
220+ GHContent content = mock (GHContent .class );
221+ InputStream inputStream = new ByteArrayInputStream ("{enabled:true}" .getBytes ());
222+ when (gitHubContentToProcess .getParent ()).thenReturn (ghRepository );
223+ when (ghRepository .getFileContent (anyString ())).thenReturn (content );
224+ when (content .read ()).thenReturn (inputStream );
225+ Assert .assertTrue (pullRequests .isRenovateEnabled (filePaths , gitHubContentToProcess ));
226+ }
113227}
0 commit comments