You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2020/11/05 10:40:41 UTC

[camel] 01/05: CAMEL-15814 - Camel-Git: Commit consumer should be able to be based on specific branch

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 4296f8734414960a0a02cbe0f1f2229e75bc55dd
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Thu Nov 5 09:30:54 2020 +0100

    CAMEL-15814 - Camel-Git: Commit consumer should be able to be based on specific branch
---
 .../git/consumer/AbstractGitConsumer.java          |  2 +-
 .../component/git/consumer/GitCommitConsumer.java  |  8 +++++-
 .../component/git/consumer/GitConsumerTest.java    | 29 +++++++++++++++++++++-
 3 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/AbstractGitConsumer.java b/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/AbstractGitConsumer.java
index 0fb735e..5916642 100644
--- a/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/AbstractGitConsumer.java
+++ b/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/AbstractGitConsumer.java
@@ -79,6 +79,6 @@ public abstract class AbstractGitConsumer extends ScheduledPollConsumer {
         return git;
     }
 
-    @Override
+	@Override
     protected abstract int poll() throws Exception;
 }
diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitCommitConsumer.java b/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitCommitConsumer.java
index 6712c66..b016175 100644
--- a/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitCommitConsumer.java
+++ b/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitCommitConsumer.java
@@ -22,6 +22,7 @@ import java.util.List;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.component.git.GitEndpoint;
+import org.apache.camel.util.ObjectHelper;
 import org.eclipse.jgit.revwalk.RevCommit;
 
 public class GitCommitConsumer extends AbstractGitConsumer {
@@ -35,7 +36,12 @@ public class GitCommitConsumer extends AbstractGitConsumer {
     @Override
     protected int poll() throws Exception {
         int count = 0;
-        Iterable<RevCommit> commits = getGit().log().all().call();
+        Iterable<RevCommit> commits;
+        if (ObjectHelper.isNotEmpty(((GitEndpoint)getEndpoint()).getBranchName())) {
+            commits = getGit().log().add(getGit().getRepository().resolve(((GitEndpoint)getEndpoint()).getBranchName())).call();
+        } else {
+        	commits = getGit().log().all().call();	
+        }
         for (RevCommit commit : commits) {
             if (!commitsConsumed.contains(commit.getId())) {
                 Exchange e = getEndpoint().createExchange();
diff --git a/components/camel-git/src/test/java/org/apache/camel/component/git/consumer/GitConsumerTest.java b/components/camel-git/src/test/java/org/apache/camel/component/git/consumer/GitConsumerTest.java
index e5574f1..d7d6282 100644
--- a/components/camel-git/src/test/java/org/apache/camel/component/git/consumer/GitConsumerTest.java
+++ b/components/camel-git/src/test/java/org/apache/camel/component/git/consumer/GitConsumerTest.java
@@ -66,6 +66,32 @@ public class GitConsumerTest extends GitTestSupport {
         assertEquals("Test test Commit", ex1.getMessage().getBody(RevCommit.class).getShortMessage());
         git.close();
     }
+    
+    @Test
+    public void commitConsumerNotExistingBranchTest() throws Exception {
+        // Init
+        MockEndpoint mockResultCommit = getMockEndpoint("mock:result-commit-notexistent");
+        mockResultCommit.expectedMessageCount(0);
+        Git git = getGitTestRepository();
+        File gitDir = new File(gitLocalRepo, ".git");
+        assertEquals(true, gitDir.exists());
+        File fileToAdd = new File(gitLocalRepo, filenameToAdd);
+        fileToAdd.createNewFile();
+        git.add().addFilepattern(filenameToAdd).call();
+        Status status = git.status().call();
+        assertTrue(status.getAdded().contains(filenameToAdd));
+        git.commit().setMessage(commitMessage).call();
+        File fileToAdd1 = new File(gitLocalRepo, filenameBranchToAdd);
+        fileToAdd1.createNewFile();
+        git.add().addFilepattern(filenameBranchToAdd).call();
+        status = git.status().call();
+        assertTrue(status.getAdded().contains(filenameBranchToAdd));
+        git.commit().setMessage("Test test Commit").call();
+        validateGitLogs(git, "Test test Commit", commitMessage);
+        // Test
+        mockResultCommit.assertIsSatisfied();
+        git.close();
+    }
 
     @Test
     public void tagConsumerTest() throws Exception {
@@ -148,7 +174,8 @@ public class GitConsumerTest extends GitTestSupport {
                 from("direct:commit").to("git://" + gitLocalRepo + "?operation=commit");
                 from("direct:create-branch").to("git://" + gitLocalRepo + "?operation=createBranch&branchName=" + branchTest);
                 from("direct:create-tag").to("git://" + gitLocalRepo + "?operation=createTag&tagName=" + tagTest);
-                from("git://" + gitLocalRepo + "?type=commit").to("mock:result-commit");
+                from("git://" + gitLocalRepo + "?type=commit&branchName=master").to("mock:result-commit");
+                from("git://" + gitLocalRepo + "?type=commit&branchName=notexisting").to("mock:result-commit-notexistent");
                 from("git://" + gitLocalRepo + "?type=tag").to("mock:result-tag");
                 from("git://" + gitLocalRepo + "?type=branch").to("mock:result-branch");
             }