Skip to content

Commit

Permalink
Merge pull request #1130 from avimanyum/main
Browse files Browse the repository at this point in the history
Handle JSONException while reading Renovate configuration file
  • Loading branch information
avimanyum authored Feb 13, 2024
2 parents ee90f18 + 1b2fa73 commit f409bf1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.sourceforge.argparse4j.inf.*;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONException;
import org.kohsuke.github.*;
import org.slf4j.*;

Expand Down Expand Up @@ -64,9 +65,11 @@ protected boolean isRenovateEnabled(List<String> filePaths, GitHubContentToProce
//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
return readJsonFromContent(fork.getParent().getFileContent(filePath)).optBoolean("enabled", true);
} catch (FileNotFoundException e) {
log.debug("The file with name {} not found in the repository. Exception: {}", filePath, e.getMessage());
log.debug("The file with name {} not found in the repository.Returning false. Exception: {}", filePath, e.getMessage());
} catch (IOException e) {
log.debug("Exception while trying to close a resource. Exception: {}", e.getMessage());
log.warn("Exception while trying to close a resource. Returning false. Exception: {}", e.getMessage());
} catch (JSONException e) {
log.warn("Exception while trying to read the renovate configuration file. Returning false. Exception: {}", e.getMessage());
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.salesforce.dockerfileimageupdate.model.*;
import com.salesforce.dockerfileimageupdate.process.*;
import net.sourceforge.argparse4j.inf.*;
import org.json.JSONException;
import org.kohsuke.github.*;
import org.mockito.*;
import org.testng.*;
Expand Down Expand Up @@ -199,7 +200,7 @@ public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundButEnabledK
}

@Test
public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndResourcesThrowAnException() throws IOException {
public void testisRenovateEnabledReturnsFalseIfRenovateConfigFileFoundAndResourcesThrowAnException() throws IOException {
PullRequests pullRequests = new PullRequests();
List<String> filePaths = Collections.singletonList("renovate.json");
GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class);
Expand All @@ -211,6 +212,19 @@ public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndResource
Assert.assertFalse(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess));
}

@Test
public void testisRenovateEnabledReturnsFalseIfRenovateConfigFileFoundAndJSONParsingThrowsAnException() throws IOException {
PullRequests pullRequests = new PullRequests();
List<String> filePaths = Collections.singletonList("renovate.json");
GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class);
GHRepository ghRepository = mock(GHRepository.class);
GHContent content = mock(GHContent.class);
when(gitHubContentToProcess.getParent()).thenReturn(ghRepository);
when(ghRepository.getFileContent(anyString())).thenReturn(content);
when(content.read()).thenThrow(new JSONException(""));
Assert.assertFalse(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess));
}

@Test
public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndEnabledKeySetToTrue() throws IOException {
PullRequests pullRequests = new PullRequests();
Expand Down

0 comments on commit f409bf1

Please sign in to comment.