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 2021/04/16 11:24:17 UTC

[camel] branch master updated: CAMEL-16523: camel-github - Commit consumer add option for starting sha.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 8a4e383  CAMEL-16523: camel-github - Commit consumer add option for starting sha.
8a4e383 is described below

commit 8a4e383024781317de59814e7a89c697ed1f4548
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Apr 16 13:23:42 2021 +0200

    CAMEL-16523: camel-github - Commit consumer add option for starting sha.
---
 .../camel/catalog/docs/github-component.adoc       |  3 +-
 .../component/github/GitHubEndpointUriFactory.java |  4 +-
 .../org/apache/camel/component/github/github.json  |  1 +
 .../src/main/docs/github-component.adoc            |  3 +-
 .../camel/component/github/GitHubEndpoint.java     | 18 +++++-
 .../component/github/consumer/CommitConsumer.java  | 27 +++++---
 .../consumer/CommitConsumerBeginningTest.java      | 71 ++++++++++++++++++++++
 .../builder/endpoint/StaticEndpointBuilders.java   | 12 ++++
 .../endpoint/dsl/GitHubEndpointBuilderFactory.java | 14 +++++
 .../modules/ROOT/pages/github-component.adoc       |  3 +-
 10 files changed, 143 insertions(+), 13 deletions(-)

diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/github-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/github-component.adoc
index 0cfa568..7ea9832 100644
--- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/github-component.adoc
+++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/github-component.adoc
@@ -76,7 +76,7 @@ github:type/branchName
 
 with the following path and query parameters:
 
-=== Path Parameters (2 parameters):
+=== Path Parameters (3 parameters):
 
 
 [width="100%",cols="2,5,^1,2",options="header"]
@@ -84,6 +84,7 @@ with the following path and query parameters:
 | Name | Description | Default | Type
 | *type* | *Required* What git operation to execute. There are 10 enums and the value can be one of: CLOSEPULLREQUEST, PULLREQUESTCOMMENT, COMMIT, PULLREQUEST, TAG, PULLREQUESTSTATE, PULLREQUESTFILES, GETCOMMITFILE, CREATEISSUE, EVENT |  | GitHubType
 | *branchName* | Name of branch |  | String
+| *startingSha* | The starting sha to use for polling commits with the commit consumer. The value can either be a sha for the sha to start from, or use beginning to start from the beginning, or last to start from the last commit. | last | String
 |===
 
 
diff --git a/components/camel-github/src/generated/java/org/apache/camel/component/github/GitHubEndpointUriFactory.java b/components/camel-github/src/generated/java/org/apache/camel/component/github/GitHubEndpointUriFactory.java
index 38418d2..0e9021d 100644
--- a/components/camel-github/src/generated/java/org/apache/camel/component/github/GitHubEndpointUriFactory.java
+++ b/components/camel-github/src/generated/java/org/apache/camel/component/github/GitHubEndpointUriFactory.java
@@ -20,7 +20,7 @@ public class GitHubEndpointUriFactory extends org.apache.camel.support.component
     private static final Set<String> PROPERTY_NAMES;
     private static final Set<String> SECRET_PROPERTY_NAMES;
     static {
-        Set<String> props = new HashSet<>(29);
+        Set<String> props = new HashSet<>(30);
         props.add("backoffMultiplier");
         props.add("eventFetchStrategy");
         props.add("initialDelay");
@@ -48,6 +48,7 @@ public class GitHubEndpointUriFactory extends org.apache.camel.support.component
         props.add("delay");
         props.add("pollStrategy");
         props.add("startScheduler");
+        props.add("startingSha");
         props.add("exceptionHandler");
         props.add("targetUrl");
         PROPERTY_NAMES = Collections.unmodifiableSet(props);
@@ -70,6 +71,7 @@ public class GitHubEndpointUriFactory extends org.apache.camel.support.component
 
         uri = buildPathParameter(syntax, uri, "type", null, true, copy);
         uri = buildPathParameter(syntax, uri, "branchName", null, false, copy);
+        uri = buildPathParameter(syntax, uri, "startingSha", "last", false, copy);
         uri = buildQueryParameters(uri, copy, encode);
         return uri;
     }
