GitStorageConnector implements StorageConnector { @Override public void write(Exportable artifact, UnitOfWork transaction, Properties parameters) throws Exception { ArgCheck.isNotNull(parameters); String destination = getFilePath(parameters); ArgCheck.isNotEmpty(destination); cloneRepository(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SSS"); Date now = new Date(); String timestamp = sdf.format(now); String artifactName = artifact.getName(transaction); String branchName = artifactName + HYPHEN + timestamp; git.checkout() .setName(branchName) .setCreateBranch(true) .setForce(true) .setStartPoint(getBranch()) .call(); byte[] contents = artifact.export(transaction, parameters); File destFile; DocumentType documentType = artifact.getDocumentType(transaction); if (DocumentType.ZIP.equals(documentType)) { destination = directory(destination, documentType); destFile = new File(git.getRepository().getWorkTree(), destination); Files.createDirectories(destFile.toPath()); ByteArrayInputStream byteStream = new ByteArrayInputStream(contents); FileUtils.zipExtract(byteStream, destFile); } else { destFile = new File(git.getRepository().getWorkTree(), destination); FileUtils.write(contents, destFile); } git.add() .addFilepattern(destination) .call(); String author = parameters.getProperty(GitStorageConnector.AUTHOR_NAME_PROPERTY, "anonymous"); String authorEmail = parameters.getProperty(GitStorageConnector.AUTHOR_EMAIL_PROPERTY, "anon@komodo.org"); RevCommit mergeCommit = git.commit() .setAuthor(author, authorEmail) .setCommitter(author, authorEmail) .setMessage("Change to artifact " + artifactName + " at " + timestamp) .call(); git.checkout() .setName(getBranch()) .setForce(true) .call(); refresh(); git.merge() .include(mergeCommit) .call(); git.push() .setTransportConfigCallback(transportConfigCallback) .call(); } GitStorageConnector(Properties parameters); @Override StorageConnectorId getId(); @Override Set<Descriptor> getDescriptors(); String getPath(); String getBranch(); String getDestination(); String getFilePath(Properties parameters); @Override boolean refresh(); @Override void write(Exportable artifact, UnitOfWork transaction, Properties parameters); @Override InputStream read(Properties parameters); @Override StorageTree<String> browse(); @Override void dispose(); static final String REPO_PATH_PROPERTY; static final String REPO_DEST_PROPERTY; static final String REPO_BRANCH_PROPERTY; static final String REPO_PRIVATE_KEY; static final String REPO_PASSPHRASE; static final String REPO_KNOWN_HOSTS_ID; static final String REPO_USERNAME; static final String REPO_PASSWORD; static final String AUTHOR_NAME_PROPERTY; static final String AUTHOR_EMAIL_PROPERTY; }
@Test public void testRequiredFileDestParameter() { try { Properties parameters = new Properties(); parameters.setProperty(GitStorageConnector.REPO_DEST_PROPERTY, localTmpDir.getAbsolutePath()); parameters.setProperty(GitStorageConnector.REPO_PATH_PROPERTY, myGitDir.getAbsolutePath()); connector = new GitStorageConnector(parameters); UnitOfWork transaction = mock(UnitOfWork.class); when(transaction.getState()).thenReturn(State.NOT_STARTED); parameters = new Properties(); Exportable artifact = mock(Exportable.class); connector.write(artifact, transaction, parameters); fail("Should not allow null dest parameter"); } catch (Exception ex) { } }