Skip to content

Commit f409bf1

Browse files
authored
Merge pull request #1130 from avimanyum/main
Handle JSONException while reading Renovate configuration file
2 parents ee90f18 + 1b2fa73 commit f409bf1

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

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

Lines changed: 5 additions & 2 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

@@ -64,9 +65,11 @@ protected boolean isRenovateEnabled(List<String> filePaths, GitHubContentToProce
6465
//If the file has the key 'enabled' set to false, it indicates that while the repo has been onboarded to renovate, it has been disabled for some reason
6566
return readJsonFromContent(fork.getParent().getFileContent(filePath)).optBoolean("enabled", true);
6667
} catch (FileNotFoundException e) {
67-
log.debug("The file with name {} not found in the repository. Exception: {}", filePath, e.getMessage());
68+
log.debug("The file with name {} not found in the repository.Returning false. Exception: {}", filePath, e.getMessage());
6869
} catch (IOException e) {
69-
log.debug("Exception while trying to close a resource. Exception: {}", e.getMessage());
70+
log.warn("Exception while trying to close a resource. Returning false. Exception: {}", e.getMessage());
71+
} catch (JSONException e) {
72+
log.warn("Exception while trying to read the renovate configuration file. Returning false. 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)