CAMEL-10159: github endpoint to support creating issues

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

Branch: refs/heads/master
Commit: d619bb70178968ad7a9a6a67e49d9efab4ddbb19
Parents: 1153beb
Author: Andrea Cosentino <anco...@gmail.com>
Authored: Thu Jul 21 11:06:20 2016 +0200
Committer: Andrea Cosentino <anco...@gmail.com>
Committed: Thu Jul 21 11:31:04 2016 +0200

----------------------------------------------------------------------
 .../camel/component/github/GitHubEndpoint.java  |  3 +
 .../camel/component/github/GitHubType.java      |  2 +-
 .../github/producer/CreateIssueProducer.java    | 63 ++++++++++++++++
 .../producer/CreateIssueProducerTest.java       | 79 ++++++++++++++++++++
 .../github/services/MockIssueService.java       | 19 +++++
 5 files changed, 165 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/d619bb70/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 a76fee4..2dd757a 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
@@ -24,6 +24,7 @@ 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.CreateIssueProducer;
 import org.apache.camel.component.github.producer.GetCommitFileProducer;
 import org.apache.camel.component.github.producer.PullRequestCommentProducer;
 import org.apache.camel.component.github.producer.PullRequestFilesProducer;
@@ -97,6 +98,8 @@ public class GitHubEndpoint extends DefaultEndpoint {
             return new PullRequestFilesProducer(this);
         } else if (type == GitHubType.GETCOMMITFILE) {
             return new GetCommitFileProducer(this);
+        } else if (type == GitHubType.CREATEISSUE) {
+            return new CreateIssueProducer(this);
         }
         throw new IllegalArgumentException("Cannot create producer with type " 
+ type);
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/d619bb70/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubType.java
----------------------------------------------------------------------
diff --git 
a/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubType.java
 
