camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [1/4] camel git commit: CAMEL-8497 Add support for setting a commit status, getting list of files associated with PR and retrieving a specific file content.
Date Thu, 19 Mar 2015 06:57:10 GMT
Repository: camel
Updated Branches:
  refs/heads/master 392455dd7 -> 70485e975


http://git-wip-us.apache.org/repos/asf/camel/blob/c458f533/components/camel-github/src/test/java/org/apache/camel/component/github/producer/PullRequestStateProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/producer/PullRequestStateProducerTest.java
b/components/camel-github/src/test/java/org/apache/camel/component/github/producer/PullRequestStateProducerTest.java
new file mode 100644
index 0000000..1aabe95
--- /dev/null
+++ b/components/camel-github/src/test/java/org/apache/camel/component/github/producer/PullRequestStateProducerTest.java
@@ -0,0 +1,94 @@
+/**
+ * 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.Date;
+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.CommitStatus;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class PullRequestStateProducerTest extends GitHubComponentTestBase {
+    protected static final Logger LOG = LoggerFactory.getLogger(PullRequestStateProducerTest.class);
+    private String commitsha;
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+
+            @Override
+            public void configure() throws Exception {
+                context.addComponent("github", new GitHubComponent());
+                from("direct:validPullRequest")
+                        .process(new MockPullRequestStateProducerProcessor())
+                        .to("github://pullRequestState?state=success&" + GITHUB_CREDENTIALS_STRING);
+            } // end of configure
+
+
+        };
+    }
+
+
+    @Test
+    public void testPullRequestStateProducer() throws Exception {
+        commitsha = commitService.getNextSha();
+
+        Endpoint stateProducerEndpoint = getMandatoryEndpoint("direct:validPullRequest");
+        Exchange exchange = stateProducerEndpoint.createExchange();
+        String text = "Message sent at " + new Date();
+        exchange.getIn().setBody(text);
+        Exchange response=template.send(stateProducerEndpoint, exchange);
+
+        assertNotNull(response.getOut().getBody());
+
+        if (!(response.getOut().getBody() instanceof CommitStatus)) {
+            fail("Expecting CommitStatus");
+        }
+
+        CommitStatus status=response.getOut().getBody(CommitStatus.class);
+
+        // Check status set on commit service
+        if (commitService.getCommitStatus(commitsha) != status) {
+            fail("Commit status sent to service is different from response");
+        }
+
+        assertEquals(status.getState(), "success");
+
+        assertEquals(status.getDescription(), text);
+    }
+
+
+    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("GitHubPullRequestHeadCommitSHA", commitsha);
+        }
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/c458f533/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockCommitService.java
----------------------------------------------------------------------
diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockCommitService.java
b/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockCommitService.java
new file mode 100644
index 0000000..4759170
--- /dev/null
+++ b/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockCommitService.java
@@ -0,0 +1,77 @@
+/**
+ * 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.services;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.eclipse.egit.github.core.CommitStatus;
+import org.eclipse.egit.github.core.IRepositoryIdProvider;
+import org.eclipse.egit.github.core.RepositoryCommit;
+import org.eclipse.egit.github.core.User;
+import org.eclipse.egit.github.core.service.CommitService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MockCommitService extends CommitService {
+    protected static final Logger LOG = LoggerFactory.getLogger(MockCommitService.class);
+
+    private List<RepositoryCommit> commitsList = new ArrayList<RepositoryCommit>();
+    private AtomicLong fakeSha = new AtomicLong(System.currentTimeMillis());
+    private Map<String,CommitStatus> commitStatus = new HashMap<String,CommitStatus>();
+
+    public synchronized RepositoryCommit addRepositoryCommit() {
+        User author = new User();
+        author.setEmail("someguy@gmail.com");       // TODO change
+        author.setHtmlUrl("http://github/someguy");
+        author.setLogin("someguy");
+
+        RepositoryCommit rc = new RepositoryCommit();
+        rc.setAuthor(author);
+        rc.setSha(fakeSha.incrementAndGet() + "");
+        LOG.debug("In MockCommitService added commit with sha " + rc.getSha());
+        commitsList.add(rc);
+
+        return rc;
+    }
+
+    @Override
+    public synchronized List<RepositoryCommit> getCommits(IRepositoryIdProvider repository,
String sha, String path) throws IOException {
+        LOG.debug("Returning list of size " + commitsList.size());
+        return commitsList;
+    }
+
+    @Override
+    public CommitStatus createStatus(IRepositoryIdProvider repository,
+            String sha, CommitStatus status) throws IOException {
+        commitStatus.put(sha, status);
+
+        return status;
+    }
+
+    public String getNextSha() {
+        return (fakeSha.incrementAndGet()+"");
+    }
+
+    public CommitStatus getCommitStatus(String sha) {
+        return commitStatus.get(sha);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/c458f533/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
new file mode 100644
index 0000000..dd7b376
--- /dev/null
+++ b/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockIssueService.java
@@ -0,0 +1,47 @@
+/**
+ * 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.services;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.egit.github.core.Comment;
+import org.eclipse.egit.github.core.IRepositoryIdProvider;
+import org.eclipse.egit.github.core.service.IssueService;
+
+public class MockIssueService extends IssueService {
+
+    private List<Comment> comments = new ArrayList<Comment>();
+    private MockPullRequestService mockPullRequestService;
+
+    public MockIssueService(MockPullRequestService mockPullRequestService) {
+        this.mockPullRequestService = mockPullRequestService;
+
+    }
+
+    @Override
+    public List<Comment> getComments(IRepositoryIdProvider repository, int issueNumber)
{
+        return comments;
+    }
+
+    @Override
+    public Comment createComment(IRepositoryIdProvider repository, int issueNumber, String
commentText) throws IOException {
+        Comment addedComment = mockPullRequestService.addComment((long) issueNumber, commentText);
+        return addedComment;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/c458f533/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockPullRequestService.java
----------------------------------------------------------------------
diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockPullRequestService.java
b/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockPullRequestService.java
new file mode 100755
index 0000000..2619c8b
--- /dev/null
+++ b/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockPullRequestService.java
@@ -0,0 +1,138 @@
+/**
+ * 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.services;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.eclipse.egit.github.core.CommitComment;
+import org.eclipse.egit.github.core.CommitFile;
+import org.eclipse.egit.github.core.IRepositoryIdProvider;
+import org.eclipse.egit.github.core.PullRequest;
+import org.eclipse.egit.github.core.User;
+import org.eclipse.egit.github.core.service.PullRequestService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MockPullRequestService extends PullRequestService {
+    protected static final Logger LOG = LoggerFactory.getLogger(MockPullRequestService.class);
+
+    private Map<Long, PullRequest> pullRequests = new HashMap<Long, PullRequest>();
+    private List<CommitComment> emptyComments = new ArrayList<CommitComment>();
+    private AtomicInteger pullRequestNumber = new AtomicInteger(101);
+    private AtomicInteger commentId = new AtomicInteger(500);
+    private Map<Long, List<CommitComment>> allComments = new HashMap<Long,
List<CommitComment>>();
+    private Map<Integer,List<CommitFile>> files = new HashMap<Integer,List<CommitFile>>();
+
+    public List<CommitComment> getComments(IRepositoryIdProvider repository, int pullRequestId)
{
+        Long id = new Long(pullRequestId);
+        if (allComments.containsKey(id)) {
+            List<CommitComment> comments = allComments.get(id);
+            return comments;
+        } else {
+            return emptyComments;
+        }
+    }
+
+    private User createAuthor() {
+        User author = new User();
+        author.setEmail("someguy@gmail.com");
+        author.setHtmlUrl("http://github/someguy");
+        author.setLogin("someguy");
+
+        return author;
+    }
+
+    public CommitComment addComment(Long pullRequestId, String bodyText) {
+        CommitComment commitComment = new CommitComment();
+
+        User author = createAuthor();
+        commitComment.setUser(author);
+        commitComment.setCommitId("" + pullRequestId);
+        commitComment.setId(commentId.getAndIncrement());
+        commitComment.setBody(bodyText);
+        commitComment.setBodyText(bodyText);
+
+        List<CommitComment> comments;
+        if (allComments.containsKey(pullRequestId)) {
+            comments = allComments.get(pullRequestId);
+        } else {
+            comments = new ArrayList<CommitComment>();
+        }
+        comments.add(commitComment);
+        allComments.put(pullRequestId, comments);
+
+        return commitComment;
+    }
+
+    public PullRequest addPullRequest(String title) {
+        User author = createAuthor();
+
+        PullRequest pullRequest = new PullRequest();
+        pullRequest.setUser(author);
+        pullRequest.setHtmlUrl("https://github.com/someguy/somerepo/pull" + pullRequestNumber);
+        pullRequest.setTitle(title);
+        pullRequest.setNumber(pullRequestNumber.get());
+        pullRequest.setId(pullRequestNumber.get());
+        pullRequest.setState("open");
+        pullRequests.put(pullRequest.getId(), pullRequest);
+
+        pullRequestNumber.incrementAndGet();
+        return pullRequest;
+    }
+
+    @Override
+    public PullRequest getPullRequest(IRepositoryIdProvider repository, int id) throws IOException
{
+        PullRequest pullRequest = pullRequests.get((long) id);
+        return pullRequest;
+    }
+
+    @Override
+    public PullRequest editPullRequest(IRepositoryIdProvider repository, PullRequest request)
throws IOException {
+        pullRequests.put(request.getId(), request);
+        return request;
+    }
+
+    @Override
+    public synchronized List<PullRequest> getPullRequests(IRepositoryIdProvider repository,
String state) {
+        List<PullRequest> result = new ArrayList<PullRequest>();
+
+        for (Long id : pullRequests.keySet()) {
+            PullRequest pr = pullRequests.get(id);
+            if (pr.getState().equals(state)) {
+                result.add(pr);
+            }
+        }
+
+        LOG.debug("Returning list of " + result.size() + " pull requests with state " + state);
+        return result;
+    }
+
+    public void setFiles(int id, List<CommitFile> commitFiles) {
+        files.put(id, commitFiles);
+    }
+
+    @Override
+    public List<CommitFile> getFiles(IRepositoryIdProvider repository, int id)
+                            throws IOException {
+        return (files.get(id));
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/c458f533/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockRepositoryService.java
----------------------------------------------------------------------
diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockRepositoryService.java
b/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockRepositoryService.java
new file mode 100644
index 0000000..9c93273
--- /dev/null
+++ b/components/camel-github/src/test/java/org/apache/camel/component/github/services/MockRepositoryService.java
@@ -0,0 +1,55 @@
+/**
+ * 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.services;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.egit.github.core.IRepositoryIdProvider;
+import org.eclipse.egit.github.core.Repository;
+import org.eclipse.egit.github.core.RepositoryTag;
+import org.eclipse.egit.github.core.service.RepositoryService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MockRepositoryService extends RepositoryService {
+    protected static final Logger LOG = LoggerFactory.getLogger(MockRepositoryService.class);
+
+    private List<RepositoryTag> tags = new ArrayList<RepositoryTag>();
+
+    public RepositoryTag addTag(String tagName) {
+        RepositoryTag tag = new RepositoryTag();
+        tag.setName(tagName);
+        tags.add(tag);
+
+        return tag;
+    }
+
+    @Override
+    public Repository getRepository(final String owner, final String name) throws IOException
{
+        Repository repository = new Repository();
+        repository.setName(name);
+        return repository;
+    }
+
+    @Override
+    public List<RepositoryTag> getTags(IRepositoryIdProvider repository) throws IOException
{
+        LOG.debug("in MockRepositoryService returning " + tags.size() + " tags");
+        return tags;
+    }
+}


Mime
View raw message