Provide scm info in factory v2

pull/5/head
Max Shaposhnik 2021-05-21 12:27:02 +03:00
parent 578e36ecf3
commit bbcebfbff7
20 changed files with 438 additions and 21 deletions

View File

@ -0,0 +1,21 @@
/*
* Copyright (c) 2012-2018 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/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.api.core.model.factory;
/** Defines the contract for the SCM information. */
public interface ScmInfo {
String getScmProviderName();
String getRepositoryUrl();
String getBranch();
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2012-2021 Red Hat, Inc.
Copyright (c) 2012-2018 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/
@ -82,6 +82,10 @@
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-factory-shared</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-model</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-workspace</artifactId>

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2018 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/
@ -25,9 +25,11 @@ import org.eclipse.che.api.factory.server.DefaultFactoryParameterResolver;
import org.eclipse.che.api.factory.server.scm.GitCredentialManager;
import org.eclipse.che.api.factory.server.scm.PersonalAccessTokenManager;
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.FactoryMetaDto;
import org.eclipse.che.api.factory.shared.dto.FactoryVisitor;
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.ProjectDto;
@ -112,6 +114,18 @@ public class BitbucketServerAuthorizingFactoryParametersResolver
this.bitbucketUrl = bitbucketUrl;
}
@Override
public FactoryDevfileV2Dto visit(FactoryDevfileV2Dto factoryDto) {
ScmInfoDto scmInfo =
newDto(ScmInfoDto.class)
.withScmProviderName(bitbucketUrl.getProviderName())
.withRepositoryUrl(bitbucketUrl.repositoryLocation());
if (bitbucketUrl.getBranch() != null) {
scmInfo.withBranch(bitbucketUrl.getBranch());
}
return factoryDto.withScmInfo(scmInfo);
}
@Override
public FactoryDto visit(FactoryDto factory) {
if (factory.getDevfile() == null) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2018 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/
@ -22,6 +22,8 @@ import org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl;
/** Representation of a bitbucket URL, allowing to get details from it. */
public class BitbucketUrl implements RemoteFactoryUrl {
private final String NAME = "bitbucket";
/** Hostname of bitbucket URL */
private String hostName;
@ -43,6 +45,11 @@ public class BitbucketUrl implements RemoteFactoryUrl {
*/
protected BitbucketUrl() {}
@Override
public String getProviderName() {
return NAME;
}
/**
* Gets hostname of this bitbucket url
*

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2018 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/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2018 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/
@ -30,11 +30,13 @@ import static org.testng.Assert.assertTrue;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import java.util.Optional;
import org.eclipse.che.api.core.model.factory.ScmInfo;
import org.eclipse.che.api.factory.server.scm.GitCredentialManager;
import org.eclipse.che.api.factory.server.scm.PersonalAccessTokenManager;
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.FactoryDevfileV2Dto;
import org.eclipse.che.api.factory.shared.dto.FactoryDto;
import org.eclipse.che.api.workspace.server.devfile.URLFetcher;
import org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto;
@ -139,6 +141,28 @@ public class BitbucketServerAuthorizingFactoryParametersResolverTest {
assertEquals(source.getBranch(), "foobar");
}
@Test
public void shouldSetScmInfoIntoDevfileV2() throws Exception {
String bitbucketUrl = "http://bitbucket.2mcl.com/scm/test/repo.git?at=foobar";
FactoryDevfileV2Dto computedFactory = generateDevfileV2Factory();
when(urlFactoryBuilder.createFactoryFromDevfile(any(RemoteFactoryUrl.class), any(), anyMap()))
.thenReturn(Optional.of(computedFactory));
Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, bitbucketUrl);
// when
FactoryDevfileV2Dto factory =
(FactoryDevfileV2Dto) bitbucketServerFactoryParametersResolver.createFactory(params);
// then
ScmInfo scmInfo = factory.getScmInfo();
assertNotNull(scmInfo);
assertEquals(scmInfo.getScmProviderName(), "bitbucket");
assertEquals(scmInfo.getRepositoryUrl(), "http://bitbucket.2mcl.com/scm/test/repo.git");
assertEquals(scmInfo.getBranch(), "foobar");
}
private FactoryDto generateDevfileFactory() {
return newDto(FactoryDto.class)
.withV(CURRENT_VERSION)
@ -148,4 +172,11 @@ public class BitbucketServerAuthorizingFactoryParametersResolverTest {
.withApiVersion(CURRENT_API_VERSION)
.withMetadata(newDto(MetadataDto.class).withName("che")));
}
private FactoryDevfileV2Dto generateDevfileV2Factory() {
return newDto(FactoryDevfileV2Dto.class)
.withV(CURRENT_VERSION)
.withSource("repo")
.withDevfile(Map.of("schemaVersion", "2.0.0"));
}
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2012-2021 Red Hat, Inc.
Copyright (c) 2012-2018 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/
@ -54,6 +54,10 @@
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-factory-shared</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-model</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-workspace</artifactId>

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2018 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/
@ -24,9 +24,11 @@ import org.eclipse.che.api.core.BadRequestException;
import org.eclipse.che.api.factory.server.DefaultFactoryParameterResolver;
import org.eclipse.che.api.factory.server.urlfactory.ProjectConfigDtoMerger;
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.FactoryMetaDto;
import org.eclipse.che.api.factory.shared.dto.FactoryVisitor;
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.ProjectConfigDto;
import org.eclipse.che.api.workspace.shared.dto.devfile.ProjectDto;
@ -109,6 +111,18 @@ public class GithubFactoryParametersResolver extends DefaultFactoryParameterReso
this.githubUrl = githubUrl;
}
@Override
public FactoryDevfileV2Dto visit(FactoryDevfileV2Dto factoryDto) {
ScmInfoDto scmInfo =
newDto(ScmInfoDto.class)
.withScmProviderName(githubUrl.getProviderName())
.withRepositoryUrl(githubUrl.repositoryLocation());
if (githubUrl.getBranch() != null) {
scmInfo.withBranch(githubUrl.getBranch());
}
return factoryDto.withScmInfo(scmInfo);
}
@Override
public FactoryDto visit(FactoryDto factory) {
if (factory.getWorkspace() != null) {
@ -134,8 +148,7 @@ public class GithubFactoryParametersResolver extends DefaultFactoryParameterReso
.withName(githubUrl.getRepository()),
project -> {
final String location = project.getSource().getLocation();
if (location.equals(githubUrl.repositoryLocation())
|| location.equals(githubUrl.repositoryLocation() + ".git")) {
if (location.equals(githubUrl.repositoryLocation())) {
project.getSource().setBranch(githubUrl.getBranch());
}
});

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2018 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/
@ -31,6 +31,8 @@ import org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl;
*/
public class GithubUrl implements RemoteFactoryUrl {
private final String NAME = "github";
private static final String HOSTNAME = "https://github.com";
/** Username part of github URL */
@ -54,6 +56,11 @@ public class GithubUrl implements RemoteFactoryUrl {
*/
protected GithubUrl() {}
@Override
public String getProviderName() {
return NAME;
}
/**
* Gets username of this github url
*
@ -173,6 +180,6 @@ public class GithubUrl implements RemoteFactoryUrl {
* @return location of the repository.
*/
protected String repositoryLocation() {
return HOSTNAME + "/" + this.username + "/" + this.repository;
return HOSTNAME + "/" + this.username + "/" + this.repository + ".git";
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2018 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/
@ -32,10 +32,12 @@ import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import org.eclipse.che.api.core.model.factory.ScmInfo;
import org.eclipse.che.api.factory.server.urlfactory.DevfileFilenamesProvider;
import org.eclipse.che.api.factory.server.urlfactory.ProjectConfigDtoMerger;
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.FactoryDto;
import org.eclipse.che.api.workspace.server.devfile.FileContentProvider;
import org.eclipse.che.api.workspace.server.devfile.URLFetcher;
@ -225,6 +227,28 @@ public class GithubFactoryParametersResolverTest {
assertEquals(source.getBranch(), "foobranch");
}
@Test
public void shouldSetScmInfoIntoDevfileV2() throws Exception {
String githubUrl = "https://github.com/eclipse/che/tree/foobar";
FactoryDevfileV2Dto computedFactory = generateDevfileV2Factory();
when(urlFactoryBuilder.createFactoryFromDevfile(any(RemoteFactoryUrl.class), any(), anyMap()))
.thenReturn(Optional.of(computedFactory));
Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, githubUrl);
// when
FactoryDevfileV2Dto factory =
(FactoryDevfileV2Dto) githubFactoryParametersResolver.createFactory(params);
// then
ScmInfo scmInfo = factory.getScmInfo();
assertNotNull(scmInfo);
assertEquals(scmInfo.getScmProviderName(), "github");
assertEquals(scmInfo.getRepositoryUrl(), "https://github.com/eclipse/che.git");
assertEquals(scmInfo.getBranch(), "foobar");
}
private FactoryDto generateDevfileFactory() {
return newDto(FactoryDto.class)
.withV(CURRENT_VERSION)
@ -234,4 +258,11 @@ public class GithubFactoryParametersResolverTest {
.withApiVersion(CURRENT_API_VERSION)
.withMetadata(newDto(MetadataDto.class).withName("che")));
}
private FactoryDevfileV2Dto generateDevfileV2Factory() {
return newDto(FactoryDevfileV2Dto.class)
.withV(CURRENT_VERSION)
.withSource("repo")
.withDevfile(Map.of("schemaVersion", "2.0.0"));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2018 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/
@ -72,6 +72,6 @@ public class GithubUrlTest {
/** Check the original repository */
@Test
public void checkRepositoryLocation() {
assertEquals(githubUrl.repositoryLocation(), "https://github.com/eclipse/che");
assertEquals(githubUrl.repositoryLocation(), "https://github.com/eclipse/che.git");
}
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2012-2021 Red Hat, Inc.
Copyright (c) 2012-2018 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/
@ -71,6 +71,10 @@
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-factory-shared</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-model</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-workspace</artifactId>

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2018 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/
@ -25,9 +25,11 @@ import org.eclipse.che.api.factory.server.DefaultFactoryParameterResolver;
import org.eclipse.che.api.factory.server.scm.GitCredentialManager;
import org.eclipse.che.api.factory.server.scm.PersonalAccessTokenManager;
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.FactoryMetaDto;
import org.eclipse.che.api.factory.shared.dto.FactoryVisitor;
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.ProjectDto;
import org.eclipse.che.api.workspace.shared.dto.devfile.SourceDto;
@ -106,6 +108,18 @@ public class GitlabFactoryParametersResolver extends DefaultFactoryParameterReso
this.gitlabUrl = gitlabUrl;
}
@Override
public FactoryDevfileV2Dto visit(FactoryDevfileV2Dto factoryDto) {
ScmInfoDto scmInfo =
newDto(ScmInfoDto.class)
.withScmProviderName(gitlabUrl.getProviderName())
.withRepositoryUrl(gitlabUrl.repositoryLocation());
if (gitlabUrl.getBranch() != null) {
scmInfo.withBranch(gitlabUrl.getBranch());
}
return factoryDto.withScmInfo(scmInfo);
}
@Override
public FactoryDto visit(FactoryDto factory) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2018 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/
@ -32,6 +32,8 @@ import org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl;
*/
public class GitlabUrl implements RemoteFactoryUrl {
private final String NAME = "gitlab";
/** Hostname of the gitlab URL */
private String hostName;
@ -59,6 +61,11 @@ public class GitlabUrl implements RemoteFactoryUrl {
*/
protected GitlabUrl() {}
@Override
public String getProviderName() {
return NAME;
}
/**
* Gets hostname of this gitlab url
*

View File

@ -0,0 +1,183 @@
/*
* Copyright (c) 2012-2018 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/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.api.factory.server.gitlab;
import static java.util.Collections.singletonMap;
import static org.eclipse.che.api.factory.shared.Constants.CURRENT_VERSION;
import static org.eclipse.che.api.factory.shared.Constants.URL_PARAMETER_NAME;
import static org.eclipse.che.api.workspace.server.devfile.Constants.CURRENT_API_VERSION;
import static org.eclipse.che.dto.server.DtoFactory.newDto;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
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;
import java.util.Map;
import java.util.Optional;
import org.eclipse.che.api.core.model.factory.ScmInfo;
import org.eclipse.che.api.factory.server.scm.GitCredentialManager;
import org.eclipse.che.api.factory.server.scm.PersonalAccessTokenManager;
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.FactoryDevfileV2Dto;
import org.eclipse.che.api.factory.shared.dto.FactoryDto;
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;
import org.eclipse.che.api.workspace.shared.dto.devfile.SourceDto;
import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
/**
* Validate operations performed by the Github Factory service
*
* @author Florent Benoit
*/
@Listeners(MockitoTestNGListener.class)
public class GitlabFactoryParametersResolverTest {
@Mock private URLFactoryBuilder urlFactoryBuilder;
@Mock private URLFetcher urlFetcher;
@Mock private DevfileFilenamesProvider devfileFilenamesProvider;
GitlabUrlParser gitlabUrlParser;
@Mock private GitCredentialManager gitCredentialManager;
@Mock private PersonalAccessTokenManager personalAccessTokenManager;
private GitlabFactoryParametersResolver gitlabFactoryParametersResolver;
@BeforeMethod
protected void init() {
gitlabUrlParser = new GitlabUrlParser("http://gitlab.2mcl.com", devfileFilenamesProvider);
assertNotNull(this.gitlabUrlParser);
gitlabFactoryParametersResolver =
new GitlabFactoryParametersResolver(
urlFactoryBuilder,
urlFetcher,
gitlabUrlParser,
gitCredentialManager,
personalAccessTokenManager);
assertNotNull(this.gitlabFactoryParametersResolver);
}
/** Check url which is not a bitbucket url can't be accepted by this resolver */
@Test
public void checkInvalidAcceptUrl() {
Map<String, String> parameters = singletonMap(URL_PARAMETER_NAME, "http://github.com");
// shouldn't be accepted
assertFalse(gitlabFactoryParametersResolver.accept(parameters));
}
/** Check bitbucket url will be be accepted by this resolver */
@Test
public void checkValidAcceptUrl() {
Map<String, String> parameters =
singletonMap(URL_PARAMETER_NAME, "http://gitlab.2mcl.com/test/proj/repo.git");
// should be accepted
assertTrue(gitlabFactoryParametersResolver.accept(parameters));
}
@Test
public void shouldGenerateDevfileForFactoryWithNoDevfileOrJson() throws Exception {
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()))
.thenReturn(Optional.empty());
Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, gitlabUrl);
// when
FactoryDto factory = (FactoryDto) gitlabFactoryParametersResolver.createFactory(params);
// then
verify(urlFactoryBuilder).buildDefaultDevfile(eq("proj"));
assertEquals(factory, computedFactory);
SourceDto source = factory.getDevfile().getProjects().get(0).getSource();
assertEquals(source.getLocation(), gitlabUrl);
assertNull(source.getBranch());
}
@Test
public void shouldSetDefaultProjectIntoDevfileIfNotSpecified() throws Exception {
String gitlabUrl = "http://gitlab.2mcl.com/test/proj/repo/-/tree/foobar";
FactoryDto computedFactory = generateDevfileFactory();
when(urlFactoryBuilder.createFactoryFromDevfile(any(RemoteFactoryUrl.class), any(), anyMap()))
.thenReturn(Optional.of(computedFactory));
Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, gitlabUrl);
// when
FactoryDto factory = (FactoryDto) gitlabFactoryParametersResolver.createFactory(params);
// then
assertNotNull(factory.getDevfile());
SourceDto source = factory.getDevfile().getProjects().get(0).getSource();
assertEquals(source.getLocation(), "http://gitlab.2mcl.com/test/proj/repo.git");
assertEquals(source.getBranch(), "foobar");
}
@Test
public void shouldSetScmInfoIntoDevfileV2() throws Exception {
String gitlabUrl = "http://gitlab.2mcl.com/eclipse/che/-/tree/foobar";
FactoryDevfileV2Dto computedFactory = generateDevfileV2Factory();
when(urlFactoryBuilder.createFactoryFromDevfile(any(RemoteFactoryUrl.class), any(), anyMap()))
.thenReturn(Optional.of(computedFactory));
Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, gitlabUrl);
// when
FactoryDevfileV2Dto factory =
(FactoryDevfileV2Dto) gitlabFactoryParametersResolver.createFactory(params);
// then
ScmInfo scmInfo = factory.getScmInfo();
assertNotNull(scmInfo);
assertEquals(scmInfo.getScmProviderName(), "gitlab");
assertEquals(scmInfo.getRepositoryUrl(), "http://gitlab.2mcl.com/eclipse/che.git");
assertEquals(scmInfo.getBranch(), "foobar");
}
private FactoryDto generateDevfileFactory() {
return newDto(FactoryDto.class)
.withV(CURRENT_VERSION)
.withSource("repo")
.withDevfile(
newDto(DevfileDto.class)
.withApiVersion(CURRENT_API_VERSION)
.withMetadata(newDto(MetadataDto.class).withName("che")));
}
private FactoryDevfileV2Dto generateDevfileV2Factory() {
return newDto(FactoryDevfileV2Dto.class)
.withV(CURRENT_VERSION)
.withSource("repo")
.withDevfile(Map.of("schemaVersion", "2.0.0"));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2018 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/
@ -12,6 +12,7 @@
package org.eclipse.che.api.factory.shared.dto;
import static org.eclipse.che.api.core.factory.FactoryParameter.Obligation.MANDATORY;
import static org.eclipse.che.api.core.factory.FactoryParameter.Obligation.OPTIONAL;
import java.util.List;
import java.util.Map;
@ -19,6 +20,7 @@ import org.eclipse.che.api.core.factory.FactoryParameter;
import org.eclipse.che.api.core.rest.shared.dto.Hyperlinks;
import org.eclipse.che.api.core.rest.shared.dto.Link;
import org.eclipse.che.dto.shared.DTO;
import org.eclipse.che.dto.shared.JsonFieldName;
/**
* Factory DTO for Devfile v2. As che-server don't know the structure of Devfile v2, we're using
@ -42,6 +44,12 @@ public interface FactoryDevfileV2Dto extends FactoryMetaDto, Hyperlinks {
FactoryDevfileV2Dto withDevfile(Map<String, Object> devfile);
@FactoryParameter(obligation = OPTIONAL)
@JsonFieldName("scm_info")
ScmInfoDto getScmInfo();
FactoryDevfileV2Dto withScmInfo(ScmInfoDto scmInfo);
@Override
FactoryDevfileV2Dto withSource(String source);

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2012-2018 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/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.api.factory.shared.dto;
import org.eclipse.che.api.core.model.factory.ScmInfo;
import org.eclipse.che.dto.shared.DTO;
import org.eclipse.che.dto.shared.JsonFieldName;
@DTO
public interface ScmInfoDto extends ScmInfo {
@Override
@JsonFieldName("scm_provider")
String getScmProviderName();
void setScmProviderName(String scmProviderName);
ScmInfoDto withScmProviderName(String scmProviderName);
@Override
@JsonFieldName("clone_url")
String getRepositoryUrl();
void setRepositoryUrl(String repositoryUrl);
ScmInfoDto withRepositoryUrl(String repositoryUrl);
@Override
String getBranch();
void setBranch(String branch);
ScmInfoDto withBranch(String branch);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2018 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/
@ -25,6 +25,11 @@ public class DefaultFactoryUrl implements RemoteFactoryUrl {
private String devfileFileLocation;
@Override
public String getProviderName() {
return "default";
}
@Override
public List<DevfileLocation> devfileFileLocations() {
return singletonList(
@ -51,6 +56,11 @@ public class DefaultFactoryUrl implements RemoteFactoryUrl {
return URI.create(devfileFileLocation).getHost();
}
@Override
public String getBranch() {
return null;
}
public DefaultFactoryUrl withDevfileFileLocation(String devfileFileLocation) {
this.devfileFileLocation = devfileFileLocation;
return this;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2018 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/
@ -20,6 +20,9 @@ import java.util.Optional;
*/
public interface RemoteFactoryUrl {
/** Provider name for given URL. */
String getProviderName();
/**
* List of possible filenames and raw locations of devfile.
*
@ -33,6 +36,9 @@ public interface RemoteFactoryUrl {
/** Remote hostname */
String getHostName();
/** Remote branch */
String getBranch();
/** Describes devfile location, including filename if any. */
interface DevfileLocation {
Optional<String> filename();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2018 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/
@ -175,6 +175,11 @@ public class URLFactoryBuilderTest {
RemoteFactoryUrl githubLikeRemoteUrl =
new RemoteFactoryUrl() {
@Override
public String getProviderName() {
return null;
}
@Override
public List<DevfileLocation> devfileFileLocations() {
return Collections.singletonList(
@ -200,6 +205,11 @@ public class URLFactoryBuilderTest {
public String getHostName() {
return null;
}
@Override
public String getBranch() {
return null;
}
};
FactoryMetaDto factory =