Skip to content

Commit

Permalink
fix: allow to override devfileFilename
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitf committed Oct 19, 2021
1 parent fc5a7c3 commit f2bceb8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class URLFactoryBuilder {

private static final Logger LOG = LoggerFactory.getLogger(URLFactoryBuilder.class);

public static final String DEVFILE_NAME = "devfileName";
public static final String DEVFILE_FILENAME = "devfileFilename";

private final String defaultCheEditor;
private final String defaultChePlugins;
Expand Down Expand Up @@ -106,8 +106,8 @@ public Optional<FactoryMetaDto> createFactoryFromDevfile(
String devfileYamlContent;

// Apply the new devfile name to look for
if (overrideProperties.containsKey(DEVFILE_NAME)) {
remoteFactoryUrl.setDevfileFilename(overrideProperties.get(DEVFILE_NAME));
if (overrideProperties.containsKey(DEVFILE_FILENAME)) {
remoteFactoryUrl.setDevfileFilename(overrideProperties.get(DEVFILE_FILENAME));
}

for (DevfileLocation location : remoteFactoryUrl.devfileFileLocations()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ public void setDevfileFilename(String devfileName) {
};

String pathToDevfile = "my-custom-devfile-path.yaml";
Map<String, String> propertiesMap = singletonMap(URLFactoryBuilder.DEVFILE_NAME, pathToDevfile);
Map<String, String> propertiesMap =
singletonMap(URLFactoryBuilder.DEVFILE_FILENAME, pathToDevfile);
FactoryMetaDto factory =
urlFactoryBuilder
.createFactoryFromDevfile(githubLikeRemoteUrl, s -> myLocation + ".list", propertiesMap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,19 @@
public class OverridePropertiesApplier {

private final List<String> allowedFirstSegments =
asList("apiVersion", "metadata", "projects", "attributes");
asList("apiVersion", "metadata", "projects", "attributes", "devfileFilename");

private final List<String> skipJsonSegments = asList("devfileFilename");

public JsonNode applyPropertiesOverride(
JsonNode devfileNode, Map<String, String> overrideProperties)
throws OverrideParameterException {
for (Map.Entry<String, String> entry : overrideProperties.entrySet()) {

// skip some segment
if (skipJsonSegments.contains(entry.getKey())) {
continue;
}
String[] pathSegments = parseSegments(entry.getKey());
if (pathSegments.length < 1) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.eclipse.che.api.workspace.server.devfile;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -44,6 +45,27 @@ public void shouldOverrideExistingPropertiesInDevfile() throws Exception {
assertEquals(result.get("metadata").get("generateName").textValue(), "go");
}

@Test
public void shouldNotOverrideDevfileFilenameInDevfile() throws Exception {
String json =
"{"
+ "\"apiVersion\": \"2.0.0\","
+ "\"metadata\": {"
+ " \"generateName\": \"python\""
+ " }"
+ "}";
Map<String, String> overrides = new HashMap<>();
overrides.put("devfileFilename", "devfile.v2");
overrides.put("metadata.generateName", "go");
// when
JsonNode result = applier.applyPropertiesOverride(jsonMapper.readTree(json), overrides);

// then
assertEquals(result.get("metadata").get("generateName").textValue(), "go");
// this parameter is accepted but not added into the JSON
assertNull(result.get("devfileFilename"));
}

@Test
public void shouldCreateUnExistingOverridePropertiesInDevfile() throws Exception {
String json = "{" + "\"apiVersion\": \"1.0.0\"" + "}";
Expand Down Expand Up @@ -197,7 +219,7 @@ public void shouldThrowExceptionIfOverrideReferenceIsJustWithArray() throws Exce
@Test(
expectedExceptions = OverrideParameterException.class,
expectedExceptionsMessageRegExp =
"Override path 'commands.run.foo.bar' starts with an unsupported field pointer. Supported fields are \\{\"apiVersion\",\"metadata\",\"projects\"\\,\"attributes\"\\}.")
"Override path 'commands.run.foo.bar' starts with an unsupported field pointer. Supported fields are \\{\"apiVersion\",\"metadata\",\"projects\"\\,\"attributes\"\\,\"devfileFilename\"\\}.")
public void shouldThrowExceptionIfOverrideReferenceUsesUnsupportedField() throws Exception {
String json =
"{"
Expand Down

0 comments on commit f2bceb8

Please sign in to comment.