CAMEL-7982: camel-git - A generic git component, add createTag operation
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/bc958064 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/bc958064 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/bc958064 Branch: refs/heads/master Commit: bc9580640cb2a15709041f49ea105d28a3968387 Parents: 828dc8b Author: Andrea Cosentino <[email protected]> Authored: Sat Jul 18 11:07:02 2015 +0200 Committer: Andrea Cosentino <[email protected]> Committed: Sat Jul 18 11:08:16 2015 +0200 ---------------------------------------------------------------------- .../apache/camel/component/git/GitEndpoint.java | 11 +++ .../component/git/producer/GitOperation.java | 1 + .../component/git/producer/GitProducer.java | 16 ++++ .../camel/component/git/GitTestSupport.java | 90 ++++++++++++++++++++ .../component/git/consumer/GitConsumerTest.java | 3 +- .../component/git/producer/GitProducerTest.java | 51 +++++++++-- .../git/producer/GitRemoteProducerTest.java | 10 +-- .../component/git/producer/GitTestSupport.java | 88 ------------------- 8 files changed, 165 insertions(+), 105 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/bc958064/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 1b06092..79673e8 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 @@ -36,6 +36,8 @@ public class GitEndpoint extends DefaultEndpoint { private String localPath; @UriPath private String branchName; + @UriPath + private String tagName; @UriPath(label = "consumer") private GitType type; @UriParam @@ -124,4 +126,13 @@ public class GitEndpoint extends DefaultEndpoint { public void setType(GitType type) { this.type = type; } + + public String getTagName() { + return tagName; + } + + public void setTagName(String tagName) { + this.tagName = tagName; + } + } http://git-wip-us.apache.org/repos/asf/camel/blob/bc958064/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitOperation.java ---------------------------------------------------------------------- diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitOperation.java b/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitOperation.java index de03fc7..d45c743 100644 --- a/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitOperation.java +++ b/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitOperation.java @@ -26,6 +26,7 @@ public interface GitOperation { public final static String COMMIT_ALL_OPERATION = "commitAll"; public final static String CREATE_BRANCH_OPERATION = "createBranch"; public final static String DELETE_BRANCH_OPERATION = "deleteBranch"; + public final static String CREATE_TAG_OPERATION = "createTag"; public final static String STATUS_OPERATION = "status"; public final static String LOG_OPERATION = "log"; public final static String PUSH_OPERATION = "push"; http://git-wip-us.apache.org/repos/asf/camel/blob/bc958064/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitProducer.java b/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitProducer.java index b9d88fe..f6eccf4 100644 --- a/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitProducer.java +++ b/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitProducer.java @@ -108,6 +108,10 @@ public class GitProducer extends DefaultProducer{ case GitOperation.PULL_OPERATION: doPull(exchange, operation); break; + + case GitOperation.CREATE_TAG_OPERATION: + doCreateTag(exchange, operation); + break; } } @@ -314,6 +318,18 @@ public class GitProducer extends DefaultProducer{ exchange.getOut().setBody(result); } + protected void doCreateTag(Exchange exchange, String operation) throws Exception { + if (ObjectHelper.isEmpty(endpoint.getTagName())) { + throw new IllegalArgumentException("Tag Name must be specified to execute " + operation); + } + try { + git.tag().setName(endpoint.getTagName()).call(); + } catch (Exception e) { + LOG.error("There was an error in Git " + operation + " operation"); + throw e; + } + } + private Repository getLocalRepository(){ FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repo = null; http://git-wip-us.apache.org/repos/asf/camel/blob/bc958064/components/camel-git/src/test/java/org/apache/camel/component/git/GitTestSupport.java ---------------------------------------------------------------------- diff --git a/components/camel-git/src/test/java/org/apache/camel/component/git/GitTestSupport.java b/components/camel-git/src/test/java/org/apache/camel/component/git/GitTestSupport.java new file mode 100644 index 0000000..ae763b3 --- /dev/null +++ b/components/camel-git/src/test/java/org/apache/camel/component/git/GitTestSupport.java @@ -0,0 +1,90 @@ +/** + * 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; + +import java.io.File; +import java.io.IOException; + +import org.apache.camel.EndpointInject; +import org.apache.camel.component.mock.MockEndpoint; +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"; + + public final static String TAG_TEST = "testTag"; + + @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() ); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/bc958064/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 21273e2..b61286d0 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 @@ -22,11 +22,10 @@ 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.component.git.producer.GitTestSupport; +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; http://git-wip-us.apache.org/repos/asf/camel/blob/bc958064/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitProducerTest.java b/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitProducerTest.java index 5a9c7f3..910d942 100755 --- a/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitProducerTest.java +++ b/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitProducerTest.java @@ -17,24 +17,19 @@ package org.apache.camel.component.git.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.apache.camel.component.git.GitTestSupport; 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.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 GitTestSupport { @@ -680,6 +675,48 @@ public class GitProducerTest extends GitTestSupport { repository.close(); } + @Test + public void createTagTest() 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-tag", ""); + + List<Ref> ref = git.tagList().call(); + boolean branchCreated = false; + for (Ref refInternal : ref) { + if (refInternal.getName().equals("refs/tags/" + TAG_TEST)) { + branchCreated = true; + } + } + assertEquals(branchCreated, true); + repository.close(); + } + @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @@ -717,6 +754,8 @@ public class GitProducerTest extends GitTestSupport { .to("git://" + GIT_LOCAL_REPO + "?operation=log"); from("direct:log-branch") .to("git://" + GIT_LOCAL_REPO + "?operation=log&branchName=" + BRANCH_TEST); + from("direct:create-tag") + .to("git://" + GIT_LOCAL_REPO + "?operation=createTag&tagName=" + TAG_TEST); } }; } http://git-wip-us.apache.org/repos/asf/camel/blob/bc958064/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitRemoteProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitRemoteProducerTest.java b/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitRemoteProducerTest.java index a82eba7..81ba158 100644 --- a/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitRemoteProducerTest.java +++ b/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitRemoteProducerTest.java @@ -17,23 +17,15 @@ package org.apache.camel.component.git.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.apache.camel.component.git.GitTestSupport; 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; http://git-wip-us.apache.org/repos/asf/camel/blob/bc958064/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitTestSupport.java ---------------------------------------------------------------------- diff --git a/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitTestSupport.java b/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitTestSupport.java deleted file mode 100644 index f0cfacb..0000000 --- a/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitTestSupport.java +++ /dev/null @@ -1,88 +0,0 @@ -/** - * 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.producer; - -import java.io.File; -import java.io.IOException; - -import org.apache.camel.EndpointInject; -import org.apache.camel.component.mock.MockEndpoint; -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() ); - } -}