diff --git a/components/camel-github/src/generated/resources/org/apache/camel/component/github/github.json b/components/camel-github/src/generated/resources/org/apache/camel/component/github/github.json
index 0ad9ba2..0b9747a 100644
--- a/components/camel-github/src/generated/resources/org/apache/camel/component/github/github.json
+++ b/components/camel-github/src/generated/resources/org/apache/camel/component/github/github.json
@@ -30,6 +30,7 @@
   "properties": {
     "type": { "kind": "path", "displayName": "Type", "group": "common", "label": "", "required": true, "type": "object", "javaType": "org.apache.camel.component.github.GitHubType", "enum": [ "CLOSEPULLREQUEST", "PULLREQUESTCOMMENT", "COMMIT", "PULLREQUEST", "TAG", "PULLREQUESTSTATE", "PULLREQUESTFILES", "GETCOMMITFILE", "CREATEISSUE", "EVENT" ], "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "What git operation to execute" },
     "branchName": { "kind": "path", "displayName": "Branch Name", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "Name of branch" },
+    "startingSha": { "kind": "path", "displayName": "Starting Sha", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "defaultValue": "last", "description": "The starting sha to use for polling commits with the commit consumer. The value can either be a sha for the sha to start from, or use beginning to start from the beginning, or last to start from the last commit." },
     "repoName": { "kind": "parameter", "displayName": "Repo Name", "group": "common", "label": "", "required": true, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "GitHub repository name" },
     "repoOwner": { "kind": "parameter", "displayName": "Repo Owner", "group": "common", "label": "", "required": true, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "GitHub repository owner (organization)" },
     "bridgeErrorHandler": { "kind": "parameter", "displayName": "Bridge Error Handler", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a m [...]
diff --git a/components/camel-github/src/main/docs/github-component.adoc b/components/camel-github/src/main/docs/github-component.adoc
index 0cfa568..7ea9832 100644
--- a/components/camel-github/src/main/docs/github-component.adoc
+++ b/components/camel-github/src/main/docs/github-component.adoc
@@ -76,7 +76,7 @@ github:type/branchName
 
 with the following path and query parameters:
 
-=== Path Parameters (2 parameters):
+=== Path Parameters (3 parameters):
 
 
 [width="100%",cols="2,5,^1,2",options="header"]
@@ -84,6 +84,7 @@ with the following path and query parameters:
 | Name | Description | Default | Type
 | *type* | *Required* What git operation to execute. There are 10 enums and the value can be one of: CLOSEPULLREQUEST, PULLREQUESTCOMMENT, COMMIT, PULLREQUEST, TAG, PULLREQUESTSTATE, PULLREQUESTFILES, GETCOMMITFILE, CREATEISSUE, EVENT |  | GitHubType
 | *branchName* | Name of branch |  | String
+| *startingSha* | The starting sha to use for polling commits with the commit consumer. The value can either be a sha for the sha to start from, or use beginning to start from the beginning, or last to start from the last commit. | last | String
 |===
 
 
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 e8558b4..0233c73 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
@@ -69,6 +69,8 @@ public class GitHubEndpoint extends ScheduledPollEndpoint {
     private GitHubType type;
     @UriPath(label = "consumer")
     private String branchName;
+    @UriPath(label = "consumer", defaultValue = "last")
+    private String startingSha = "last";
     @UriParam(label = "security", secret = true)
     private String oauthToken;
     @UriParam
@@ -113,7 +115,7 @@ public class GitHubEndpoint extends ScheduledPollEndpoint {
         Consumer consumer = null;
         if (type == GitHubType.COMMIT) {
             StringHelper.notEmpty(branchName, "branchName", this);
-            consumer = new CommitConsumer(this, processor, branchName);
+            consumer = new CommitConsumer(this, processor, branchName, startingSha);
         } else if (type == GitHubType.PULLREQUEST) {
             consumer = new PullRequestConsumer(this, processor);
         } else if (type == GitHubType.PULLREQUESTCOMMENT) {
@@ -154,6 +156,20 @@ public class GitHubEndpoint extends ScheduledPollEndpoint {
         this.branchName = branchName;
     }
 
+    public String getStartingSha() {
+        return startingSha;
+    }
+
+    /**
+     * The starting sha to use for polling commits with the commit consumer.
+     *
+     * The value can either be a sha for the sha to start from, or use <tt>beginning</tt> to start from the beginning,
+     * or <tt>last</tt> to start from the last commit.
+     */
+    public void setStartingSha(String startingSha) {
+        this.startingSha = startingSha;
+    }
+
     public String getOauthToken() {
         return oauthToken;
     }
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 d8660f9..f4abe1f 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
@@ -35,15 +35,18 @@ public class CommitConsumer extends AbstractGitHubConsumer {
     private static final transient Logger LOG = LoggerFactory.getLogger(CommitConsumer.class);
 
     private CommitService commitService;
-    private String branchName;
+    private final String branchName;
+    private final String startingSha;
 
     // keep a chunk of the last hashes so we can filter out duplicates
-    private Queue<String> commitHashes = new ArrayBlockingQueue<>(100);
+    private final Queue<String> commitHashes = new ArrayBlockingQueue<>(100);
     private volatile String lastSha;
 
-    public CommitConsumer(GitHubEndpoint endpoint, Processor processor, String branchName) throws Exception {
+    public CommitConsumer(GitHubEndpoint endpoint, Processor processor, String branchName,
+                          String startingSha) throws Exception {
         super(endpoint, processor);
         this.branchName = branchName;
+        this.startingSha = startingSha;
 
         Registry registry = endpoint.getCamelContext().getRegistry();
         Object service = registry.lookupByName(GitHubConstants.GITHUB_COMMIT_SERVICE);
@@ -69,11 +72,19 @@ public class CommitConsumer extends AbstractGitHubConsumer {
         commitHashes.clear();
         lastSha = null;
 
-        LOG.info("GitHub CommitConsumer: Indexing current commits...");
-        List<RepositoryCommit> commits = commitService.getCommits(getRepository(), branchName, null);
-        for (RepositoryCommit commit : commits) {
-            commitHashes.add(commit.getSha());
-            lastSha = commit.getSha();
+        if (startingSha.equals("last")) {
+            LOG.info("GitHub CommitConsumer: Indexing current commits...");
+            List<RepositoryCommit> commits = commitService.getCommits(getRepository(), branchName, null);
+            for (RepositoryCommit commit : commits) {
+                commitHashes.add(commit.getSha());
+                lastSha = commit.getSha();
+            }
+            LOG.info("GitHub CommitConsumer: Starting from last sha: {}", lastSha);
+        } else if (!startingSha.equals("beginning")) {
+            lastSha = startingSha;
+            LOG.info("GitHub CommitConsumer: Starting from sha: {}", lastSha);
+        } else {
+            LOG.info("GitHub CommitConsumer: Starting from beginning");
         }
     }
 
diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/CommitConsumerBeginningTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/CommitConsumerBeginningTest.java
new file mode 100644
index 0000000..6a02326
--- /dev/null
+++ b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/CommitConsumerBeginningTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.consumer;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.github.GitHubComponentTestBase;
+import org.apache.camel.component.github.GitHubConstants;
+import org.junit.jupiter.api.Test;
+
+public class CommitConsumerBeginningTest extends GitHubComponentTestBase {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+
+            @Override
+            public void configure() throws Exception {
+                from("github://commit/master?startingSha=beginning&repoOwner=anotherguy&repoName=somerepo")
+                        .routeId("foo").noAutoStartup()
+                        .process(new GitHubCommitProcessor())
+                        .to(mockResultEndpoint);
+            }
+        };
+    }
+
+    @Test
+    public void commitConsumerTest() throws Exception {
+        // add 2 commits before starting route
+        commitService.addRepositoryCommit("test-1");
+        commitService.addRepositoryCommit("test-2");
+
+        mockResultEndpoint.expectedMessageCount(5);
+        mockResultEndpoint.expectedBodiesReceivedInAnyOrder("test-1", "test-2", "test-3", "test-4", "test-5");
+
+        context.getRouteController().startAllRoutes();
+
+        commitService.addRepositoryCommit("test-3");
+        commitService.addRepositoryCommit("test-4");
+        commitService.addRepositoryCommit("test-5");
+
+        mockResultEndpoint.assertIsSatisfied();
+    }
+
+    public class GitHubCommitProcessor implements Processor {
+        @Override
+        public void process(Exchange exchange) throws Exception {
+            String author = exchange.getMessage().getHeader(GitHubConstants.GITHUB_COMMIT_AUTHOR, String.class);
+            String sha = exchange.getMessage().getHeader(GitHubConstants.GITHUB_COMMIT_SHA, String.class);
+            if (log.isDebugEnabled()) {
+                log.debug("Got commit with author: " + author + ": SHA "
+                          + sha);
+            }
+        }
+    }
+}
diff --git a/core/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/StaticEndpointBuilders.java b/core/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/StaticEndpointBuilders.java
index 6ea9e70..560b2e0 100644
--- a/core/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/StaticEndpointBuilders.java
+++ b/core/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/StaticEndpointBuilders.java
@@ -5284,6 +5284,12 @@ public class StaticEndpointBuilders {
      * Path parameter: branchName
      * Name of branch
      * 
+     * Path parameter: startingSha
+     * The starting sha to use for polling commits with the commit consumer. The
+     * value can either be a sha for the sha to start from, or use beginning to
+     * start from the beginning, or last to start from the last commit.
+     * Default value: last
+     * 
      * @param path type/branchName
      * @return the dsl builder
      */
@@ -5310,6 +5316,12 @@ public class StaticEndpointBuilders {
      * Path parameter: branchName
      * Name of branch
      * 
+     * Path parameter: startingSha
+     * The starting sha to use for polling commits with the commit consumer. The
+     * value can either be a sha for the sha to start from, or use beginning to
+     * start from the beginning, or last to start from the last commit.
+     * Default value: last
+     * 
      * @param componentName to use a custom component name for the endpoint
      * instead of the default name
      * @param path type/branchName
diff --git a/core/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/GitHubEndpointBuilderFactory.java b/core/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/GitHubEndpointBuilderFactory.java
index 94695c0..a3414ee 100644
--- a/core/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/GitHubEndpointBuilderFactory.java
+++ b/core/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/GitHubEndpointBuilderFactory.java
@@ -1049,6 +1049,13 @@ public interface GitHubEndpointBuilderFactory {
          * Path parameter: branchName
          * Name of branch
          * 
+         * Path parameter: startingSha
+         * The starting sha to use for polling commits with the commit consumer.
+         * The value can either be a sha for the sha to start from, or use
+         * beginning to start from the beginning, or last to start from the last
+         * commit.
+         * Default value: last
+         * 
          * @param path type/branchName
          * @return the dsl builder
          */
@@ -1074,6 +1081,13 @@ public interface GitHubEndpointBuilderFactory {
          * Path parameter: branchName
          * Name of branch
          * 
+         * Path parameter: startingSha
+         * The starting sha to use for polling commits with the commit consumer.
+         * The value can either be a sha for the sha to start from, or use
+         * beginning to start from the beginning, or last to start from the last
+         * commit.
+         * Default value: last
+         * 
          * @param componentName to use a custom component name for the endpoint
          * instead of the default name
          * @param path type/branchName
diff --git a/docs/components/modules/ROOT/pages/github-component.adoc b/docs/components/modules/ROOT/pages/github-component.adoc
index 2d6a959..d2ee434 100644
--- a/docs/components/modules/ROOT/pages/github-component.adoc
+++ b/docs/components/modules/ROOT/pages/github-component.adoc
@@ -78,7 +78,7 @@ github:type/branchName
 
 with the following path and query parameters:
 
-=== Path Parameters (2 parameters):
+=== Path Parameters (3 parameters):
 
 
 [width="100%",cols="2,5,^1,2",options="header"]
@@ -86,6 +86,7 @@ with the following path and query parameters:
 | Name | Description | Default | Type
 | *type* | *Required* What git operation to execute. There are 10 enums and the value can be one of: CLOSEPULLREQUEST, PULLREQUESTCOMMENT, COMMIT, PULLREQUEST, TAG, PULLREQUESTSTATE, PULLREQUESTFILES, GETCOMMITFILE, CREATEISSUE, EVENT |  | GitHubType
 | *branchName* | Name of branch |  | String
+| *startingSha* | The starting sha to use for polling commits with the commit consumer. The value can either be a sha for the sha to start from, or use beginning to start from the beginning, or last to start from the last commit. | last | String
 |===