CAMEL-7982: camel-git - A generic git component, add commit operation

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/288d3bde
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/288d3bde
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/288d3bde

Branch: refs/heads/master
Commit: 288d3bded33dd180af5c7c7390bbbc2515b89ec5
Parents: 13b7373
Author: Andrea Cosentino <[email protected]>
Authored: Sat Jul 18 11:05:59 2015 +0200
Committer: Andrea Cosentino <[email protected]>
Committed: Sat Jul 18 11:08:14 2015 +0200

----------------------------------------------------------------------
 .../camel/component/git/GitConstants.java       |   1 +
 .../camel/component/git/GitOperation.java       |   1 +
 .../apache/camel/component/git/GitProducer.java |  64 +++++++++--
 .../github/producer/GitProducerTest.java        | 108 ++++++++++++++++++-
 4 files changed, 162 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/288d3bde/components/camel-git/src/main/java/org/apache/camel/component/git/GitConstants.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitConstants.java
 
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitConstants.java
index c96959b..0ec6856 100644
--- 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitConstants.java
+++ 
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitConstants.java
@@ -19,4 +19,5 @@ package org.apache.camel.component.git;
 public interface GitConstants {
        public static final String GIT_OPERATION = "CamelGitOperation";
        public static final String GIT_FILE_NAME = "CamelGitFilename";
+       public static final String GIT_COMMIT_MESSAGE = "CamelGitCommitMessage";
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/288d3bde/components/camel-git/src/main/java/org/apache/camel/component/git/GitOperation.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitOperation.java
 
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitOperation.java
index 62b19a4..49109c6 100644
--- 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitOperation.java
+++ 
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitOperation.java
@@ -21,4 +21,5 @@ public interface GitOperation {
     public final static String CLONE_OPERATION = "clone";
     public final static String INIT_OPERATION = "init";
     public final static String ADD_OPERATION = "add";
+    public final static String COMMIT_OPERATION = "commit";
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/288d3bde/components/camel-git/src/main/java/org/apache/camel/component/git/GitProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitProducer.java
 
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitProducer.java
index 1ccbac7..5ee7ee3 100644
--- 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitProducer.java
+++ 
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitProducer.java
@@ -1,12 +1,14 @@
 package org.apache.camel.component.git;
 
 import java.io.File;
+import java.io.IOException;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.camel.util.ObjectHelper;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -22,12 +24,17 @@ public class GitProducer extends DefaultProducer{
 
        @Override
        public void process(Exchange exchange) throws Exception {
-        String operation;              
+        String operation;      
+        Repository repo;
            if (ObjectHelper.isEmpty(endpoint.getOperation())) {
                operation = 
exchange.getIn().getHeader(GitConstants.GIT_OPERATION, String.class);
            } else {
                operation = endpoint.getOperation();
            }
+       if (ObjectHelper.isEmpty(endpoint.getLocalPath())) {
+               throw new IllegalArgumentException("Local path must specified 
to execute " + operation);
+       }
+       repo = getLocalRepository();
            
            switch (operation) {
            case GitOperation.CLONE_OPERATION:
@@ -39,9 +46,14 @@ public class GitProducer extends DefaultProducer{
                break;
 
            case GitOperation.ADD_OPERATION:
-               doAdd(exchange, operation);
-               break;          
+               doAdd(exchange, operation, repo);
+               break;
+               
+           case GitOperation.COMMIT_OPERATION:
+               doCommit(exchange, operation, repo);
+               break;  
            }
+           repo.close();
        }
        
     protected void doClone(Exchange exchange, String operation) {
@@ -70,7 +82,7 @@ public class GitProducer extends DefaultProducer{
                throw new IllegalArgumentException("Local path must specified 
to execute " + operation);
        }
        try {
-                       result = Git.init().setDirectory(new 
File(endpoint.getLocalPath(),"")).call();
+                       result = Git.init().setDirectory(new 
File(endpoint.getLocalPath(),"")).setBare(false).call();
                } catch (Exception e) {
                        LOG.error("There was an error in Git " + operation + " 
operation");
                        e.printStackTrace();
@@ -79,21 +91,55 @@ public class GitProducer extends DefaultProducer{
                }
     }
     
-    protected void doAdd(Exchange exchange, String operation) {
+    protected void doAdd(Exchange exchange, String operation, Repository repo) 
{
+       Git git = null;
        String fileName = null;
-       if (ObjectHelper.isEmpty(endpoint.getLocalPath())) {
-               throw new IllegalArgumentException("Local path must specified 
to execute " + operation);
-       }
        if 
(ObjectHelper.isNotEmpty(exchange.getIn().getHeader(GitConstants.GIT_FILE_NAME)))
 {
                fileName = 
exchange.getIn().getHeader(GitConstants.GIT_FILE_NAME, String.class);
        } else {
                throw new IllegalArgumentException("File name must be specified 
to execute " + operation);
        }
        try {
-                       Git.open(new 
File(endpoint.getLocalPath())).add().addFilepattern(fileName).call();
+               git = new Git(repo);
+                       git.add().addFilepattern(fileName).call();
                } catch (Exception e) {
                        LOG.error("There was an error in Git " + operation + " 
operation");
                        e.printStackTrace();
                }
     }
+    
+    protected void doCommit(Exchange exchange, String operation, Repository 
repo) {
+       Git git = null;
+       String commitMessage = null;
+       if 
(ObjectHelper.isNotEmpty(exchange.getIn().getHeader(GitConstants.GIT_COMMIT_MESSAGE)))
 {
+               commitMessage = 
exchange.getIn().getHeader(GitConstants.GIT_COMMIT_MESSAGE, String.class);
+       } else {
+               throw new IllegalArgumentException("Commit message must be 
specified to execute " + operation);
+       }
+       try {
+            git = new Git(repo);
+            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                
git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+            }
+               git.commit().setMessage(commitMessage).call();
+               } catch (Exception e) {
+                       LOG.error("There was an error in Git " + operation + " 
operation");
+                       e.printStackTrace();
+               }
+    }
+    
+    private Repository getLocalRepository(){
+        FileRepositoryBuilder builder = new FileRepositoryBuilder();
+        Repository repo = null;
+               try {
+                       repo = builder.setGitDir(new 
File(endpoint.getLocalPath(), ".git"))
+                               .readEnvironment() // scan environment GIT_* 
variables
+                               .findGitDir() // scan up the file system tree
+                               .build();
+               } catch (IOException e) {
+                       LOG.error("There was an error, cannot open " + 
endpoint.getLocalPath() + " repository");
+                       e.printStackTrace();
+               }
+               return repo;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/288d3bde/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitProducerTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitProducerTest.java
 
b/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitProducerTest.java
index 18965c8..f911648 100755
--- 
a/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitProducerTest.java
+++ 
b/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitProducerTest.java
@@ -24,17 +24,22 @@ import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.git.GitConstants;
 import org.apache.camel.test.junit4.CamelTestSupport;
+import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.Status;
 import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
 import org.junit.Test;
 
-public class GitProducerTest extends CamelTestSupport{
+public class GitProducerTest extends CamelTestSupport {
 
-       private final static String GIT_LOCAL_REPO = "pippo";
+       private final static String GIT_LOCAL_REPO = "testRepo";
        private final static String FILENAME_TO_ADD = "filetest.txt";
+       private final static String COMMIT_MESSAGE = "Test commit";
+       private final static String COMMIT_MESSAGE_BRANCH = "Test commit on a 
branch";
+       private final static String BRANCH_TEST = "testBranch";
        
     @Override
     public void setUp() throws Exception {
@@ -88,6 +93,99 @@ public class GitProducerTest extends CamelTestSupport{
         repository.close();
     }
     
+    @Test
+    public void commitTest() throws Exception {
+
+       Repository repository = getTestRepository();
+        
+        File fileToAdd = new File(GIT_LOCAL_REPO, FILENAME_TO_ADD);
+        fileToAdd.createNewFile();
+        
+        template.send("direct:add", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_FILE_NAME, 
FILENAME_TO_ADD);
+            }
+        });
+        File gitDir = new File(GIT_LOCAL_REPO, ".git");
+        assertEquals(gitDir.exists(), true);
+        
+        Status status = new Git(repository).status().call();
+        assertTrue(status.getAdded().contains(FILENAME_TO_ADD));
+        
+        template.send("direct:commit", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_COMMIT_MESSAGE, 
COMMIT_MESSAGE);
+            }
+        });
+        Iterable<RevCommit> logs = new Git(repository).log()
+                .call();
+        int count = 0;
+        for (RevCommit rev : logs) {
+            assertEquals(rev.getShortMessage(), COMMIT_MESSAGE);
+            count++;
+        }
+        assertEquals(count, 1);
+        repository.close();
+    }
+    
+    @Test
+    public void commitBranchTest() throws Exception {
+
+       Repository repository = getTestRepository();
+        
+        File fileToAdd = new File(GIT_LOCAL_REPO, FILENAME_TO_ADD);
+        fileToAdd.createNewFile();
+        
+        template.send("direct:add", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_FILE_NAME, 
FILENAME_TO_ADD);
+            }
+        });
+        File gitDir = new File(GIT_LOCAL_REPO, ".git");
+        assertEquals(gitDir.exists(), true);
+        
+        Status status = new Git(repository).status().call();
+        assertTrue(status.getAdded().contains(FILENAME_TO_ADD));
+        
+        template.send("direct:commit", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_COMMIT_MESSAGE, 
COMMIT_MESSAGE);
+            }
+        });
+        Iterable<RevCommit> logs = new Git(repository).log()
+                .call();
+        int count = 0;
+        for (RevCommit rev : logs) {
+            assertEquals(rev.getShortMessage(), COMMIT_MESSAGE);
+            count++;
+        }
+        assertEquals(count, 1);
+        
+        Git git = new Git(repository);
+        git.checkout().setCreateBranch(true).setName(BRANCH_TEST).
+        setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM).call();
+        
+        template.send("direct:commit-branch", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_COMMIT_MESSAGE, 
COMMIT_MESSAGE_BRANCH);
+            }
+        });
+        logs = git.log().call();
+        count = 0;
+        for (RevCommit rev : logs) {
+               if (count == 0) assertEquals(rev.getShortMessage(), 
COMMIT_MESSAGE_BRANCH);
+               if (count == 1) assertEquals(rev.getShortMessage(), 
COMMIT_MESSAGE);
+            count++;
+        }
+        assertEquals(count, 2);
+        repository.close();
+    }
+    
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {            
@@ -99,13 +197,17 @@ public class GitProducerTest extends CamelTestSupport{
                         
.to("git://https://github.com/oscerd/json-webserver-example.git?localPath="; + 
GIT_LOCAL_REPO + "&operation=init");
                 from("direct:add")
                         
.to("git://https://github.com/oscerd/json-webserver-example.git?localPath="; + 
GIT_LOCAL_REPO + "&operation=add");
+                from("direct:commit")
+                        
.to("git://https://github.com/oscerd/json-webserver-example.git?localPath="; + 
GIT_LOCAL_REPO + "&operation=commit");
+                from("direct:commit-branch")
+                        
.to("git://https://github.com/oscerd/json-webserver-example.git?localPath="; + 
GIT_LOCAL_REPO + "&operation=commit&branchName=" + BRANCH_TEST);
             } 
         };
     }
     
     private Repository getTestRepository() throws IOException, 
IllegalStateException, GitAPIException {
         File gitRepo = new File(GIT_LOCAL_REPO, ".git");
-        Git.init().setDirectory(new File(GIT_LOCAL_REPO,"")).call();
+        Git.init().setDirectory(new 
File(GIT_LOCAL_REPO,"")).setBare(false).call();
         // now open the resulting repository with a FileRepositoryBuilder
         FileRepositoryBuilder builder = new FileRepositoryBuilder();
         Repository repo = builder.setGitDir(gitRepo)

Reply via email to