Search in sources :

Example 1 with GitInfo

use of org.eclipse.winery.repository.GitInfo in project winery by eclipse.

the class CsarExporter method addArtifactTemplateToZipFile.

/**
 * Special handling for artifact template directories source and files
 *
 * @param zos         Output stream for the archive that should contain the file
 * @param ref         Reference to the file that should be added to the archive
 * @param archivePath Path to the file inside the archive
 * @throws IOException thrown when the temporary directory can not be created
 */
private void addArtifactTemplateToZipFile(ArchiveOutputStream zos, IGenericRepository repository, RepositoryFileReference ref, String archivePath) throws IOException {
    GitInfo gitInfo = BackendUtils.getGitInformation((DirectoryId) ref.getParent());
    if (gitInfo == null) {
        try (InputStream is = repository.newInputStream(ref)) {
            if (is != null) {
                ArchiveEntry archiveEntry = new ZipArchiveEntry(archivePath);
                zos.putArchiveEntry(archiveEntry);
                IOUtils.copy(is, zos);
                zos.closeArchiveEntry();
            }
        } catch (Exception e) {
            CsarExporter.LOGGER.error("Could not copy file to ZIP outputstream", e);
        }
        return;
    }
    // TODO: This is not quite correct. The files should reside checked out at "source/"
    Path tempDir = Files.createTempDirectory(WINERY_TEMP_DIR_PREFIX);
    try {
        Git git = Git.cloneRepository().setURI(gitInfo.URL).setDirectory(tempDir.toFile()).call();
        git.checkout().setName(gitInfo.BRANCH).call();
        String path = "artifacttemplates/" + Util.URLencode(((ArtifactTemplateId) ref.getParent().getParent()).getQName().getNamespaceURI()) + "/" + ((ArtifactTemplateId) ref.getParent().getParent()).getQName().getLocalPart() + "/files/";
        TArtifactTemplate template = BackendUtils.getTArtifactTemplate((DirectoryId) ref.getParent());
        addWorkingTreeToArchive(zos, template, tempDir, path);
    } catch (GitAPIException e) {
        CsarExporter.LOGGER.error(String.format("Error while cloning repo: %s / %s", gitInfo.URL, gitInfo.BRANCH), e);
    } finally {
        deleteDirectory(tempDir);
    }
}
Also used : Path(java.nio.file.Path) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) GitInfo(org.eclipse.winery.repository.GitInfo) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) JAXBException(javax.xml.bind.JAXBException) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)

Example 2 with GitInfo

use of org.eclipse.winery.repository.GitInfo in project winery by eclipse.

the class BackendUtils method getGitInformation.

/**
 * @param directoryId DirectoryId of the ArtifactTemplate that should contain a reference to a git repository.
 * @return The URL and the branch/tag that contains the files for the ArtifactTemplate. null if no git information
 * is given.
 */
public static GitInfo getGitInformation(DirectoryId directoryId) {
    if (!(directoryId.getParent() instanceof ArtifactTemplateId)) {
        return null;
    }
    RepositoryFileReference ref = BackendUtils.getRefOfDefinitions((ArtifactTemplateId) directoryId.getParent());
    try (InputStream is = RepositoryFactory.getRepository().newInputStream(ref)) {
        Unmarshaller u = JAXBSupport.createUnmarshaller();
        Definitions defs = ((Definitions) u.unmarshal(is));
        Map<QName, String> atts = defs.getOtherAttributes();
        String src = atts.get(new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "gitsrc"));
        String branch = atts.get(new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "gitbranch"));
        // ^ is the XOR operator
        if (src == null ^ branch == null) {
            LOGGER.error("Git information not complete, URL or branch missing");
            return null;
        } else if (src == null && branch == null) {
            return null;
        }
        return new GitInfo(src, branch);
    } catch (IOException e) {
        LOGGER.error("Error reading definitions of " + directoryId.getParent() + " at " + ref.getFileName(), e);
    } catch (JAXBException e) {
        LOGGER.error("Error in XML in " + ref.getFileName(), e);
    }
    return null;
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Definitions(org.eclipse.winery.model.tosca.Definitions) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller) GitInfo(org.eclipse.winery.repository.GitInfo) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)

Aggregations

JAXBException (javax.xml.bind.JAXBException)2 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)2 GitInfo (org.eclipse.winery.repository.GitInfo)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 QName (javax.xml.namespace.QName)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)1 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)1 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1 Git (org.eclipse.jgit.api.Git)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)1 Definitions (org.eclipse.winery.model.tosca.Definitions)1