CAMEL-7982: camel-git - A generic git component, add branches consumer
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2cbeee72 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2cbeee72 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2cbeee72 Branch: refs/heads/master Commit: 2cbeee721769e385743b4595ff796d0c293869ae Parents: d38b525 Author: Andrea Cosentino <[email protected]> Authored: Sat Jul 18 11:07:15 2015 +0200 Committer: Andrea Cosentino <[email protected]> Committed: Sat Jul 18 11:08:16 2015 +0200 ---------------------------------------------------------------------- .../apache/camel/component/git/GitEndpoint.java | 2 + .../git/consumer/GitBranchConsumer.java | 52 ++++++++++ .../camel/component/git/consumer/GitType.java | 2 +- .../component/git/consumer/GitConsumerTest.java | 101 ++++++++++++++++++- 4 files changed, 151 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/2cbeee72/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java b/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java index 79673e8..ce1a325 100644 --- a/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java +++ b/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java @@ -19,6 +19,7 @@ package org.apache.camel.component.git; import org.apache.camel.Consumer; import org.apache.camel.Processor; import org.apache.camel.Producer; +import org.apache.camel.component.git.consumer.GitBranchConsumer; import org.apache.camel.component.git.consumer.GitCommitConsumer; import org.apache.camel.component.git.consumer.GitTagConsumer; import org.apache.camel.component.git.consumer.GitType; @@ -62,6 +63,7 @@ public class GitEndpoint extends DefaultEndpoint { public Consumer createConsumer(Processor processor) throws Exception { if (type == GitType.COMMIT) return new GitCommitConsumer(this, processor); else if (type == GitType.TAG) return new GitTagConsumer(this, processor); + else if (type == GitType.BRANCH) return new GitBranchConsumer(this, processor); else throw new IllegalArgumentException("Cannot create producer with type " + type); } http://git-wip-us.apache.org/repos/asf/camel/blob/2cbeee72/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitBranchConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitBranchConsumer.java b/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitBranchConsumer.java new file mode 100644 index 0000000..d906d7a --- /dev/null +++ b/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitBranchConsumer.java @@ -0,0 +1,52 @@ +/** + * 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.git.consumer; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.component.git.GitEndpoint; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.revwalk.RevCommit; + +public class GitBranchConsumer extends AbstractGitConsumer { + + private List used = new ArrayList(); + + public GitBranchConsumer(GitEndpoint endpoint, Processor processor) { + super(endpoint, processor); + } + + @Override + protected int poll() throws Exception { + int count = 0; + List<Ref> call = getGit().branchList().call(); + for (Ref ref : call) { + if (!used.contains(ref.getName())) { + Exchange e = getEndpoint().createExchange(); + e.getOut().setBody(ref); + getProcessor().process(e); + used.add(ref.getName()); + count++; + } + } + return count; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/2cbeee72/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitType.java ---------------------------------------------------------------------- diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitType.java b/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitType.java index 4015445..ecc9774 100644 --- a/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitType.java +++ b/components/camel-git/src/main/java/org/apache/camel/component/git/consumer/GitType.java @@ -18,6 +18,6 @@ package org.apache.camel.component.git.consumer; public enum GitType { - COMMIT, TAG + COMMIT, TAG, BRANCH } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/2cbeee72/components/camel-git/src/test/java/org/apache/camel/component/git/consumer/GitConsumerTest.java ---------------------------------------------------------------------- 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 b61286d0..a080e23 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 @@ -17,6 +17,7 @@ package org.apache.camel.component.git.consumer; import java.io.File; +import java.util.List; import org.apache.camel.Exchange; import org.apache.camel.Processor; @@ -26,6 +27,7 @@ import org.apache.camel.component.git.GitTestSupport; import org.apache.camel.component.mock.MockEndpoint; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Status; +import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.junit.Test; @@ -36,7 +38,7 @@ public class GitConsumerTest extends GitTestSupport { public void commitConsumerTest() throws Exception { Repository repository = getTestRepository(); - MockEndpoint added = getMockEndpoint("mock:result"); + MockEndpoint added = getMockEndpoint("mock:result-commit"); File fileToAdd = new File(GIT_LOCAL_REPO, FILENAME_TO_ADD); fileToAdd.createNewFile(); @@ -107,10 +109,93 @@ public class GitConsumerTest extends GitTestSupport { public void tagConsumerTest() throws Exception { Repository repository = getTestRepository(); - MockEndpoint added = getMockEndpoint("mock:result"); + + 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); + } + }); + + Git git = new Git(repository); + + template.sendBody("direct:create-tag", ""); + + List<Ref> ref = git.tagList().call(); + boolean tagCreated = false; + for (Ref refInternal : ref) { + if (refInternal.getName().equals("refs/tags/" + TAG_TEST)) { + tagCreated = true; + } + } + assertEquals(tagCreated, true); + + MockEndpoint added = getMockEndpoint("mock:result-tag"); + + Thread.sleep(1 * 5000); + assertEquals(added.getExchanges().size(), 1); + repository.close(); + } + + @Test + public void branchConsumerTest() 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); + } + }); + + Git git = new Git(repository); + + template.sendBody("direct:create-branch", ""); + + List<Ref> ref = git.branchList().call(); + boolean branchCreated = false; + for (Ref refInternal : ref) { + if (refInternal.getName().equals("refs/heads/" + BRANCH_TEST)) { + branchCreated = true; + } + } + assertEquals(branchCreated, true); + + MockEndpoint added = getMockEndpoint("mock:result-branch"); Thread.sleep(1 * 5000); - assertEquals(added.getExchanges().size(), 0); + assertEquals(added.getExchanges().size(), 2); repository.close(); } @@ -127,10 +212,16 @@ public class GitConsumerTest extends GitTestSupport { .to("git://" + GIT_LOCAL_REPO + "?operation=add"); from("direct:commit") .to("git://" + GIT_LOCAL_REPO + "?operation=commit"); + from("direct:create-branch") + .to("git://" + GIT_LOCAL_REPO + "?operation=createBranch&branchName=" + BRANCH_TEST); + from("direct:create-tag") + .to("git://" + GIT_LOCAL_REPO + "?operation=createTag&tagName=" + TAG_TEST); from("git://" + GIT_LOCAL_REPO + "?type=commit") - .to("mock:result"); + .to("mock:result-commit"); from("git://" + GIT_LOCAL_REPO + "?type=tag") - .to("mock:result"); + .to("mock:result-tag"); + from("git://" + GIT_LOCAL_REPO + "?type=branch") + .to("mock:result-branch"); } }; }
