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);
}
}
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;
}
Aggregations