Skip to content

Commit

Permalink
Added more meaningful error message when config file not found in rep…
Browse files Browse the repository at this point in the history
…ository, with test
  • Loading branch information
mskacelik committed Feb 24, 2025
1 parent b585334 commit 8ef09d9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/main/java/org/wildfly/bot/LifecycleProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> emailAddresses = wildflyBotConfigFile.wildfly.emails;
List<String> problems = configFileChangeProcessor.validateFile(wildflyBotConfigFile, repository);

Expand Down
36 changes: 33 additions & 3 deletions src/test/java/org/wildfly/bot/StartupEventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ private class CustomGithubMockSetup implements GitHubMockSetup {
private final String configFile;
private final Consumer<GitHubMockSetupContext> 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<GitHubMockSetupContext> additionalMocking) {
Expand All @@ -118,7 +122,9 @@ public void setup(GitHubMockSetupContext mocks) throws Throwable {
GHAuthenticatedAppInstallation mockGHAuthenticatedAppInstallation = mock(GHAuthenticatedAppInstallation.class);
PagedSearchIterable<GHRepository> mockGHRepositories = mock(PagedSearchIterable.class);
PagedIterator<GHRepository> 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);

Expand Down Expand Up @@ -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<? extends Throwable> 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()
}
}

0 comments on commit 8ef09d9

Please sign in to comment.