Skip to content

Commit 53a890a

Browse files
committed
Catch json exception
1 parent 8fb7a2f commit 53a890a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.sourceforge.argparse4j.inf.*;
77
import org.json.JSONObject;
88
import org.json.JSONTokener;
9+
import org.json.JSONException;
910
import org.kohsuke.github.*;
1011
import org.slf4j.*;
1112

@@ -67,6 +68,8 @@ protected boolean isRenovateEnabled(List<String> filePaths, GitHubContentToProce
6768
log.debug("The file with name {} not found in the repository. Exception: {}", filePath, e.getMessage());
6869
} catch (IOException e) {
6970
log.debug("Exception while trying to close a resource. Exception: {}", e.getMessage());
71+
} catch (JSONException e) {
72+
log.debug("Exception while trying to read the renovate configuration file. Exception: {}", e.getMessage());
7073
}
7174
}
7275
return false;

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.salesforce.dockerfileimageupdate.model.*;
55
import com.salesforce.dockerfileimageupdate.process.*;
66
import net.sourceforge.argparse4j.inf.*;
7+
import org.json.JSONException;
78
import org.kohsuke.github.*;
89
import org.mockito.*;
910
import org.testng.*;
@@ -199,7 +200,7 @@ public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundButEnabledK
199200
}
200201

201202
@Test
202-
public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndResourcesThrowAnException() throws IOException {
203+
public void testisRenovateEnabledReturnsFalseIfRenovateConfigFileFoundAndResourcesThrowAnException() throws IOException {
203204
PullRequests pullRequests = new PullRequests();
204205
List<String> filePaths = Collections.singletonList("renovate.json");
205206
GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class);
@@ -211,6 +212,19 @@ public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndResource
211212
Assert.assertFalse(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess));
212213
}
213214

215+
@Test
216+
public void testisRenovateEnabledReturnsFalseIfRenovateConfigFileFoundAndJSONParsingThrowsAnException() throws IOException {
217+
PullRequests pullRequests = new PullRequests();
218+
List<String> filePaths = Collections.singletonList("renovate.json");
219+
GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class);
220+
GHRepository ghRepository = mock(GHRepository.class);
221+
GHContent content = mock(GHContent.class);
222+
when(gitHubContentToProcess.getParent()).thenReturn(ghRepository);
223+
when(ghRepository.getFileContent(anyString())).thenReturn(content);
224+
when(content.read()).thenThrow(new JSONException(""));
225+
Assert.assertFalse(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess));
226+
}
227+
214228
@Test
215229
public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndEnabledKeySetToTrue() throws IOException {
216230
PullRequests pullRequests = new PullRequests();

0 commit comments

Comments
 (0)