From 9d43803100dab5629aa440c4f29ec748a23740be Mon Sep 17 00:00:00 2001 From: Igor Vinokur Date: Wed, 6 Sep 2023 14:15:11 +0300 Subject: [PATCH] Simplify the Raw devfile filename check (#548) Do not check the Raw devfile file name but check if the url ends with .yaml before using the Raw devfile url resolver. --- ...RawDevfileUrlFactoryParameterResolver.java | 6 +--- ...evfileUrlFactoryParameterResolverTest.java | 28 +++++++++---------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/RawDevfileUrlFactoryParameterResolver.java b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/RawDevfileUrlFactoryParameterResolver.java index 9f65640a21..2c87847d6a 100644 --- a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/RawDevfileUrlFactoryParameterResolver.java +++ b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/RawDevfileUrlFactoryParameterResolver.java @@ -32,7 +32,6 @@ import org.eclipse.che.api.core.ApiException; import org.eclipse.che.api.core.BadRequestException; import org.eclipse.che.api.factory.server.urlfactory.DefaultFactoryUrl; -import org.eclipse.che.api.factory.server.urlfactory.DevfileFilenamesProvider; import org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl; import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder; import org.eclipse.che.api.factory.shared.dto.FactoryMetaDto; @@ -52,7 +51,6 @@ public class RawDevfileUrlFactoryParameterResolver implements FactoryParametersR protected final URLFactoryBuilder urlFactoryBuilder; protected final URLFetcher urlFetcher; - @Inject private DevfileFilenamesProvider devfileFilenamesProvider; @Inject public RawDevfileUrlFactoryParameterResolver( @@ -71,9 +69,7 @@ public RawDevfileUrlFactoryParameterResolver( @Override public boolean accept(Map factoryParameters) { String url = factoryParameters.get(URL_PARAMETER_NAME); - return !isNullOrEmpty(url) - && devfileFilenamesProvider.getConfiguredDevfileFilenames().stream() - .anyMatch(url::endsWith); + return !isNullOrEmpty(url) && (url.endsWith(".yaml") || url.endsWith(".yml")); } /** diff --git a/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/RawDevfileUrlFactoryParameterResolverTest.java b/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/RawDevfileUrlFactoryParameterResolverTest.java index 800353220b..12bed43bc5 100644 --- a/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/RawDevfileUrlFactoryParameterResolverTest.java +++ b/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/RawDevfileUrlFactoryParameterResolverTest.java @@ -28,12 +28,10 @@ import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertTrue; -import java.lang.reflect.Field; import java.util.Collections; import java.util.HashMap; import java.util.Map; import org.eclipse.che.api.core.BadRequestException; -import org.eclipse.che.api.factory.server.urlfactory.DevfileFilenamesProvider; import org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl; import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder; import org.eclipse.che.api.workspace.server.devfile.DevfileParser; @@ -67,8 +65,6 @@ public class RawDevfileUrlFactoryParameterResolverTest { + " reference: ../localfile\n"; @Mock private URLFetcher urlFetcher; - private final DevfileFilenamesProvider devfileFilenamesProvider = - new DevfileFilenamesProvider("devfile.yaml, .devfile.yaml"); @InjectMocks private RawDevfileUrlFactoryParameterResolver rawDevfileUrlFactoryParameterResolver; @@ -167,16 +163,7 @@ public void shouldThrowExceptionOnInvalidURL(String url, String message) throws } @Test(dataProvider = "devfileNames") - public void shouldAcceptRawDevfileUrl(String devfileName) - throws NoSuchFieldException, IllegalAccessException { - // given - Field field = - rawDevfileUrlFactoryParameterResolver - .getClass() - .getDeclaredField("devfileFilenamesProvider"); - field.setAccessible(true); - field.set(rawDevfileUrlFactoryParameterResolver, devfileFilenamesProvider); - + public void shouldAcceptRawDevfileUrl(String devfileName) { // when boolean result = rawDevfileUrlFactoryParameterResolver.accept( @@ -186,6 +173,17 @@ public void shouldAcceptRawDevfileUrl(String devfileName) assertTrue(result); } + @Test + public void shouldNotAcceptRawDevfileUrl() { + // when + boolean result = + rawDevfileUrlFactoryParameterResolver.accept( + Collections.singletonMap(URL_PARAMETER_NAME, "https://host/user/repo.git")); + + // then + assertFalse(result); + } + @DataProvider(name = "invalidURLsProvider") private Object[][] invalidUrlsProvider() { return new Object[][] { @@ -200,6 +198,6 @@ private Object[][] invalidUrlsProvider() { @DataProvider(name = "devfileNames") private Object[] devfileNames() { - return new String[] {"devfile.yaml", ".devfile.yaml"}; + return new String[] {"devfile.yaml", ".devfile.yaml", "any-name.yaml", "any-name.yml"}; } }