camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [11/23] camel git commit: CAMEL-7982: camel-git - A generic git component, add Push and Pull operations
Date Sat, 18 Jul 2015 09:09:28 GMT
CAMEL-7982: camel-git - A generic git component, add Push and Pull operations


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

Branch: refs/heads/master
Commit: f3731ed4c1058c97f6471114715e737d5b1d1028
Parents: e224e8e
Author: Andrea Cosentino <ancosen@gmail.com>
Authored: Sat Jul 18 11:06:38 2015 +0200
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Sat Jul 18 11:08:15 2015 +0200

----------------------------------------------------------------------
 .../camel/component/git/GitOperation.java       |  2 +
 .../apache/camel/component/git/GitProducer.java | 60 +++++++++++++
 .../github/producer/GitProducerTest.java        | 56 +-----------
 .../github/producer/GitRemoteProducerTest.java  | 91 ++++++++++++++++++++
 .../github/producer/GitTestSupport.java         | 86 ++++++++++++++++++
 5 files changed, 243 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/f3731ed4/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 df7bb0f..8d7b69a 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
@@ -28,4 +28,6 @@ public interface GitOperation {
     public final static String DELETE_BRANCH_OPERATION = "deleteBranch";
     public final static String STATUS_OPERATION = "status";
     public final static String LOG_OPERATION = "log";
+    public final static String PUSH_OPERATION = "push";
+    public final static String PULL_OPERATION = "pull";
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/f3731ed4/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 e931d54..42a55e6 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
@@ -7,10 +7,14 @@ 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.api.PullResult;
 import org.eclipse.jgit.api.Status;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+import org.eclipse.jgit.transport.PushResult;
+import org.eclipse.jgit.transport.RefSpec;
+import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -78,6 +82,14 @@ public class GitProducer extends DefaultProducer{
             case GitOperation.LOG_OPERATION:
                 doLog(exchange, operation, repo);
                 break;
+                
+            case GitOperation.PUSH_OPERATION:
+                doPush(exchange, operation, repo);
+                break;
+                            
+            case GitOperation.PULL_OPERATION:
+                doPull(exchange, operation, repo);
+                break;
 	    }
 	    repo.close();
 	}
@@ -257,6 +269,54 @@ public class GitProducer extends DefaultProducer{
         exchange.getOut().setBody(revCommit);
     }
     
