From f3635b9d5278bbd0fe0a939dca1af291f7cecf4d Mon Sep 17 00:00:00 2001 From: Igor Vinokur Date: Tue, 23 Jul 2024 12:35:52 +0300 Subject: [PATCH] return devfile v2 as a default devfile (#702) --- ...horizingFactoryParametersResolverTest.java | 18 +++++--------- ...itbucketFactoryParametersResolverTest.java | 16 +++++-------- .../GithubFactoryParametersResolverTest.java | 24 +++++-------------- .../GitlabFactoryParametersResolverTest.java | 17 +++++-------- .../che/api/factory/shared/Constants.java | 6 ++++- .../server/BaseFactoryParameterResolver.java | 10 ++++++-- .../BaseFactoryParameterResolverTest.java | 21 +++++++++++++++- 7 files changed, 57 insertions(+), 55 deletions(-) diff --git a/wsmaster/che-core-api-factory-bitbucket-server/src/test/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketServerAuthorizingFactoryParametersResolverTest.java b/wsmaster/che-core-api-factory-bitbucket-server/src/test/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketServerAuthorizingFactoryParametersResolverTest.java index 86e5d9a8b3a..2d2657227e5 100644 --- a/wsmaster/che-core-api-factory-bitbucket-server/src/test/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketServerAuthorizingFactoryParametersResolverTest.java +++ b/wsmaster/che-core-api-factory-bitbucket-server/src/test/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketServerAuthorizingFactoryParametersResolverTest.java @@ -27,7 +27,6 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; import com.google.common.collect.ImmutableMap; @@ -42,6 +41,7 @@ import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder; import org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto; import org.eclipse.che.api.factory.shared.dto.FactoryDto; +import org.eclipse.che.api.factory.shared.dto.ScmInfoDto; import org.eclipse.che.api.workspace.server.devfile.URLFetcher; import org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto; import org.eclipse.che.api.workspace.shared.dto.devfile.MetadataDto; @@ -112,23 +112,17 @@ public void shouldGenerateDevfileForFactoryWithNoDevfileOrJson() throws Exceptio String bitbucketUrl = "http://bitbucket.2mcl.com/scm/test/repo.git"; - FactoryDto computedFactory = generateDevfileFactory(); - - when(urlFactoryBuilder.buildDefaultDevfile(any())).thenReturn(computedFactory.getDevfile()); - when(urlFactoryBuilder.createFactoryFromDevfile( any(RemoteFactoryUrl.class), any(), anyMap(), anyBoolean())) .thenReturn(Optional.empty()); Map params = ImmutableMap.of(URL_PARAMETER_NAME, bitbucketUrl); // when - FactoryDto factory = - (FactoryDto) bitbucketServerFactoryParametersResolver.createFactory(params); + FactoryDevfileV2Dto factory = + (FactoryDevfileV2Dto) bitbucketServerFactoryParametersResolver.createFactory(params); // then - verify(urlFactoryBuilder).buildDefaultDevfile(eq("repo")); - assertEquals(factory, computedFactory); - SourceDto source = factory.getDevfile().getProjects().get(0).getSource(); - assertEquals(source.getLocation(), bitbucketUrl); - assertNull(source.getBranch()); + ScmInfoDto scmInfo = factory.getScmInfo(); + assertEquals(scmInfo.getRepositoryUrl(), bitbucketUrl); + assertEquals(scmInfo.getBranch(), null); } @Test diff --git a/wsmaster/che-core-api-factory-bitbucket/src/test/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketFactoryParametersResolverTest.java b/wsmaster/che-core-api-factory-bitbucket/src/test/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketFactoryParametersResolverTest.java index 9137d32823d..48b79828082 100644 --- a/wsmaster/che-core-api-factory-bitbucket/src/test/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketFactoryParametersResolverTest.java +++ b/wsmaster/che-core-api-factory-bitbucket/src/test/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketFactoryParametersResolverTest.java @@ -44,6 +44,7 @@ import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder; import org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto; import org.eclipse.che.api.factory.shared.dto.FactoryDto; +import org.eclipse.che.api.factory.shared.dto.ScmInfoDto; import org.eclipse.che.api.workspace.server.devfile.FileContentProvider; import org.eclipse.che.api.workspace.server.devfile.URLFetcher; import org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto; @@ -145,22 +146,17 @@ public void shouldGenerateDevfileForFactoryWithNoDevfile() throws Exception { String bitbucketUrl = "https://bitbucket.org/eclipse/che"; - FactoryDto computedFactory = generateDevfileFactory(); - - when(urlFactoryBuilder.buildDefaultDevfile(any())).thenReturn(computedFactory.getDevfile()); - when(urlFactoryBuilder.createFactoryFromDevfile( any(RemoteFactoryUrl.class), any(), anyMap(), anyBoolean())) .thenReturn(Optional.empty()); Map params = ImmutableMap.of(URL_PARAMETER_NAME, bitbucketUrl); // when - FactoryDto factory = (FactoryDto) bitbucketFactoryParametersResolver.createFactory(params); + FactoryDevfileV2Dto factory = + (FactoryDevfileV2Dto) bitbucketFactoryParametersResolver.createFactory(params); // then - verify(urlFactoryBuilder).buildDefaultDevfile(eq("che")); - assertEquals(factory, computedFactory); - SourceDto source = factory.getDevfile().getProjects().get(0).getSource(); - assertEquals(source.getLocation(), bitbucketUrl + ".git"); - assertEquals(source.getBranch(), null); + ScmInfoDto scmInfo = factory.getScmInfo(); + assertEquals(scmInfo.getRepositoryUrl(), bitbucketUrl + ".git"); + assertEquals(scmInfo.getBranch(), null); } @Test diff --git a/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubFactoryParametersResolverTest.java b/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubFactoryParametersResolverTest.java index 6f8d2d5fb5d..e7374efca6d 100644 --- a/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubFactoryParametersResolverTest.java +++ b/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubFactoryParametersResolverTest.java @@ -46,6 +46,7 @@ import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder; import org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto; import org.eclipse.che.api.factory.shared.dto.FactoryDto; +import org.eclipse.che.api.factory.shared.dto.ScmInfoDto; import org.eclipse.che.api.workspace.server.devfile.FileContentProvider; import org.eclipse.che.api.workspace.server.devfile.URLFetcher; import org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto; @@ -155,34 +156,23 @@ public void shouldGenerateDevfileForFactoryWithNoDevfile() throws Exception { String githubUrl = "https://github.com/eclipse/che"; - FactoryDto computedFactory = generateDevfileFactory(); - - when(urlFactoryBuilder.buildDefaultDevfile(any())).thenReturn(computedFactory.getDevfile()); - - when(urlFactoryBuilder.createFactoryFromDevfile( - any(RemoteFactoryUrl.class), any(), anyMap(), anyBoolean())) - .thenReturn(Optional.empty()); - when(githubApiClient.isConnected(eq("https://github.com"))).thenReturn(true); when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any())) .thenReturn(new GithubCommit().withSha("test-sha")); Map params = ImmutableMap.of(URL_PARAMETER_NAME, githubUrl); // when - FactoryDto factory = (FactoryDto) abstractGithubFactoryParametersResolver.createFactory(params); + FactoryDevfileV2Dto factory = + (FactoryDevfileV2Dto) abstractGithubFactoryParametersResolver.createFactory(params); // then - verify(urlFactoryBuilder).buildDefaultDevfile(eq("che")); - assertEquals(factory, computedFactory); - SourceDto source = factory.getDevfile().getProjects().get(0).getSource(); - assertEquals(source.getLocation(), githubUrl + ".git"); - assertEquals(source.getBranch(), null); + ScmInfoDto scmInfo = factory.getScmInfo(); + assertEquals(scmInfo.getRepositoryUrl(), githubUrl + ".git"); + assertEquals(scmInfo.getBranch(), null); } @Test public void shouldSkipAuthenticationWhenAccessDenied() throws Exception { // given - when(urlFactoryBuilder.buildDefaultDevfile(any())) - .thenReturn(generateDevfileFactory().getDevfile()); when(githubApiClient.isConnected(eq("https://github.com"))).thenReturn(true); when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any())) .thenReturn(new GithubCommit().withSha("test-sha")); @@ -204,8 +194,6 @@ public void shouldSkipAuthenticationWhenAccessDenied() throws Exception { @Test public void shouldNotSkipAuthenticationWhenNoErrorParameterPassed() throws Exception { // given - when(urlFactoryBuilder.buildDefaultDevfile(any())) - .thenReturn(generateDevfileFactory().getDevfile()); when(githubApiClient.isConnected(eq("https://github.com"))).thenReturn(true); when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any())) .thenReturn(new GithubCommit().withSha("test-sha")); diff --git a/wsmaster/che-core-api-factory-gitlab/src/test/java/org/eclipse/che/api/factory/server/gitlab/GitlabFactoryParametersResolverTest.java b/wsmaster/che-core-api-factory-gitlab/src/test/java/org/eclipse/che/api/factory/server/gitlab/GitlabFactoryParametersResolverTest.java index e4afa847d52..af93f29228d 100644 --- a/wsmaster/che-core-api-factory-gitlab/src/test/java/org/eclipse/che/api/factory/server/gitlab/GitlabFactoryParametersResolverTest.java +++ b/wsmaster/che-core-api-factory-gitlab/src/test/java/org/eclipse/che/api/factory/server/gitlab/GitlabFactoryParametersResolverTest.java @@ -27,7 +27,6 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; import com.google.common.collect.ImmutableMap; @@ -42,6 +41,7 @@ import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder; import org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto; import org.eclipse.che.api.factory.shared.dto.FactoryDto; +import org.eclipse.che.api.factory.shared.dto.ScmInfoDto; import org.eclipse.che.api.workspace.server.devfile.URLFetcher; import org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto; import org.eclipse.che.api.workspace.shared.dto.devfile.MetadataDto; @@ -114,22 +114,17 @@ public void shouldGenerateDevfileForFactoryWithNoDevfileOrJson() throws Exceptio String gitlabUrl = "http://gitlab.2mcl.com/test/proj/repo.git"; - FactoryDto computedFactory = generateDevfileFactory(); - - when(urlFactoryBuilder.buildDefaultDevfile(any())).thenReturn(computedFactory.getDevfile()); - when(urlFactoryBuilder.createFactoryFromDevfile( any(RemoteFactoryUrl.class), any(), anyMap(), anyBoolean())) .thenReturn(Optional.empty()); Map params = ImmutableMap.of(URL_PARAMETER_NAME, gitlabUrl); // when - FactoryDto factory = (FactoryDto) gitlabFactoryParametersResolver.createFactory(params); + FactoryDevfileV2Dto factory = + (FactoryDevfileV2Dto) gitlabFactoryParametersResolver.createFactory(params); // then - verify(urlFactoryBuilder).buildDefaultDevfile(eq("repo")); - assertEquals(factory, computedFactory); - SourceDto source = factory.getDevfile().getProjects().get(0).getSource(); - assertEquals(source.getLocation(), gitlabUrl); - assertNull(source.getBranch()); + ScmInfoDto scmInfo = factory.getScmInfo(); + assertEquals(scmInfo.getRepositoryUrl(), gitlabUrl); + assertEquals(scmInfo.getBranch(), null); } @Test diff --git a/wsmaster/che-core-api-factory-shared/src/main/java/org/eclipse/che/api/factory/shared/Constants.java b/wsmaster/che-core-api-factory-shared/src/main/java/org/eclipse/che/api/factory/shared/Constants.java index db111911877..16601b34758 100644 --- a/wsmaster/che-core-api-factory-shared/src/main/java/org/eclipse/che/api/factory/shared/Constants.java +++ b/wsmaster/che-core-api-factory-shared/src/main/java/org/eclipse/che/api/factory/shared/Constants.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2018 Red Hat, Inc. + * Copyright (c) 2012-2024 Red Hat, Inc. * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 * which is available at https://www.eclipse.org/legal/epl-2.0/ @@ -11,6 +11,8 @@ */ package org.eclipse.che.api.factory.shared; +import java.util.Map; + /** * Constants for Factory API. * @@ -29,5 +31,7 @@ public final class Constants { // url factory parameter names public static final String URL_PARAMETER_NAME = "url"; + public static final Map DEFAULT_DEVFILE = Map.of("schemaVersion", "2.3.0"); + private Constants() {} } diff --git a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/BaseFactoryParameterResolver.java b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/BaseFactoryParameterResolver.java index b5b80980449..08efe28e43e 100644 --- a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/BaseFactoryParameterResolver.java +++ b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/BaseFactoryParameterResolver.java @@ -13,6 +13,7 @@ import static java.util.stream.Collectors.toMap; import static org.eclipse.che.api.factory.shared.Constants.CURRENT_VERSION; +import static org.eclipse.che.api.factory.shared.Constants.DEFAULT_DEVFILE; import static org.eclipse.che.dto.server.DtoFactory.newDto; import java.util.Collections; @@ -24,7 +25,7 @@ import org.eclipse.che.api.factory.server.scm.AuthorisationRequestManager; 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.FactoryDto; +import org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto; import org.eclipse.che.api.factory.shared.dto.FactoryMetaDto; import org.eclipse.che.api.factory.shared.dto.FactoryVisitor; import org.eclipse.che.api.workspace.server.devfile.FileContentProvider; @@ -60,7 +61,12 @@ protected FactoryMetaDto createFactory( contentProvider, extractOverrideParams(factoryParameters), getSkipAuthorisation(factoryParameters)) - .orElseGet(() -> newDto(FactoryDto.class).withV(CURRENT_VERSION).withSource("repo")) + .orElseGet( + () -> + newDto(FactoryDevfileV2Dto.class) + .withDevfile(DEFAULT_DEVFILE) + .withV(CURRENT_VERSION) + .withSource("repo")) .acceptVisitor(factoryVisitor); } diff --git a/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/BaseFactoryParameterResolverTest.java b/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/BaseFactoryParameterResolverTest.java index 105de8e5f3d..e9293f8617b 100644 --- a/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/BaseFactoryParameterResolverTest.java +++ b/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/BaseFactoryParameterResolverTest.java @@ -12,14 +12,20 @@ package org.eclipse.che.api.factory.server; import static java.util.Collections.emptyMap; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; import java.util.Map; import org.eclipse.che.api.factory.server.scm.AuthorisationRequestManager; +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.FactoryDevfileV2Dto; +import org.eclipse.che.api.factory.shared.dto.FactoryVisitor; +import org.eclipse.che.api.workspace.server.devfile.FileContentProvider; import org.mockito.Mock; import org.mockito.testng.MockitoTestNGListener; import org.testng.annotations.BeforeMethod; @@ -31,7 +37,9 @@ public class BaseFactoryParameterResolverTest { @Mock private AuthorisationRequestManager authorisationRequestManager; @Mock private URLFactoryBuilder urlFactoryBuilder; - + @Mock private RemoteFactoryUrl remoteFactoryUrl; + @Mock private FactoryVisitor factoryVisitor; + @Mock private FileContentProvider contentProvider; private static final String PROVIDER_NAME = "test"; private BaseFactoryParameterResolver baseFactoryParameterResolver; @@ -73,4 +81,15 @@ public void shouldReturnTrueOnGetSkipAuthorisationFromFactoryParams() { // then assertTrue(result); } + + @Test + public void shouldReturnDevfileV2() throws Exception { + // given + when(authorisationRequestManager.isStored(eq(PROVIDER_NAME))).thenReturn(false); + // when + baseFactoryParameterResolver.createFactory( + emptyMap(), remoteFactoryUrl, factoryVisitor, contentProvider); + // then + verify(factoryVisitor).visit(any(FactoryDevfileV2Dto.class)); + } }