Skip to content

Commit

Permalink
Try to validate commit headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbien committed Oct 28, 2023
1 parent fb08355 commit 4ab27c8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
78 changes: 78 additions & 0 deletions .github/scripts/CommitHeaderChecker.java
Original file line number Diff line number Diff line change
@@ -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.
*/

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;

public class CommitHeaderChecker {

// java CommitHeaderChecker.java https://github.com/apache/netbeans/pull/${{ github.event.number }}
public static void main(String[] args) throws IOException, InterruptedException {

if (args.length != 1 || !args[0].startsWith("https://github.com/")) {
throw new IllegalArgumentException("PR URL expected");
}

HttpClient client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).build();
HttpRequest request = HttpRequest.newBuilder(URI.create(args[0]+".patch")).build();

// https://openjdk.org/jeps/461 nice usecase for gatherers
List<String> headers = client.send(request, HttpResponse.BodyHandlers.ofLines()).body()
.filter(l -> l.startsWith("From: ") || l.startsWith("Date: ") || l.startsWith("Subject: "))
.toList();

record Commit(String from, String date, String subject) {}

List<Commit> commits = new ArrayList<>();
for (int i = 0; i < headers.size()-2; i++) {
if (headers.get(i).startsWith("From: ") && headers.get(i+1).startsWith("Date: ") && headers.get(i+2).startsWith("Subject: ")) {
commits.add(new Commit(headers.get(i), headers.get(i+1), headers.get(i+2)));
i+=2;
}
}

boolean green = true;
System.out.println("checking "+commits.size()+" commit(s)");
for (int i = 0; i < commits.size(); i++) {
Commit commit = commits.get(i);
int start = commit.from.indexOf("<");
int end = commit.from.indexOf(">");
if (start != -1 && end != -1) {
String mail = commit.from.substring(start+1, end);
if (mail.contains("@") && !mail.contains("noreply") && !mail.contains("localhost")) {
String author = commit.from.substring(6, start).strip();
if (author.contains(" ")) { // single word author -> probably the nickname/username/root etc
continue;
}
}
}
System.out.println("::error::invalid email or author in commit#"+i+" '"+commit.from+"'");
green = false;
}
System.out.println("done");
if (!green) {
System.exit(1);
}
}
}
7 changes: 3 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ jobs:
ANT_OPTS: -Dmetabuild.jsonurl=https://raw.githubusercontent.com/apache/netbeans-jenkins-lib/master/meta/netbeansrelease.json
strategy:
matrix:
java: [ '11' ]
java: [ '21' ]
steps:

- name: Set up JDK ${{ matrix.java }}
Expand All @@ -285,11 +285,10 @@ jobs:
persist-credentials: false
submodules: false
show-progress: false
fetch-depth: 10

- name: Print last 10 Commits
- name: Check Commit Headers
if: ${{ github.event_name == 'pull_request' && !cancelled() }}
run: git log --oneline -n10 --pretty=format:'%h %an [%ae] %s'
run: java .github/scripts/CommitHeaderChecker.java https://github.com/apache/netbeans/pull/${{ github.event.number }}

- name: Check line endings and verify RAT report
if: ${{ !cancelled() }}
Expand Down

0 comments on commit 4ab27c8

Please sign in to comment.