+    protected void doPush(Exchange exchange, String operation, Repository repo) {
+        Git git = null;
+        Iterable<PushResult> result = null;
+        try {
+            git = new Git(repo);
+            if (ObjectHelper.isEmpty(endpoint.getRemotePath())) {
+                throw new IllegalArgumentException("Remote path must be specified to execute
" + operation);
+            } 
+            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+            } 
+            if (ObjectHelper.isNotEmpty(endpoint.getUsername()) && ObjectHelper.isNotEmpty(endpoint.getPassword()))
{
+                UsernamePasswordCredentialsProvider credentials = new UsernamePasswordCredentialsProvider(endpoint.getUsername(),
endpoint.getPassword());
+                result = git.push().setCredentialsProvider(credentials).setRemote(endpoint.getRemotePath()).call();
+            } else {
+                result = git.push().setRemote(endpoint.getRemotePath()).call();
+            }
+                } catch (Exception e) {
+                        LOG.error("There was an error in Git " + operation + " operation");
+                        e.printStackTrace();
+                }
+        exchange.getOut().setBody(result);
+    }
+    
+    protected void doPull(Exchange exchange, String operation, Repository repo) {
+        Git git = null;
+        PullResult result = null;
+        try {
+            git = new Git(repo);
+            if (ObjectHelper.isEmpty(endpoint.getRemotePath())) {
+                throw new IllegalArgumentException("Remote path must be specified to execute
" + operation);
+            } 
+            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+            } 
+            if (ObjectHelper.isNotEmpty(endpoint.getUsername()) && ObjectHelper.isNotEmpty(endpoint.getPassword()))
{
+                UsernamePasswordCredentialsProvider credentials = new UsernamePasswordCredentialsProvider(endpoint.getUsername(),
endpoint.getPassword());
+                result = git.pull().setCredentialsProvider(credentials).setRemote(endpoint.getRemotePath()).call();
+            } else {
+                result = git.pull().setRemote(endpoint.getRemotePath()).call();
+            }
+                } catch (Exception e) {
+                        LOG.error("There was an error in Git " + operation + " operation");
+                        e.printStackTrace();
+                }
+        exchange.getOut().setBody(result);
+    }
+    
     private Repository getLocalRepository(){
         FileRepositoryBuilder builder = new FileRepositoryBuilder();
         Repository repo = null;

http://git-wip-us.apache.org/repos/asf/camel/blob/f3731ed4/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 8ad0cb6..cee4011 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
@@ -33,33 +33,11 @@ import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+import org.eclipse.jgit.transport.PushResult;
+import org.junit.Ignore;
 import org.junit.Test;
 
-public class GitProducerTest extends CamelTestSupport {
-
-	private final static String GIT_LOCAL_REPO = "testRepo";
-	private final static String FILENAME_TO_ADD = "filetest.txt";
-	private final static String FILENAME_BRANCH_TO_ADD = "filetest1.txt";
-	private final static String COMMIT_MESSAGE = "Test commit";
-        private final static String COMMIT_MESSAGE_ALL = "Test commit all";
-	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 {
-    	super.setUp();
-        File localPath = File.createTempFile(GIT_LOCAL_REPO, "");
-        localPath.delete();
-        File path = new File(GIT_LOCAL_REPO);
-        path.deleteOnExit();
-    }
-    
-    @Override
-    public void tearDown() throws Exception {
-    	super.tearDown();
-        File path = new File(GIT_LOCAL_REPO);
-        deleteDirectory(path);
-    }
+public class GitProducerTest extends GitTestSupport {
     
     @Test
     public void cloneTest() throws Exception {
@@ -742,31 +720,5 @@ public class GitProducerTest extends CamelTestSupport {
             } 
         };
     }
-    
-    private Repository getTestRepository() throws IOException, IllegalStateException, GitAPIException
{
-        File gitRepo = new File(GIT_LOCAL_REPO, ".git");
-        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)
-                .readEnvironment() // scan environment GIT_* variables
-                .findGitDir() // scan up the file system tree
-                .build();
-        return repo;
-    }
-    
-    static public boolean deleteDirectory(File path) {
-        if( path.exists() ) {
-          File[] files = path.listFiles();
-          for(int i=0; i<files.length; i++) {
-             if(files[i].isDirectory()) {
-               deleteDirectory(files[i]);
-             }
-             else {
-               files[i].delete();
-             }
-          }
-        }
-        return( path.delete() );
-      }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/f3731ed4/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitRemoteProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitRemoteProducerTest.java
b/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitRemoteProducerTest.java
new file mode 100644
index 0000000..085fa1c
--- /dev/null
+++ b/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitRemoteProducerTest.java
@@ -0,0 +1,91 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.github.producer;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.camel.Exchange;
+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.PullResult;
+import org.eclipse.jgit.api.Status;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+import org.eclipse.jgit.transport.PushResult;
+import org.junit.Ignore;
+import org.junit.Test;
+
+public class GitRemoteProducerTest extends GitTestSupport {
+    
+    @Ignore("Require a remote git repository")
+    @Test
+    public void pushTest() 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<PushResult> result = template.requestBody("direct:push", "", Iterable.class);
+        
+        repository.close();
+    }
+   
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {            
+            @Override
+            public void configure() throws Exception {
+                from("direct:add")
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=add");
+                from("direct:commit")
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=commit");
+                from("direct:push")
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=push&remotePath=remoteURL&username=xxx&password=xxx"
);
+            } 
+        };
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/f3731ed4/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitTestSupport.java
----------------------------------------------------------------------
diff --git a/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitTestSupport.java
b/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitTestSupport.java
new file mode 100644
index 0000000..0aa3946
--- /dev/null
+++ b/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitTestSupport.java
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.github.producer;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+
+public class GitTestSupport extends CamelTestSupport {
+
+    public final static String GIT_LOCAL_REPO = "testRepo";
+
+    public final static String FILENAME_TO_ADD = "filetest.txt";
+
+    public final static String FILENAME_BRANCH_TO_ADD = "filetest1.txt";
+
+    public final static String COMMIT_MESSAGE = "Test commit";
+
+    public final static String COMMIT_MESSAGE_ALL = "Test commit all";
+
+    public final static String COMMIT_MESSAGE_BRANCH = "Test commit on a branch";
+
+    public final static String BRANCH_TEST = "testBranch";
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        File localPath = File.createTempFile(GIT_LOCAL_REPO, "");
+        localPath.delete();
+        File path = new File(GIT_LOCAL_REPO);
+        path.deleteOnExit();
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        super.tearDown();
+        File path = new File(GIT_LOCAL_REPO);
+        deleteDirectory(path);
+    }
+      
+    protected Repository getTestRepository() throws IOException, IllegalStateException, GitAPIException
{
+        File gitRepo = new File(GIT_LOCAL_REPO, ".git");
+        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)
+                .readEnvironment() // scan environment GIT_* variables
+                .findGitDir() // scan up the file system tree
+                .build();
+        return repo;
+    }
+    
+    static public boolean deleteDirectory(File path) {
+        if( path.exists() ) {
+          File[] files = path.listFiles();
+          for(int i=0; i<files.length; i++) {
+             if(files[i].isDirectory()) {
+               deleteDirectory(files[i]);
+             }
+             else {
+               files[i].delete();
+             }
+          }
+        }
+        return( path.delete() );
+      }
+}


Mime
View raw message