camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [1/2] git commit: CAMEL-7777 created camel-github producer to close pull requests - automatically set GitHubPullRequest header on pull request consumers
Date Fri, 10 Oct 2014 15:33:13 GMT
Repository: camel
Updated Branches:
  refs/heads/master 627ffe78b -> c3715c4ce


CAMEL-7777 created camel-github producer to close pull requests - automatically set GitHubPullRequest
header on pull request consumers


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

Branch: refs/heads/master
Commit: 3924c286bc31c09a2c5be2c3a0c6ce7b65e6c5a6
Parents: 627ffe7
Author: Brett Meyer <brett@3riverdev.com>
Authored: Tue Sep 30 13:41:55 2014 -0400
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Fri Oct 10 23:19:12 2014 +0800

----------------------------------------------------------------------
 .../camel/component/github/GitHubEndpoint.java  |  3 ++
 .../consumer/PullRequestCommentConsumer.java    | 12 +++++
 .../github/consumer/PullRequestConsumer.java    |  4 ++
 .../producer/ClosePullRequestProducer.java      | 56 ++++++++++++++++++++
 .../component/github/producer/ProducerType.java |  2 +-
 5 files changed, 76 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3924c286/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubEndpoint.java
b/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubEndpoint.java
index d12afec..d8ecd7d 100644
--- a/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubEndpoint.java
+++ b/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubEndpoint.java
@@ -26,6 +26,7 @@ import org.apache.camel.component.github.consumer.ConsumerType;
 import org.apache.camel.component.github.consumer.PullRequestCommentConsumer;
 import org.apache.camel.component.github.consumer.PullRequestConsumer;
 import org.apache.camel.component.github.consumer.TagConsumer;
+import org.apache.camel.component.github.producer.ClosePullRequestProducer;
 import org.apache.camel.component.github.producer.ProducerType;
 import org.apache.camel.component.github.producer.PullRequestCommentProducer;
 import org.apache.camel.impl.DefaultEndpoint;
