Search in sources :

Example 1 with ParsingResult

use of alien4cloud.tosca.parser.ParsingResult in project alien4cloud by alien4cloud.

the class CsarGitServiceTest method importManyBranchFromGit.

@Test
public void importManyBranchFromGit() {
    CsarGitCheckoutLocation alien12Location = new CsarGitCheckoutLocation();
    alien12Location.setBranchId("1.2.0");
    CsarGitCheckoutLocation alien14Location = new CsarGitCheckoutLocation();
    alien14Location.setBranchId("1.4.0");
    List<CsarGitCheckoutLocation> importLocations = new LinkedList<>();
    importLocations.add(alien12Location);
    importLocations.add(alien14Location);
    String repoId = csarGitRepositoryService.create("https://github.com/alien4cloud/tosca-normative-types.git", "", "", importLocations, false);
    List<ParsingResult<Csar>> result = csarGitService.importFromGitRepository(repoId);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals("tosca-normative-types", result.get(0).getResult().getName());
    Assert.assertEquals("1.0.0-ALIEN12", result.get(0).getResult().getVersion());
    Assert.assertEquals("tosca-normative-types", result.get(1).getResult().getName());
    Assert.assertEquals("1.0.0-ALIEN14", result.get(1).getResult().getVersion());
}
Also used : ParsingResult(alien4cloud.tosca.parser.ParsingResult) CsarGitCheckoutLocation(alien4cloud.model.git.CsarGitCheckoutLocation) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 2 with ParsingResult

use of alien4cloud.tosca.parser.ParsingResult in project alien4cloud by alien4cloud.

the class CsarGitServiceTest method importManyBranchFromGitAndStoreLocally.

@Test
public void importManyBranchFromGitAndStoreLocally() {
    CsarGitCheckoutLocation alien12Location = new CsarGitCheckoutLocation();
    alien12Location.setBranchId("1.2.0");
    CsarGitCheckoutLocation alien14Location = new CsarGitCheckoutLocation();
    alien14Location.setBranchId("1.4.0");
    List<CsarGitCheckoutLocation> importLocations = new LinkedList<>();
    importLocations.add(alien12Location);
    importLocations.add(alien14Location);
    String repoId = csarGitRepositoryService.create("https://github.com/alien4cloud/tosca-normative-types.git", "", "", importLocations, true);
    List<ParsingResult<Csar>> result = csarGitService.importFromGitRepository(repoId);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals("tosca-normative-types", result.get(0).getResult().getName());
    Assert.assertEquals("1.0.0-ALIEN12", result.get(0).getResult().getVersion());
    Assert.assertEquals("tosca-normative-types", result.get(1).getResult().getName());
    Assert.assertEquals("1.0.0-ALIEN14", result.get(1).getResult().getVersion());
    // now we re-import
    result = csarGitService.importFromGitRepository(repoId);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(ErrorCode.CSAR_ALREADY_INDEXED, result.get(0).getContext().getParsingErrors().get(0).getErrorCode());
    Assert.assertEquals(ErrorCode.CSAR_ALREADY_INDEXED, result.get(1).getContext().getParsingErrors().get(0).getErrorCode());
}
Also used : ParsingResult(alien4cloud.tosca.parser.ParsingResult) CsarGitCheckoutLocation(alien4cloud.model.git.CsarGitCheckoutLocation) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 3 with ParsingResult

use of alien4cloud.tosca.parser.ParsingResult in project alien4cloud by alien4cloud.

the class CsarGitService method doImport.

private List<ParsingResult<Csar>> doImport(CsarGitRepository csarGitRepository, CsarGitCheckoutLocation csarGitCheckoutLocation) {
    Git git = null;
    try {
        // checkout the repository branch
        log.debug("Checkout repository: url: [ {} ] branch [ {} ] to [ {} ]", csarGitRepository.getRepositoryUrl(), csarGitCheckoutLocation.getBranchId(), tempDirPath);
        git = RepositoryManager.cloneOrCheckout(tempDirPath, csarGitRepository.getRepositoryUrl(), csarGitRepository.getUsername(), csarGitRepository.getPassword(), csarGitCheckoutLocation.getBranchId(), csarGitRepository.getId());
        // if the repository is persistent we also have to pull to get the latest version
        if (csarGitRepository.isStoredLocally() && !RepositoryManager.isATag(git, csarGitCheckoutLocation.getBranchId())) {
            // try to pull
            log.debug("Pull local repository");
            RepositoryManager.pull(git, csarGitRepository.getUsername(), csarGitRepository.getPassword());
        }
        String hash = RepositoryManager.getLastHash(git);
        log.debug("Importing archives from git repository, pulled hash is {}", hash);
        // now that the repository is checked out and up to date process with the import
        List<ParsingResult<Csar>> results = processImport(csarGitRepository, csarGitCheckoutLocation, hash);
        if (!Objects.equals(csarGitCheckoutLocation.getLastImportedHash(), hash)) {
            csarGitCheckoutLocation.setLastImportedHash(hash);
            // update the hash for this location.
            alienDAO.save(csarGitRepository);
        }
        // TODO best would be to provide with a better result to show that we didn't retried import
        return results;
    } finally {
        RepositoryManager.close(git);
    }
}
Also used : Git(org.eclipse.jgit.api.Git) ParsingResult(alien4cloud.tosca.parser.ParsingResult)

Example 4 with ParsingResult

use of alien4cloud.tosca.parser.ParsingResult in project alien4cloud by alien4cloud.

the class CsarGitService method processImport.

