You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/08/31 10:56:51 UTC

[camel] branch camel-3.18.x updated (559ef5b379a -> 501b765951e)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch camel-3.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git


    from 559ef5b379a CAMEL-18439: camel-github - Consumer that polls commits crashed when repository has more than 100 commits
     new eab8dd54125 CAMEL-18442: camel-github - Fix NPE
     new 501b765951e CAMEL-18442: camel-github - Github commit consumer does not pickup new commits

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../component/github/consumer/CommitConsumer.java  | 78 ++++++++++++++--------
 .../CommitConsumerSkipExistingOnStartupTest.java   |  2 +-
 .../github/services/MockCommitService.java         |  2 +-
 3 files changed, 54 insertions(+), 28 deletions(-)


[camel] 02/02: CAMEL-18442: camel-github - Github commit consumer does not pickup new commits

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-3.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 501b765951eb12fc5763f8debb7c04f3720ab8dc
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Aug 31 12:55:47 2022 +0200

    CAMEL-18442: camel-github - Github commit consumer does not pickup new commits
---
 .../component/github/consumer/CommitConsumer.java  | 70 ++++++++++++++--------
 .../CommitConsumerSkipExistingOnStartupTest.java   |  2 +-
 .../github/services/MockCommitService.java         |  2 +-
 3 files changed, 48 insertions(+), 26 deletions(-)

diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/CommitConsumer.java b/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/CommitConsumer.java
index c7a09fc4bf1..f13cb58e36c 100644
--- a/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/CommitConsumer.java
+++ b/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/CommitConsumer.java
@@ -34,7 +34,7 @@ import org.slf4j.LoggerFactory;
 public class CommitConsumer extends AbstractGitHubConsumer {
     private static final transient Logger LOG = LoggerFactory.getLogger(CommitConsumer.class);
 
-    private static final int CAPACITY = 100;
+    private static final int CAPACITY = 1000; // in case there is a lot of commits and this runs not very frequently
 
     private CommitService commitService;
     private final String branchName;
@@ -49,8 +49,18 @@ public class CommitConsumer extends AbstractGitHubConsumer {
         super(endpoint, processor);
         this.branchName = branchName;
         this.startingSha = startingSha;
+    }
+
+    @Override
+    public GitHubEndpoint getEndpoint() {
+        return (GitHubEndpoint) super.getEndpoint();
+    }
 
-        Registry registry = endpoint.getCamelContext().getRegistry();
+    @Override
+    protected void doInit() throws Exception {
+        super.doInit();
+
+        Registry registry = getEndpoint().getCamelContext().getRegistry();
         Object service = registry.lookupByName(GitHubConstants.GITHUB_COMMIT_SERVICE);
         if (service != null) {
             LOG.debug("Using CommitService found in registry {}", service.getClass().getCanonicalName());
@@ -58,11 +68,7 @@ public class CommitConsumer extends AbstractGitHubConsumer {
         } else {
             commitService = new CommitService();
         }
-    }
 
-    @Override
-    protected void doInit() throws Exception {
-        super.doInit();
         initService(commitService);
     }
 
@@ -75,25 +81,28 @@ public class CommitConsumer extends AbstractGitHubConsumer {
         lastSha = null;
 
         if (startingSha.equals("last")) {
-            LOG.info("GitHub CommitConsumer: Indexing current commits...");
+            LOG.info("Indexing current commits on: {}/{}@{}", getEndpoint().getRepoOwner(), getEndpoint().getRepoName(),
+                    branchName);
             List<RepositoryCommit> commits = commitService.getCommits(getRepository(), branchName, null);
             for (RepositoryCommit commit : commits) {
                 String sha = commit.getSha();
-                // make room when adding new elements
-                while (commitHashes.size() > CAPACITY - 1) {
-                    commitHashes.remove();
-                }
-                commitHashes.add(sha);
-                if (lastSha == null) {
-                    lastSha = sha;
+                if (!commitHashes.contains(sha)) {
+                    // make room when adding new elements
+                    while (commitHashes.size() > CAPACITY - 1) {
+                        commitHashes.remove();
+                    }
+                    commitHashes.add(sha);
                 }
             }
-            LOG.info("GitHub CommitConsumer: Starting from last sha: {}", lastSha);
+            if (!commitHashes.isEmpty()) {
+                lastSha = commitHashes.peek();
+            }
+            LOG.info("Starting from last sha: {}", lastSha);
         } else if (!startingSha.equals("beginning")) {
             lastSha = startingSha;
-            LOG.info("GitHub CommitConsumer: Starting from sha: {}", lastSha);
+            LOG.info("Starting from sha: {}", lastSha);
         } else {
-            LOG.info("GitHub CommitConsumer: Starting from beginning");
+            LOG.info("Starting from beginning");
         }
     }
 
@@ -107,30 +116,41 @@ public class CommitConsumer extends AbstractGitHubConsumer {
 
     @Override
     protected int poll() throws Exception {
-        List<RepositoryCommit> commits;
+        List<RepositoryCommit> commits = commitService.getCommits(getRepository(), branchName, null);
+
+        // clip the list after the last sha
         if (lastSha != null) {
-            commits = commitService.getCommits(getRepository(), lastSha, null);
-        } else {
-            commits = commitService.getCommits(getRepository());
+            int pos = -1;
+            for (int i = 0; i < commits.size(); i++) {
+                RepositoryCommit commit = commits.get(i);
+                if (lastSha.equals(commit.getSha())) {
+                    pos = i;
+                    break;
+                }
+            }
+            if (pos != -1) {
+                commits = commits.subList(0, pos);
+            }
         }
 
         // In the end, we want tags oldest to newest.
         Stack<RepositoryCommit> newCommits = new Stack<>();
         for (RepositoryCommit commit : commits) {
-            if (!commitHashes.contains(commit.getSha())) {
+            String sha = commit.getSha();
+            if (!commitHashes.contains(sha)) {
                 newCommits.push(commit);
                 // make room when adding new elements
                 while (commitHashes.size() > CAPACITY - 1) {
                     commitHashes.remove();
                 }
-                commitHashes.add(commit.getSha());
-                lastSha = commit.getSha();
+                commitHashes.add(sha);
             }
         }
 
         int counter = 0;
         while (!newCommits.empty()) {
             RepositoryCommit newCommit = newCommits.pop();
+            lastSha = newCommit.getSha();
             Exchange e = createExchange(true);
             if (newCommit.getAuthor() != null) {
                 e.getMessage().setHeader(GitHubConstants.GITHUB_COMMIT_AUTHOR, newCommit.getAuthor().getName());
@@ -144,6 +164,8 @@ public class CommitConsumer extends AbstractGitHubConsumer {
             getProcessor().process(e);
             counter++;
         }
+        LOG.debug("Last sha: {}", lastSha);
         return counter;
     }
+
 }
diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/CommitConsumerSkipExistingOnStartupTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/CommitConsumerSkipExistingOnStartupTest.java
index c648dc98d2d..f4bbafecb02 100644
--- a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/CommitConsumerSkipExistingOnStartupTest.java
+++ b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/CommitConsumerSkipExistingOnStartupTest.java
@@ -46,7 +46,7 @@ public class CommitConsumerSkipExistingOnStartupTest extends GitHubComponentTest
         commitService.addRepositoryCommit("test-2");
 
         mockResultEndpoint.expectedMessageCount(3);
-        mockResultEndpoint.expectedBodiesReceivedInAnyOrder("test-3", "test-4", "test-5");
+        mockResultEndpoint.expectedBodiesReceived("test-3", "test-4", "test-5");
 
         context.getRouteController().startAllRoutes();
 
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
index 5645112d9bb..bffadff0eba 100644
--- 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
@@ -56,7 +56,7 @@ public class MockCommitService extends CommitService {
         }
         rc.setCommit(commit);
         LOG.debug("In MockCommitService added commit with sha " + rc.getSha());
-        commitsList.add(rc);
+        commitsList.add(0, rc);
 
         return rc;
     }


[camel] 01/02: CAMEL-18442: camel-github - Fix NPE

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-3.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit eab8dd54125674e1edf5c9cbb49de3323e778e86
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Aug 31 09:58:57 2022 +0200

    CAMEL-18442: camel-github - Fix NPE
---
 .../apache/camel/component/github/consumer/CommitConsumer.java    | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/CommitConsumer.java b/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/CommitConsumer.java
index b78e4922e65..c7a09fc4bf1 100644
--- a/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/CommitConsumer.java
+++ b/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/CommitConsumer.java
@@ -132,8 +132,12 @@ public class CommitConsumer extends AbstractGitHubConsumer {
         while (!newCommits.empty()) {
             RepositoryCommit newCommit = newCommits.pop();
             Exchange e = createExchange(true);
-            e.getMessage().setHeader(GitHubConstants.GITHUB_COMMIT_AUTHOR, newCommit.getAuthor().getName());
-            e.getMessage().setHeader(GitHubConstants.GITHUB_COMMIT_COMMITTER, newCommit.getCommitter().getName());
+            if (newCommit.getAuthor() != null) {
+                e.getMessage().setHeader(GitHubConstants.GITHUB_COMMIT_AUTHOR, newCommit.getAuthor().getName());
+            }
+            if (newCommit.getCommitter() != null) {
+                e.getMessage().setHeader(GitHubConstants.GITHUB_COMMIT_COMMITTER, newCommit.getCommitter().getName());
+            }
             e.getMessage().setHeader(GitHubConstants.GITHUB_COMMIT_SHA, newCommit.getSha());
             e.getMessage().setHeader(GitHubConstants.GITHUB_COMMIT_URL, newCommit.getUrl());
             e.getMessage().setBody(newCommit.getCommit().getMessage());