Copilot commented on code in PR #1737:
URL: https://github.com/apache/maven-site/pull/1737#discussion_r4362141854


##########
src/test/java/org/apache/maven/site/MarkdownDocsTest.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.maven.site;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class MarkdownDocsTest {
+    private static final Pattern UNESCAPED_ISSUE_LINK =
+            
Pattern.compile("\\[\\[([A-Z][A-Z0-9]*-[0-9]+)]]\\((https?://[^)\\s]+)\\)");
+
+    @Test
+    void markdownDocsEscapeIssueKeysInLinkLabels() throws IOException {
+        Path contentDirectory = Paths.get("content/markdown");
+        List<String> violations = new ArrayList<>();
+
+        try (Stream<Path> files = Files.walk(contentDirectory)) {
+            files.filter(path -> {
+                        String name = path.getFileName().toString();
+                        return name.endsWith(".md") || name.endsWith(".md.vm");
+                    })
+                    .sorted()
+                    .forEach(path -> collectViolations(contentDirectory, path, 
violations));
+        }
+
+        assertTrue(
+                violations.isEmpty(),
+                () -> "Issue keys in link labels must have their square 
brackets escaped in Markdown docs, "
+                        + "otherwise the generated link target is the issue 
key instead of the URL. "
+                        + "Use 
[\\[MNG-1234\\]](https://issues.apache.org/jira/browse/MNG-1234).\n"
+                        + String.join("\n", violations));
+    }
+
+    private static void collectViolations(Path contentDirectory, Path file, 
List<String> violations) {
+        List<String> lines;
+        try {
+            lines = Files.readAllLines(file);
+        } catch (IOException e) {
+            throw new IllegalStateException("Unable to read " + file, e);
+        }

Review Comment:
   `Files.readAllLines(file)` uses the platform default charset, which can make 
this test fail or misreport violations on systems not defaulting to UTF-8. Use 
an explicit charset (e.g., UTF-8) when reading Markdown sources so results are 
stable across environments.



##########
src/test/java/org/apache/maven/site/MarkdownDocsTest.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.maven.site;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class MarkdownDocsTest {
+    private static final Pattern UNESCAPED_ISSUE_LINK =
+            
Pattern.compile("\\[\\[([A-Z][A-Z0-9]*-[0-9]+)]]\\((https?://[^)\\s]+)\\)");
+
+    @Test
+    void markdownDocsEscapeIssueKeysInLinkLabels() throws IOException {
+        Path contentDirectory = Paths.get("content/markdown");
+        List<String> violations = new ArrayList<>();
+
+        try (Stream<Path> files = Files.walk(contentDirectory)) {
+            files.filter(path -> {
+                        String name = path.getFileName().toString();
+                        return name.endsWith(".md") || name.endsWith(".md.vm");
+                    })
+                    .sorted()
+                    .forEach(path -> collectViolations(contentDirectory, path, 
violations));
+        }

Review Comment:
   The PR description says the test scans `content/markdown/**/*.md` and 
`*.md.vm`, but the implementation only scans under `content/markdown` 
(including `.md.vm` files found there). If `*.md.vm` at the repository root (or 
outside `content/markdown`) should be included, add an additional scan for 
those paths or adjust the description to match the actual behavior.



##########
pom.xml:
##########
@@ -82,6 +82,11 @@
       <artifactId>jsoup</artifactId>
       <version>1.23.2</version>
     </dependency>
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter</artifactId>

Review Comment:
   This dependency has no explicit version. If the project (or parent) doesn’t 
already manage `org.junit.jupiter:junit-jupiter` via `<dependencyManagement>`, 
Maven will fail the build with a missing version error. Either confirm it’s 
managed centrally or add a version here (or introduce a managed JUnit 
BOM/version property).



##########
src/test/java/org/apache/maven/site/MarkdownDocsTest.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.maven.site;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class MarkdownDocsTest {
+    private static final Pattern UNESCAPED_ISSUE_LINK =
+            
Pattern.compile("\\[\\[([A-Z][A-Z0-9]*-[0-9]+)]]\\((https?://[^)\\s]+)\\)");
+
+    @Test
+    void markdownDocsEscapeIssueKeysInLinkLabels() throws IOException {
+        Path contentDirectory = Paths.get("content/markdown");
+        List<String> violations = new ArrayList<>();
+
+        try (Stream<Path> files = Files.walk(contentDirectory)) {
+            files.filter(path -> {
+                        String name = path.getFileName().toString();
+                        return name.endsWith(".md") || name.endsWith(".md.vm");
+                    })
+                    .sorted()
+                    .forEach(path -> collectViolations(contentDirectory, path, 
violations));
+        }
+
+        assertTrue(
+                violations.isEmpty(),
+                () -> "Issue keys in link labels must have their square 
brackets escaped in Markdown docs, "
+                        + "otherwise the generated link target is the issue 
key instead of the URL. "
+                        + "Use 
[\\[MNG-1234\\]](https://issues.apache.org/jira/browse/MNG-1234).\n"
+                        + String.join("\n", violations));
+    }
+
+    private static void collectViolations(Path contentDirectory, Path file, 
List<String> violations) {
+        List<String> lines;
+        try {
+            lines = Files.readAllLines(file);
+        } catch (IOException e) {
+            throw new IllegalStateException("Unable to read " + file, e);
+        }
+
+        for (int i = 0; i < lines.size(); i++) {
+            Matcher matcher = UNESCAPED_ISSUE_LINK.matcher(lines.get(i));
+            while (matcher.find()) {
+                violations.add(String.format(
+                        "  %s:%d: %s (expected [\\[%s\\]](%s))",
+                        contentDirectory.relativize(file), i + 1, 
matcher.group(), matcher.group(1), matcher.group(2)));
+            }

Review Comment:
   This line-by-line regex scan will also flag matches inside fenced code 
blocks (and potentially other code examples), which can create 
hard-to-understand false positives in documentation. Consider skipping fenced 
code blocks (e.g., track entering/exiting ``` / ~~~ blocks) before applying 
`UNESCAPED_ISSUE_LINK` so only prose content is enforced.



-- 
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