@@ -78,6 +79,8 @@ public class GitHubEndpoint extends DefaultEndpoint {
         
         if (uriSplit.length > 0) {
             switch (ProducerType.fromUri(uriSplit[0])) {
+            case CLOSEPULLREQUEST:
+                return new ClosePullRequestProducer(this);
             case PULLREQUESTCOMMENT:
                 return new PullRequestCommentProducer(this);
             default:

http://git-wip-us.apache.org/repos/asf/camel/blob/3924c286/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestCommentConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestCommentConsumer.java
b/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestCommentConsumer.java
index 0c76463..f6dc9ec 100644
--- a/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestCommentConsumer.java
+++ b/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestCommentConsumer.java
@@ -17,7 +17,9 @@
 package org.apache.camel.component.github.consumer;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Stack;
 
 import org.apache.camel.Exchange;
@@ -79,6 +81,10 @@ public class PullRequestCommentConsumer extends AbstractGitHubConsumer
{
 
     @Override
     protected int poll() throws Exception {
+        // Do this here, rather than at the class level.  We only care about it for setting
the Exchange header, so
+        // there's no point growing memory over time.
+        Map<Long, PullRequest> commentIdToPullRequest = new HashMap<Long, PullRequest>();
+        
         List<PullRequest> pullRequests = pullRequestService.getPullRequests(getRepository(),
"open");
         // In the end, we want comments oldest to newest.
         Stack<Comment> newComments = new Stack<Comment>();
@@ -88,6 +94,7 @@ public class PullRequestCommentConsumer extends AbstractGitHubConsumer {
                 if (!commentIds.contains(comment.getId())) {
                     newComments.add(comment);
                     commentIds.add(comment.getId());
+                    commentIdToPullRequest.put(comment.getId(), pullRequest);
                 }
             }
             List<Comment> comments = issueService.getComments(getRepository(), pullRequest.getNumber());
@@ -95,6 +102,7 @@ public class PullRequestCommentConsumer extends AbstractGitHubConsumer
{
                 if (!commentIds.contains(comment.getId())) {
                     newComments.add(comment);
                     commentIds.add(comment.getId());
+                    commentIdToPullRequest.put(comment.getId(), pullRequest);
                 }
             }
         }
@@ -103,6 +111,10 @@ public class PullRequestCommentConsumer extends AbstractGitHubConsumer
{
             Comment newComment = newComments.pop();
             Exchange e = getEndpoint().createExchange();
             e.getIn().setBody(newComment);
+            
+            // Required by the producers.  Set it here for convenience.
+            e.getIn().setHeader("GitHubPullRequest", commentIdToPullRequest.get(newComment.getId()));
+            
             getProcessor().process(e);
         }
         return newComments.size();

http://git-wip-us.apache.org/repos/asf/camel/blob/3924c286/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestConsumer.java
b/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestConsumer.java
index 53773c3..0d62f6d 100644
--- a/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestConsumer.java
+++ b/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestConsumer.java
@@ -77,6 +77,10 @@ public class PullRequestConsumer extends AbstractGitHubConsumer {
             PullRequest newPullRequest = newPullRequests.pop();
             Exchange e = getEndpoint().createExchange();
             e.getIn().setBody(newPullRequest);
+            
+            // Required by the producers.  Set it here for convenience.
+            e.getIn().setHeader("GitHubPullRequest", newPullRequest.getNumber());
+            
             getProcessor().process(e);
         }
         return newPullRequests.size();

http://git-wip-us.apache.org/repos/asf/camel/blob/3924c286/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ClosePullRequestProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ClosePullRequestProducer.java
b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ClosePullRequestProducer.java
new file mode 100644
index 0000000..cf50fd9
--- /dev/null
+++ b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ClosePullRequestProducer.java
@@ -0,0 +1,56 @@
+/**
+ * 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.util.Calendar;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.component.github.GitHubEndpoint;
+import org.eclipse.egit.github.core.PullRequest;
+import org.eclipse.egit.github.core.service.PullRequestService;
+
+/**
+ * Producer endpoint that closes a pull request.  The endpoint requires the "GitHubPullRequest"
header,
+ * identifying the pull request number (integer).
+ */
+public class ClosePullRequestProducer extends AbstractGitHubProducer {
+    
+    private PullRequestService pullRequestService;
+
+    public ClosePullRequestProducer(GitHubEndpoint endpoint) throws Exception {
+        super(endpoint);
+        
+        pullRequestService = new PullRequestService();
+        initService(pullRequestService);
+    }
+
+    public void process(Exchange exchange) throws Exception {
+        Integer pullRequestNumber = exchange.getIn().getHeader("GitHubPullRequest", Integer.class);
+        PullRequest pullRequest = pullRequestService.getPullRequest(getRepository(), pullRequestNumber);
+        pullRequest.setState("closed");
+        pullRequest.setClosedAt(Calendar.getInstance().getTime());
+        pullRequest = pullRequestService.editPullRequest(getRepository(), pullRequest);
+        
+        // support InOut
+        if (exchange.getPattern().isOutCapable()) {
+            // copy the header of in message to the out message
+            exchange.getOut().copyFrom(exchange.getIn());
+            exchange.getOut().setBody(pullRequest);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/3924c286/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ProducerType.java
----------------------------------------------------------------------
diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ProducerType.java
b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ProducerType.java
index 009ea42..04c59a5 100644
--- a/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ProducerType.java
+++ b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ProducerType.java
@@ -18,7 +18,7 @@ package org.apache.camel.component.github.producer;
 
 public enum ProducerType {
 
-    PULLREQUESTCOMMENT, UNKNOWN;
+    CLOSEPULLREQUEST, PULLREQUESTCOMMENT, UNKNOWN;
 
     public static ProducerType fromUri(String uri) {
         for (ProducerType producerType : ProducerType.values()) {


Mime
View raw message