mbien commented on code in PR #6630:
URL: https://github.com/apache/netbeans/pull/6630#discussion_r1838370409


##########
.github/scripts/CommitHeaderChecker.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpClient.Redirect;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse.BodyHandlers;
+import java.time.Duration;
+import static java.util.stream.Gatherers.fold;
+import static java.util.stream.Gatherers.scan;
+import static java.util.stream.Gatherers.windowSliding;
+
+record Commit(int index, String from, String date, String subject, String 
blank) {}
+record Result(int total, boolean green) {}
+
+// checks commit headers for valid author, email and commit msg formatting
+// Java 23+, may require preview flag
+
+// java CommitHeaderChecker.java https://github.com/apache/netbeans/pull/${{ 
github.event.pull_request.number }}
+void main(String[] args) throws IOException, InterruptedException {
+
+    if (args.length != 1 || !args[0].startsWith("https://github.com/";)) {
+        throw new IllegalArgumentException("PR URL expected");
+    }
+
+    HttpRequest request = HttpRequest.newBuilder()
+            .uri(URI.create(args[0]+".patch"))
+            .timeout(Duration.ofSeconds(10))
+            .build();
+
+    println("checking PR patch file...");
+    Result result;
+    try (HttpClient client = HttpClient.newBuilder()
+            .followRedirects(Redirect.NORMAL).build()) {
+
+        result = client.send(request, BodyHandlers.ofLines()).body()
+            // 4 line window, From/Date/Subject and extra line for blank line 
/ overflow check
+            .gather(windowSliding(4))
+            .filter(w -> w.size() == 4 && w.get(0).startsWith("From: ") && 
w.get(1).startsWith("Date: ") && w.get(2).startsWith("Subject: "))
+            .gather(scan(
+                () -> new Commit(0, "", "", "", ""),
+                (c, w) -> new Commit(c.index+1, w.get(0), w.get(1), w.get(2), 
w.get(3))))
+            .gather(fold(
+                () -> new Result(0, true),
+                (r, c) -> new Result(r.total+1, r.green & checkCommit(c))))
+            .findFirst()
+            .orElseThrow();
+    }
+
+    println(result.total + " commit(s) checked");
+    System.exit(result.green ? 0 : 1);
+}
+
+boolean checkCommit(Commit c) {
+    return checkNameAndEmail(c.index, c.from)
+         & checkSubject(c.index, c.subject)
+         & checkBlankLineAfterSubject(c.index, c.blank);
+}
+
+boolean checkNameAndEmail(int i, String from) {
+    // From: Duke <[email protected]>
+    int start = from.indexOf('<');
+    int end = from.indexOf('>');
+
+    String mail = end > start ? from.substring(start+1, end) : "";
+    String author = start > 6 ? from.substring(6, start).strip() : "";
+
+    boolean green = true;
+    if (mail.isBlank() || !mail.contains("@") || mail.contains("noreply") || 
mail.contains("localhost")) {

Review Comment:
   note to self: allow dependabot



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to