fixup! Support raw devfile urls without yaml extension

pull/683/head
ivinokur 2024-05-13 15:12:36 +03:00
parent ba0faa9784
commit 58cc77f09c
2 changed files with 14 additions and 13 deletions

View File

@ -16,6 +16,7 @@ import static java.lang.String.format;
import static org.eclipse.che.api.factory.server.FactoryResolverPriority.HIGHEST; import static org.eclipse.che.api.factory.server.FactoryResolverPriority.HIGHEST;
import static org.eclipse.che.api.factory.shared.Constants.URL_PARAMETER_NAME; import static org.eclipse.che.api.factory.shared.Constants.URL_PARAMETER_NAME;
import com.fasterxml.jackson.databind.JsonNode;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@ -69,18 +70,16 @@ public class RawDevfileUrlFactoryParameterResolver extends BaseFactoryParameterR
@Override @Override
public boolean accept(Map<String, String> factoryParameters) { public boolean accept(Map<String, String> factoryParameters) {
String url = factoryParameters.get(URL_PARAMETER_NAME); String url = factoryParameters.get(URL_PARAMETER_NAME);
return !isNullOrEmpty(url) && (PATTERN.matcher(url).matches() || containsDevfile(url)); return !isNullOrEmpty(url) && (PATTERN.matcher(url).matches() || containsYaml(url));
} }
private boolean containsDevfile(String requestURL) { private boolean containsYaml(String requestURL) {
try { try {
String fetch = urlFetcher.fetch(requestURL); String fetch = urlFetcher.fetch(requestURL);
devfileParser.parseYaml(fetch); JsonNode parsedYaml = devfileParser.parseYamlRaw(fetch);
return true; return !parsedYaml.isEmpty();
} catch (IOException e) { } catch (IOException | DevfileFormatException e) {
return false; return false;
} catch (DevfileFormatException e) {
return !e.getMessage().startsWith("Cannot construct instance of");
} }
} }

View File

@ -30,6 +30,7 @@ import static org.testng.Assert.fail;
import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue; import static org.testng.AssertJUnit.assertTrue;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -40,7 +41,6 @@ import org.eclipse.che.api.workspace.server.devfile.DevfileParser;
import org.eclipse.che.api.workspace.server.devfile.DevfileVersionDetector; import org.eclipse.che.api.workspace.server.devfile.DevfileVersionDetector;
import org.eclipse.che.api.workspace.server.devfile.URLFetcher; import org.eclipse.che.api.workspace.server.devfile.URLFetcher;
import org.eclipse.che.api.workspace.server.devfile.URLFileContentProvider; import org.eclipse.che.api.workspace.server.devfile.URLFileContentProvider;
import org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException;
import org.eclipse.che.api.workspace.server.devfile.validator.ComponentIntegrityValidator; import org.eclipse.che.api.workspace.server.devfile.validator.ComponentIntegrityValidator;
import org.eclipse.che.api.workspace.server.devfile.validator.ComponentIntegrityValidator.NoopComponentIntegrityValidator; import org.eclipse.che.api.workspace.server.devfile.validator.ComponentIntegrityValidator.NoopComponentIntegrityValidator;
import org.eclipse.che.api.workspace.server.devfile.validator.DevfileIntegrityValidator; import org.eclipse.che.api.workspace.server.devfile.validator.DevfileIntegrityValidator;
@ -170,12 +170,13 @@ public class RawDevfileUrlFactoryParameterResolverTest {
} }
@Test @Test
public void shouldAcceptRawDevfileUrlWithUnrecognizedDevfile() throws Exception { public void shouldAcceptRawDevfileUrlWithYaml() throws Exception {
// given // given
JsonNode jsonNode = mock(JsonNode.class);
String url = "https://host/path/devfile"; String url = "https://host/path/devfile";
when(urlFetcher.fetch(eq(url))).thenReturn(DEVFILE); when(urlFetcher.fetch(eq(url))).thenReturn(DEVFILE);
when(devfileParser.parseYaml(eq(DEVFILE))) when(devfileParser.parseYamlRaw(eq(DEVFILE))).thenReturn(jsonNode);
.thenThrow(new DevfileFormatException("Unrecognized field \"schemaVersion\"")); when(jsonNode.isEmpty()).thenReturn(false);
// when // when
boolean result = boolean result =
@ -188,10 +189,11 @@ public class RawDevfileUrlFactoryParameterResolverTest {
@Test @Test
public void shouldNotAcceptPublicGitRepositoryUrl() throws Exception { public void shouldNotAcceptPublicGitRepositoryUrl() throws Exception {
// given // given
JsonNode jsonNode = mock(JsonNode.class);
String gitRepositoryUrl = "https://host/user/repo.git"; String gitRepositoryUrl = "https://host/user/repo.git";
when(urlFetcher.fetch(eq(gitRepositoryUrl))).thenReturn("unsupported content"); when(urlFetcher.fetch(eq(gitRepositoryUrl))).thenReturn("unsupported content");
when(devfileParser.parseYaml(eq("unsupported content"))) when(devfileParser.parseYamlRaw(eq("unsupported content"))).thenReturn(jsonNode);
.thenThrow(new DevfileFormatException("Cannot construct instance of ...")); when(jsonNode.isEmpty()).thenReturn(true);
// when // when
boolean result = boolean result =