Set current GitHub repository URL and branch into devfile projects if they're missing or didn't match

7.20.x
Max Shaposhnik 2020-01-22 19:41:46 +02:00 committed by GitHub
parent 3ccf91192b
commit dc2f3b5ea9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 2 deletions

View File

@ -16,6 +16,7 @@ import static org.eclipse.che.api.factory.shared.Constants.URL_PARAMETER_NAME;
import static org.eclipse.che.dto.server.DtoFactory.newDto;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -115,8 +116,13 @@ public class GithubFactoryParametersResolver extends DefaultFactoryParameterReso
.withPath("/".concat(githubUrl.getRepository()));
});
} else if (factory.getDevfile() == null) {
// initialize default devfile and github project
// initialize default devfile
factory.setDevfile(urlFactoryBuilder.buildDefaultDevfile(githubUrl.getRepository()));
}
List<ProjectDto> projects = factory.getDevfile().getProjects();
// if no projects set, set the default one from GitHub url
if (projects.isEmpty()) {
factory
.getDevfile()
.setProjects(
@ -124,6 +130,16 @@ public class GithubFactoryParametersResolver extends DefaultFactoryParameterReso
newDto(ProjectDto.class)
.withSource(githubSourceStorageBuilder.buildDevfileSource(githubUrl))
.withName(githubUrl.getRepository())));
} else {
// update existing project with same repository, set current branch if needed
projects.forEach(
project -> {
final String location = project.getSource().getLocation();
if (location.equals(githubUrl.repositoryLocation())
|| location.equals(githubUrl.repositoryLocation() + ".git")) {
project.getSource().setBranch(githubUrl.getBranch());
}
});
}
return factory;
}

View File

@ -57,7 +57,7 @@ public class GithubSourceStorageBuilder {
*/
public SourceDto buildDevfileSource(GithubUrl githubUrl) {
return newDto(SourceDto.class)
.withLocation(githubUrl.repositoryLocation())
.withLocation(githubUrl.repositoryLocation() + ".git")
.withType("github")
.withBranch(githubUrl.getBranch())
.withSparseCheckoutDir(githubUrl.getSubfolder());

View File

@ -39,6 +39,8 @@ import org.eclipse.che.api.workspace.server.devfile.FileContentProvider;
import org.eclipse.che.api.workspace.shared.dto.WorkspaceConfigDto;
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.ProjectDto;
import org.eclipse.che.api.workspace.shared.dto.devfile.SourceDto;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
@ -126,6 +128,9 @@ public class GithubFactoryParametersResolverTest {
// 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(), "master");
}
@Test
@ -156,6 +161,52 @@ public class GithubFactoryParametersResolverTest {
assertEquals(factoryUrlArgumentCaptor.getValue().getDevfileFilename(), "devfile.yaml");
}
@Test
public void shouldSetDefaultProjectIntoDevfileIfNotSpecified() throws Exception {
String githubUrl = "https://github.com/eclipse/che/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, githubUrl);
// when
FactoryDto factory = githubFactoryParametersResolver.createFactory(params);
// then
assertNotNull(factory.getDevfile());
SourceDto source = factory.getDevfile().getProjects().get(0).getSource();
assertEquals(source.getLocation(), "https://github.com/eclipse/che.git");
assertEquals(source.getBranch(), "foobar");
}
@Test
public void shouldSetBranchIntoDevfileIfNotMatchesCurrent() throws Exception {
String githubUrl = "https://github.com/eclipse/che/tree/foobranch";
FactoryDto computedFactory = generateDevfileFactory();
computedFactory
.getDevfile()
.getProjects()
.add(
newDto(ProjectDto.class)
.withSource(
newDto(SourceDto.class).withLocation("https://github.com/eclipse/che.git")));
when(urlFactoryBuilder.createFactoryFromDevfile(any(RemoteFactoryUrl.class), any(), anyMap()))
.thenReturn(Optional.of(computedFactory));
Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, githubUrl);
// when
FactoryDto factory = githubFactoryParametersResolver.createFactory(params);
// then
assertNotNull(factory.getDevfile());
SourceDto source = factory.getDevfile().getProjects().get(0).getSource();
assertEquals(source.getBranch(), "foobranch");
}
@Test
public void shouldReturnFactoryFromRepositoryWithFactoryJson() throws Exception {