private List<ParsingResult<Csar>> processImport(CsarGitRepository csarGitRepository, CsarGitCheckoutLocation csarGitCheckoutLocation, String gitHash) {
    // find all the archives under the given hierarchy and zip them to create archives
    Path archiveZipRoot = tempZipDirPath.resolve(csarGitRepository.getId());
    Path archiveGitRoot = tempDirPath.resolve(csarGitRepository.getId());
    if (csarGitCheckoutLocation.getSubPath() != null && !csarGitCheckoutLocation.getSubPath().isEmpty()) {
        archiveGitRoot = archiveGitRoot.resolve(csarGitCheckoutLocation.getSubPath());
    }
    Set<Path> archivePaths = csarFinderService.prepare(archiveGitRoot, archiveZipRoot);
    List<ParsingResult<Csar>> parsingResults = Lists.newArrayList();
    Map<CSARDependency, CsarDependenciesBean> csarDependenciesBeans = uploadService.preParsing(archivePaths, parsingResults);
    List<CsarDependenciesBean> sorted = sort(csarDependenciesBeans);
    for (CsarDependenciesBean csarBean : sorted) {
        String archiveRepoPath = archiveZipRoot.relativize(csarBean.getPath().getParent()).toString();
        if (csarGitCheckoutLocation.getLastImportedHash() != null && csarGitCheckoutLocation.getLastImportedHash().equals(gitHash) && csarService.get(csarBean.getSelf().getName(), csarBean.getSelf().getVersion()) != null) {
            // no commit since last import and the archive still exist in the repo, so do not import
            addAlreadyImportParsingResult(archiveRepoPath, parsingResults);
            continue;
        }
        try {
            // FIXME Add possibility to choose an workspace
            ParsingResult<Csar> result = uploadService.upload(csarBean.getPath(), CSARSource.GIT, AlienConstants.GLOBAL_WORKSPACE_ID);
            result.getContext().setFileName(archiveRepoPath + "/" + result.getContext().getFileName());
            parsingResults.add(result);
        } catch (ParsingException e) {
            ParsingResult<Csar> failedResult = new ParsingResult<>();
            failedResult.setContext(new ParsingContext(archiveRepoPath));
            failedResult.getContext().setParsingErrors(e.getParsingErrors());
            parsingResults.add(failedResult);
            log.debug("Failed to import archive from git as it cannot be parsed", e);
        } catch (AlreadyExistException | ToscaTypeAlreadyDefinedInOtherCSAR | CSARUsedInActiveDeployment e) {
            ParsingResult<Csar> failedResult = new ParsingResult<>();
            failedResult.setContext(new ParsingContext(archiveRepoPath));
            failedResult.getContext().setParsingErrors(Lists.newArrayList(UploadExceptionUtil.parsingErrorFromException(e)));
            parsingResults.add(failedResult);
        }
    }
    return parsingResults;
}
Also used : Path(java.nio.file.Path) Csar(org.alien4cloud.tosca.model.Csar) ParsingContext(alien4cloud.tosca.parser.ParsingContext) ToscaTypeAlreadyDefinedInOtherCSAR(alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR) CsarDependenciesBean(org.alien4cloud.tosca.model.CsarDependenciesBean) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) ParsingResult(alien4cloud.tosca.parser.ParsingResult) ParsingException(alien4cloud.tosca.parser.ParsingException) CSARUsedInActiveDeployment(alien4cloud.component.repository.exception.CSARUsedInActiveDeployment) AlreadyExistException(alien4cloud.exception.AlreadyExistException)

Example 5 with ParsingResult

use of alien4cloud.tosca.parser.ParsingResult in project alien4cloud by alien4cloud.

the class CsarGitService method addAlreadyImportParsingResult.

/**
 * Just add a parsing info to the list stating that the archive is already imported and
 *
 * @param archivePath The path of the archive in the repo.
 * @param parsingResults The list of parsing results.
 */
private void addAlreadyImportParsingResult(String archivePath, List<ParsingResult<Csar>> parsingResults) {
    ParsingResult<Csar> result = new ParsingResult<>();
    result.setContext(new ParsingContext(archivePath));
    result.getContext().setParsingErrors(Lists.newArrayList(new ParsingError(ParsingErrorLevel.INFO, ErrorCode.CSAR_ALREADY_INDEXED, "No new commit since last import and archive already indexed.", null, null, null, null)));
    parsingResults.add(result);
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) ParsingResult(alien4cloud.tosca.parser.ParsingResult) ParsingContext(alien4cloud.tosca.parser.ParsingContext) ParsingError(alien4cloud.tosca.parser.ParsingError)

Aggregations

ParsingResult (alien4cloud.tosca.parser.ParsingResult)10 CsarGitCheckoutLocation (alien4cloud.model.git.CsarGitCheckoutLocation)5 ParsingContext (alien4cloud.tosca.parser.ParsingContext)4 LinkedList (java.util.LinkedList)4 Test (org.junit.Test)4 Path (java.nio.file.Path)3 Csar (org.alien4cloud.tosca.model.Csar)3 ParsingException (alien4cloud.tosca.parser.ParsingException)2 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)2 CsarDependenciesBean (org.alien4cloud.tosca.model.CsarDependenciesBean)2 CSARUsedInActiveDeployment (alien4cloud.component.repository.exception.CSARUsedInActiveDeployment)1 ToscaTypeAlreadyDefinedInOtherCSAR (alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR)1 AlreadyExistException (alien4cloud.exception.AlreadyExistException)1 CsarGitRepository (alien4cloud.model.git.CsarGitRepository)1 ToscaContextual (alien4cloud.tosca.context.ToscaContextual)1 ParsingError (alien4cloud.tosca.parser.ParsingError)1 IOException (java.io.IOException)1 Git (org.eclipse.jgit.api.Git)1