Skip to content

Commit 19857a4

Browse files
committed
load from configmap if available
1 parent 7635417 commit 19857a4

File tree

2 files changed

+81
-12
lines changed

2 files changed

+81
-12
lines changed

src/main/java/com/redhat/labs/omp/service/ConfigService.java

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.redhat.labs.omp.service;
22

3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
37
import java.util.ArrayList;
48
import java.util.List;
59
import java.util.Optional;
@@ -25,31 +29,40 @@ public class ConfigService {
2529

2630
@ConfigProperty(name = "config.file")
2731
String configFile;
28-
32+
2933
@ConfigProperty(name = "webhook.file")
3034
String webHooksFile;
3135

3236
@ConfigProperty(name = "config.repository.id", defaultValue = "9407")
3337
String configRepositoryId;
34-
38+
3539
@ConfigProperty(name = "config.gitlab.ref", defaultValue = "master")
3640
String gitRef;
37-
41+
3842
List<HookConfig> hookConfigList;
43+
File configuration;
3944

4045
@Inject
4146
FileService fileService;
42-
47+
4348
@Inject
4449
JsonMarshaller marshaller;
45-
50+
4651
void onStart(@Observes StartupEvent event) {
4752
hookConfigList = marshaller.fromYamlFile(webHooksFile, HookConfig.class);
4853
LOGGER.debug("Hook Config List {}", hookConfigList);
54+
String content = readFile(configFile);
55+
if (null != content) {
56+
configuration = File.builder().filePath(configFile).content(content).build();
57+
}
4958
}
5059

5160
public File getConfigFile() {
5261

62+
if (null != configuration) {
63+
return configuration;
64+
}
65+
5366
Optional<File> optional = fileService.getFile(configRepositoryId, configFile, gitRef);
5467

5568
if (!optional.isPresent()) {
@@ -58,25 +71,42 @@ public File getConfigFile() {
5871

5972
return optional.get();
6073
}
61-
74+
6275
public List<HookConfig> getHookConfig() {
6376

64-
if(hookConfigList != null) {
77+
if (hookConfigList != null) {
6578
return hookConfigList;
6679
}
67-
80+
6881
String gitLabHookFile = webHooksFile.charAt(0) == '/' ? webHooksFile.substring(1) : webHooksFile;
6982
Optional<File> optional = fileService.getFile(configRepositoryId, gitLabHookFile, gitRef);
7083

7184
if (!optional.isPresent()) {
7285
LOGGER.error("No webhook file could be found. This is abnormal but not a deal breaker");
7386
return new ArrayList<>();
7487
}
75-
88+
7689
File file = optional.get();
77-
90+
7891
return marshaller.fromYaml(file.getContent(), HookConfig.class);
79-
92+
93+
}
94+
95+
private String readFile(String file) {
96+
97+
Path path = Paths.get(file);
98+
99+
if (Files.isReadable(path)) {
100+
LOGGER.debug("Loading config file {}", file);
101+
try {
102+
return new String(Files.readAllBytes(path));
103+
} catch (IOException e) {
104+
LOGGER.error(String.format("Found but unable to read file %s", file), e);
105+
}
106+
}
107+
108+
return null;
109+
80110
}
81111

82112
}

src/test/java/com/redhat/labs/omp/service/ConfigServiceTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,48 @@
1111
import com.redhat.labs.exception.FileNotFoundException;
1212
import com.redhat.labs.omp.config.JsonMarshaller;
1313
import com.redhat.labs.omp.mocks.MockGitLabService;
14+
import com.redhat.labs.omp.models.gitlab.File;
1415
import com.redhat.labs.omp.models.gitlab.HookConfig;
16+
import com.redhat.labs.utils.ResourceLoader;
1517

1618
import io.quarkus.runtime.StartupEvent;
1719

1820
class ConfigServiceTest {
19-
21+
22+
@Test void testGetConfigFilePreLoaded() {
23+
ConfigService service = new ConfigService();
24+
service.configFile = "src/test/resources/config.yml";
25+
service.webHooksFile = "src/test/resources/webhooks.yaml";
26+
service.marshaller = new JsonMarshaller();
27+
28+
service.onStart(new StartupEvent());
29+
File config = service.getConfigFile();
30+
31+
assertNotNull(config);
32+
assertEquals("src/test/resources/config.yml", config.getFilePath());
33+
assertEquals(ResourceLoader.load("config.yml"), config.getContent());
34+
35+
}
36+
37+
@Test void testGetConfigFileFromGitLab() {
38+
ConfigService service = new ConfigService();
39+
service.configFile = "schema/config.yml";
40+
service.webHooksFile = "src/test/resources/webhooks.yaml";
41+
service.marshaller = new JsonMarshaller();
42+
43+
FileService fileService = new FileService();
44+
fileService.gitLabService = new MockGitLabService();
45+
service.fileService = fileService;
46+
47+
service.onStart(new StartupEvent());
48+
File config = service.getConfigFile();
49+
50+
assertNotNull(config);
51+
assertEquals("schema/config.yml", config.getFilePath());
52+
assertEquals(ResourceLoader.load("config.yml"), config.getContent());
53+
54+
}
55+
2056
@Test void testGetConfigFileNotFound() {
2157
ConfigService service = new ConfigService();
2258
service.configFile = "schema/notfound.yaml";
@@ -34,6 +70,7 @@ class ConfigServiceTest {
3470

3571
@Test void testGetHookConfigPreLoaded() {
3672
ConfigService service = new ConfigService();
73+
service.configFile = "src/test/resources/config.yml";
3774
service.webHooksFile = "src/test/resources/webhooks.yaml";
3875
service.marshaller = new JsonMarshaller();
3976

@@ -48,6 +85,7 @@ class ConfigServiceTest {
4885

4986
@Test void testGetHookConfigLabNotEmpty() {
5087
ConfigService service = new ConfigService();
88+
service.configFile = "src/test/resources/config.yml";
5189
service.webHooksFile = "/schema/webhooks.yaml";
5290
service.marshaller = new JsonMarshaller();
5391

@@ -65,6 +103,7 @@ class ConfigServiceTest {
65103

66104
@Test void testGetHookConfigLabsEmpty() {
67105
ConfigService service = new ConfigService();
106+
service.configFile = "src/test/resources/config.yml";
68107
service.webHooksFile = "schema/notfound.yaml";
69108
service.marshaller = new JsonMarshaller();
70109

0 commit comments

Comments
 (0)