b/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubType.java
index cc831d0..14269f7 100644
--- 
a/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubType.java
+++ 
b/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubType.java
@@ -19,6 +19,6 @@ package org.apache.camel.component.github;
 public enum GitHubType {
 
     CLOSEPULLREQUEST, PULLREQUESTCOMMENT, COMMIT, PULLREQUEST, TAG, 
PULLREQUESTSTATE,
-    PULLREQUESTFILES, GETCOMMITFILE
+    PULLREQUESTFILES, GETCOMMITFILE, CREATEISSUE
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/d619bb70/components/camel-github/src/main/java/org/apache/camel/component/github/producer/CreateIssueProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-github/src/main/java/org/apache/camel/component/github/producer/CreateIssueProducer.java
 
b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/CreateIssueProducer.java
new file mode 100644
index 0000000..337fe54
--- /dev/null
+++ 
b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/CreateIssueProducer.java
@@ -0,0 +1,63 @@
+/**
+ * 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 org.apache.camel.Exchange;
+import org.apache.camel.component.github.GitHubEndpoint;
+import org.apache.camel.spi.Registry;
+import org.apache.camel.util.ObjectHelper;
+import org.eclipse.egit.github.core.Issue;
+import org.eclipse.egit.github.core.service.IssueService;
+
+/**
+ * Producer endpoint that closes a pull request.  The endpoint requires the 
"GitHubPullRequest" header,
+ * identifying the pull request number (integer).
+ */
+public class CreateIssueProducer extends AbstractGitHubProducer {
+    
+    private IssueService issueService;
+
+    public CreateIssueProducer(GitHubEndpoint endpoint) throws Exception {
+        super(endpoint);
+        
+        Registry registry = endpoint.getCamelContext().getRegistry();
+        Object service = registry.lookupByName("githubIssuesService");
+        if (service != null) {
+               issueService = (IssueService) service;
+        } else {
+               issueService = new IssueService();
+        }
+        initService(issueService);
+    }
+
+    public void process(Exchange exchange) throws Exception {
+       Issue issue = new Issue();
+        String issueTitle = exchange.getIn().getHeader("GitHubIssueTitle", 
String.class);
+        if (ObjectHelper.isEmpty(issueTitle)) {
+            throw new IllegalArgumentException("Issue Title must be specified 
to create an issue");
+        }
+        issue.setTitle(issueTitle);
+        issue.setBody(exchange.getIn().getBody(String.class));
+        
+        Issue finalIssue = issueService.createIssue(getRepository(), issue);
+        
+        // copy the header of in message to the out message
+        exchange.getOut().copyFrom(exchange.getIn());
+        exchange.getOut().setBody(finalIssue);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/d619bb70/components/camel-github/src/test/java/org/apache/camel/component/github/producer/CreateIssueProducerTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-github/src/test/java/org/apache/camel/component/github/producer/CreateIssueProducerTest.java
 
b/components/camel-github/src/test/java/org/apache/camel/component/github/producer/CreateIssueProducerTest.java
new file mode 100644
index 0000000..14508a0
--- /dev/null
+++ 
b/components/camel-github/src/test/java/org/apache/camel/component/github/producer/CreateIssueProducerTest.java
@@ -0,0 +1,79 @@
+/**
+ * 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.Map;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.github.GitHubComponent;
+import org.apache.camel.component.github.GitHubComponentTestBase;
+import org.eclipse.egit.github.core.Issue;
+import org.eclipse.egit.github.core.Repository;
+import org.junit.Test;
+
+public class CreateIssueProducerTest extends GitHubComponentTestBase {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+
+            @Override
+            public void configure() throws Exception {
+                context.addComponent("github", new GitHubComponent());
+                from("direct:createIssue")
+                        .process(new MockPullRequestStateProducerProcessor())
+                        
.to("github://createissue?state=success&username=someguy&password=apassword&repoOwner=anotherguy&repoName=somerepo");
+            } // end of configure
+
+
+        };
+    }
+
+    @Test
+    public void testCreateIssue() throws Exception {
+       Repository repository = new Repository();
+
+        Endpoint issueProducerEndpoint = 
getMandatoryEndpoint("direct:createIssue");
+        Exchange exchange = issueProducerEndpoint.createExchange();
+        String issueBody = "There's an error";
+        exchange.getIn().setBody(issueBody);
+        template.send(issueProducerEndpoint, exchange);
+
+        Thread.sleep(1 * 1000);
+
+        // Verify that the mock pull request service received this comment.
+        Issue issue = issueService.getIssue(repository, 1);
+        assertEquals("Error", issue.getTitle());
+        assertEquals("There's an error", issue.getBody());
+    }
+
+
+    public class MockPullRequestStateProducerProcessor implements Processor {
+        @Override
+        public void process(Exchange exchange) throws Exception {
+            Message in = exchange.getIn();
+            Map<String, Object> headers = in.getHeaders();
+            headers.put("GitHubIssueTitle", "Error");
+        }
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/d619bb70/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockIssueService.java
----------------------------------------------------------------------
diff --git 
a/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockIssueService.java
 
b/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockIssueService.java
index dd7b376..a3565ba 100644
--- 
a/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockIssueService.java
+++ 
b/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockIssueService.java
@@ -22,6 +22,7 @@ import java.util.List;
 
 import org.eclipse.egit.github.core.Comment;
 import org.eclipse.egit.github.core.IRepositoryIdProvider;
+import org.eclipse.egit.github.core.Issue;
 import org.eclipse.egit.github.core.service.IssueService;
 
 public class MockIssueService extends IssueService {
@@ -44,4 +45,22 @@ public class MockIssueService extends IssueService {
         Comment addedComment = mockPullRequestService.addComment((long) 
issueNumber, commentText);
         return addedComment;
     }
+    
+    @Override
+    public Issue createIssue(IRepositoryIdProvider repository, Issue issue) {
+        Issue finalIssue = new Issue();
+        issue.setBody("There's an error");
+        issue.setTitle("Error");
+        issue.setId(1L);
+        return finalIssue;
+    }
+    
+    @Override
+    public Issue getIssue(IRepositoryIdProvider repository, String 
issueNumber) {
+       Issue issue = new Issue();
+        issue.setBody("There's an error");
+        issue.setTitle("Error");
+        issue.setId(1L);
+        return issue;
+    }
 }

Reply via email to