GithubFactoryParametersResolver extends DefaultFactoryParameterResolver { @Override public FactoryDto createFactory(@NotNull final Map<String, String> factoryParameters) throws BadRequestException, ServerException { final GithubUrl githubUrl = githubUrlParser.parse(factoryParameters.get(URL_PARAMETER_NAME)); FactoryDto factory = urlFactoryBuilder .createFactoryFromDevfile( githubUrl, fileName -> urlFetcher.fetch(githubUrl.rawFileLocation(fileName)), extractOverrideParams(factoryParameters)) .orElseGet( () -> urlFactoryBuilder .createFactoryFromJson(githubUrl) .orElseGet( () -> newDto(FactoryDto.class) .withV(CURRENT_VERSION) .withSource("repo"))); if (factory.getWorkspace() != null) { return projectConfigDtoMerger.merge( factory, () -> { return newDto(ProjectConfigDto.class) .withSource(githubSourceStorageBuilder.buildWorkspaceConfigSource(githubUrl)) .withName(githubUrl.getRepository()) .withPath("/".concat(githubUrl.getRepository())); }); } else if (factory.getDevfile() == null) { factory.setDevfile(urlFactoryBuilder.buildDefaultDevfile(githubUrl.getRepository())); } List<ProjectDto> projects = factory.getDevfile().getProjects(); if (projects.isEmpty()) { factory .getDevfile() .setProjects( Collections.singletonList( newDto(ProjectDto.class) .withSource(githubSourceStorageBuilder.buildDevfileSource(githubUrl)) .withName(githubUrl.getRepository()))); } else { 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; } @Inject  GithubFactoryParametersResolver(
      GithubURLParser githubUrlParser,
      URLFetcher urlFetcher,
      GithubSourceStorageBuilder githubSourceStorageBuilder,
      URLFactoryBuilder urlFactoryBuilder,
      ProjectConfigDtoMerger projectConfigDtoMerger); @Override boolean accept(@NotNull final Map<String, String> factoryParameters); @Override FactoryDto createFactory(@NotNull final Map<String, String> factoryParameters);  }
@Test public void shouldGenerateDevfileForFactoryWithNoDevfileOrJson() throws Exception { String githubUrl = "https: FactoryDto computedFactory = generateDevfileFactory(); when(urlFactoryBuilder.buildDefaultDevfile(any())).thenReturn(computedFactory.getDevfile()); when(urlFactoryBuilder.createFactoryFromJson(any(RemoteFactoryUrl.class))) .thenReturn(Optional.empty()); when(urlFactoryBuilder.createFactoryFromDevfile(any(RemoteFactoryUrl.class), any(), anyMap())) .thenReturn(Optional.empty()); Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, githubUrl); FactoryDto factory = githubFactoryParametersResolver.createFactory(params); 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 public void shouldReturnFactoryFromRepositoryWithDevfile() throws Exception { when(devfileFilenamesProvider.getConfiguredDevfileFilenames()) .thenReturn(Collections.singletonList("devfile.yaml")); String githubUrl = "https: 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); FactoryDto factory = githubFactoryParametersResolver.createFactory(params); assertNotNull(factory.getDevfile()); assertNull(factory.getWorkspace()); verify(urlFactoryBuilder) .createFactoryFromDevfile(factoryUrlArgumentCaptor.capture(), any(), anyMap()); verify(urlFactoryBuilder, never()).buildDefaultDevfile(eq("che")); assertEquals( factoryUrlArgumentCaptor.getValue().devfileFileLocations().iterator().next().location(), "https: }
@Test public void shouldSetDefaultProjectIntoDevfileIfNotSpecified() throws Exception { String githubUrl = "https: 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); FactoryDto factory = githubFactoryParametersResolver.createFactory(params); assertNotNull(factory.getDevfile()); SourceDto source = factory.getDevfile().getProjects().get(0).getSource(); assertEquals(source.getLocation(), "https: assertEquals(source.getBranch(), "foobar"); }
@Test public void shouldSetBranchIntoDevfileIfNotMatchesCurrent() throws Exception { String githubUrl = "https: FactoryDto computedFactory = generateDevfileFactory(); computedFactory .getDevfile() .getProjects() .add( newDto(ProjectDto.class) .withSource( newDto(SourceDto.class).withLocation("https: when(urlFactoryBuilder.createFactoryFromDevfile(any(RemoteFactoryUrl.class), any(), anyMap())) .thenReturn(Optional.of(computedFactory)); Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, githubUrl); FactoryDto factory = githubFactoryParametersResolver.createFactory(params); assertNotNull(factory.getDevfile()); SourceDto source = factory.getDevfile().getProjects().get(0).getSource(); assertEquals(source.getBranch(), "foobranch"); }
@Test public void shouldReturnFactoryFromRepositoryWithFactoryJson() throws Exception { String githubUrl = "https: FactoryDto computedFactory = generateWsConfigFactory(); when(urlFactoryBuilder.createFactoryFromJson(any(RemoteFactoryUrl.class))) .thenReturn(Optional.of(computedFactory)); Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, githubUrl); githubFactoryParametersResolver.createFactory(params); verify(urlFactoryBuilder).createFactoryFromJson(factoryUrlArgumentCaptor.capture()); verify(urlFactoryBuilder, never()).buildDefaultDevfile(eq("che")); assertEquals( factoryUrlArgumentCaptor.getValue().factoryFileLocation(), "https: assertEquals(factoryUrlArgumentCaptor.getValue().getFactoryFilename(), ".factory.json"); }