From 8ef09d96beda7264d3ffbb0651156f34687ffe59 Mon Sep 17 00:00:00 2001 From: Marek Skacelik Date: Mon, 24 Feb 2025 15:02:30 +0100 Subject: [PATCH] Added more meaningful error message when config file not found in repository, with test --- .../org/wildfly/bot/LifecycleProcessor.java | 7 +++- .../org/wildfly/bot/StartupEventTest.java | 36 +++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/wildfly/bot/LifecycleProcessor.java b/src/main/java/org/wildfly/bot/LifecycleProcessor.java index 953da77..5a68d08 100644 --- a/src/main/java/org/wildfly/bot/LifecycleProcessor.java +++ b/src/main/java/org/wildfly/bot/LifecycleProcessor.java @@ -72,7 +72,12 @@ void onStart(@Observes StartupEvent event) { for (GHRepository repository : app.getInstallation().listRepositories()) { try { WildFlyConfigFile wildflyBotConfigFile = fileProvider.fetchConfigFile(repository, - RuntimeConstants.CONFIG_FILE_NAME, ConfigFile.Source.DEFAULT, WildFlyConfigFile.class).get(); + RuntimeConstants.CONFIG_FILE_NAME, ConfigFile.Source.DEFAULT, WildFlyConfigFile.class) + .orElseThrow( + () -> new IllegalStateException( + "Unable to read file %s for repository %s. Either the file does not exist or the 'Contents' permission has not been set for the application." + .formatted(".github/" + RuntimeConstants.CONFIG_FILE_NAME, + repository.getFullName()))); List emailAddresses = wildflyBotConfigFile.wildfly.emails; List problems = configFileChangeProcessor.validateFile(wildflyBotConfigFile, repository); diff --git a/src/test/java/org/wildfly/bot/StartupEventTest.java b/src/test/java/org/wildfly/bot/StartupEventTest.java index 351dc8d..a600bc0 100644 --- a/src/test/java/org/wildfly/bot/StartupEventTest.java +++ b/src/test/java/org/wildfly/bot/StartupEventTest.java @@ -98,9 +98,13 @@ private class CustomGithubMockSetup implements GitHubMockSetup { private final String configFile; private final Consumer additionalMocking; + // no config file (wildfly-bot.yml) was provided + private CustomGithubMockSetup() { + this(null); + } + private CustomGithubMockSetup(String configFile) { - this.configFile = configFile; - this.additionalMocking = null; + this(configFile, null); } private CustomGithubMockSetup(String configFile, Consumer additionalMocking) { @@ -118,7 +122,9 @@ public void setup(GitHubMockSetupContext mocks) throws Throwable { GHAuthenticatedAppInstallation mockGHAuthenticatedAppInstallation = mock(GHAuthenticatedAppInstallation.class); PagedSearchIterable mockGHRepositories = mock(PagedSearchIterable.class); PagedIterator mockIterator = mock(PagedIterator.class); - mocks.configFile(RuntimeConstants.CONFIG_FILE_NAME).fromString(configFile); + if (configFile != null) { + mocks.configFile(RuntimeConstants.CONFIG_FILE_NAME).fromString(configFile); + } GHRepository repo = mocks.repository(TestConstants.TEST_REPO); @@ -319,4 +325,28 @@ public void testCreateOneMissingLabelsLabel() throws IOException { verify(repository, never()).createLabel(eq(LABEL_FIX_ME), anyString()); }); } + + @Test + public void testMissingConfigFile() { + final String expectedErrorLog = "Unable to retrieve or parse the configuration file from the repository %s"; + final String expectedExceptionMessage = "Unable to read file .github/wildfly-bot.yml for repository"; + + given().github(new CustomGithubMockSetup()) + .when().payloadFromString(ssePullRequestPayload.jsonString()) + .event(GHEvent.STAR); + + Assertions.assertTrue(inMemoryLogHandler.getRecords().stream().anyMatch( + logRecord -> logRecord.getMessage().equals(expectedErrorLog) + && assertThrowable(logRecord.getThrown(), IllegalStateException.class, expectedExceptionMessage))); + + } + + private boolean assertThrowable(Throwable throwable, Class expectedExceptionClass, + String expectedMessage) { + Assertions.assertEquals(expectedExceptionClass, throwable.getClass()); + Assertions.assertTrue(throwable.getMessage().contains(expectedMessage), + "Expected message: \n%s\nwas not found in the throwable message: \n%s\n".formatted(expectedMessage, + throwable.getMessage())); + return true; // for lazy evaluation of .anyMatch() + } } \ No newline at end of file