gnodet-bot commented on code in PR #26623:
URL: https://github.com/apache/camel/pull/26623#discussion_r4056607155


##########
dsl/camel-kamelet-main/src/main/known-third-party-libraries.properties:
##########
@@ -163,6 +163,7 @@ org.xmlunit = org.xmlunit:xmlunit-core:${xmlunit-version}
 # Commons and utilities
 org.apache.commons.lang3 = 
org.apache.commons:commons-lang3:${commons-lang3-version}
 org.apache.commons.io = commons-io:commons-io:${commons-io-version}
+org.apache.commons.validator = 
commons-validator:commons-validator:${commons-validator-version}

Review Comment:
   ⚠️ **Build failure: `${commons-validator-version}` is not defined.**
   
   `camel-package-maven-plugin:prepare-kamelet-main` resolves `${…}` 
placeholders against the Maven project properties and throws 
`MojoFailureException` with `"no property commons-validator-version in 
parent/pom.xml"` if the property is absent. This property is not present in the 
root `pom.xml` or any parent.
   
   Add the property to `parent/pom.xml`:
   ```xml
   <commons-validator-version>1.9.0</commons-validator-version>
   ```
   (Check the actual latest version in the BOM or Maven Central.)



##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/GroovyImportChecks.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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.dsl.jbang.core.commands.ai;
+
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static 
org.apache.camel.dsl.jbang.core.commands.ai.YamlLines.countLeadingSpaces;
+
+/**
+ * Checks on the imports of the Groovy expressions of a YAML route 
(CAMEL-24843). A Groovy script compiles against the
+ * application's classpath: an import of a library the application does not 
declare fails at runtime with "unable to
+ * resolve class", which names the class and nothing else. The check names the 
library when it is a known one and says
+ * how to declare it for the runtime; with the Camel CLI a known library is 
downloaded when the script is compiled, so
+ * only an unknown one is reported there.
+ */
+public final class GroovyImportChecks {
+
+    private static final Pattern IMPORT
+            = 
Pattern.compile("^\\s*import\\s+(static\\s+)?([a-zA-Z][.\\w]*(?:\\.\\*)?)\\s*;?\\s*$");
+    /** Packages every application has: the JDK, Groovy and Camel itself. */
+    private static final List<String> ALWAYS_PRESENT
+            = List.of("java.", "javax.", "jakarta.", "groovy.", 
"org.codehaus.groovy.", "org.apache.camel.");
+
+    private GroovyImportChecks() {
+    }
+
+    /**
+     * @param content      the YAML route file
+     * @param runtime      null or "jbang" for the Camel CLI (a known library 
is downloaded when the script is
+     *                     compiled), "main", "spring-boot" or "quarkus" for a 
Maven project (the library must be
+     *                     declared in the pom)
+     * @param projectTypes the classes the project declares itself (simple 
name to fully qualified name), from the Java
+     *                     and Groovy files next to the route
+     */
+    public static List<String> validateYamlGroovyImports(String content, 
String runtime, Map<String, String> projectTypes) {
+        List<String> errors = new ArrayList<>();
+        if (content == null || content.isBlank()) {
+            return errors;
+        }
+        boolean maven = runtime != null && !runtime.isBlank() && 
!"jbang".equalsIgnoreCase(runtime);
+        Set<String> projectClasses = new LinkedHashSet<>(projectTypes != null 
? projectTypes.values() : Set.of());
+        String[] lines = content.split("\n", -1);
+        for (int i = 0; i < lines.length; i++) {
+            String trimmed = lines[i].trim();
+            String key = trimmed.startsWith("- ") ? trimmed.substring(2) : 
trimmed;
+            if (!key.startsWith("groovy:")) {
+                continue;
+            }
+            // the script: a block scalar under groovy: |, the lines under 
groovy: / expression: |, or the inline value
+            int indent = countLeadingSpaces(lines[i]);
+            List<int[]> scriptLines = new ArrayList<>();
+            String value = key.substring("groovy:".length()).trim();
+            if (value.isEmpty() || value.startsWith("|") || 
value.startsWith(">")) {
+                for (int j = i + 1; j < lines.length; j++) {
+                    if (lines[j].isBlank()) {
+                        continue;
+                    }
+                    if (countLeadingSpaces(lines[j]) <= indent) {
+                        break;
+                    }
+                    scriptLines.add(new int[] { j });
+                }
+            } else {
+                scriptLines.add(new int[] { i });
+            }
+            for (int[] sl : scriptLines) {
+                String line = lines[sl[0]].trim();
+                if (line.startsWith("expression:")) {

Review Comment:
   💡 **Dead guard: this `continue` can never fire inside a Groovy block 
scalar.**
   
   `scriptLines` is built by scanning lines that are MORE indented than the 
`groovy:` key — i.e. the raw content of the block scalar. Content inside a 
Groovy block scalar cannot start with `expression:` (that is a YAML key, not 
Groovy syntax). The trimmed line could only start with `expression:` if someone 
wrote `expression:` literally in their Groovy code, which is not valid Groovy 
anyway.
   
   If this guard is intended for a real edge case, add a comment explaining 
which YAML structure triggers it and a test for that structure. If it is not 
needed, remove it to keep the logic clean.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to