cmccabe commented on code in PR #14389:
URL: https://github.com/apache/kafka/pull/14389#discussion_r1456448973


##########
tools/src/main/java/org/apache/kafka/tools/SchemaChecker/MetadataSchemaChecker.java:
##########
@@ -0,0 +1,347 @@
+/*
+ * 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.kafka.tools.SchemaChecker;
+
+
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import org.eclipse.jgit.api.CheckoutCommand;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.InitCommand;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.internal.storage.file.FileRepository;
+import org.eclipse.jgit.lib.*;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevTree;
+import org.eclipse.jgit.revwalk.RevWalk;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
+import org.eclipse.jgit.treewalk.TreeWalk;
+import org.eclipse.jgit.treewalk.filter.PathFilter;
+
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.*;
+
+public class MetadataSchemaChecker {
+
+    static int latestTag = -1;
+    static int  latestTagVersion = -1;
+    static int oldLatestVersion = -1;
+    static int oldFirstVersion = -1;
+    static int newLatestVersion = -1;
+    static int newFirstVersion = -1;
+
+    static String[] filesCheckMetadata = {"AccessControlEntryRecord.json", 
"BrokerRegistrationChangeRecord.json", "ClientQuotaRecord.json",
+            "ConfigRecord.json", "DelegationTokenRecord.json", 
"FeatureLevelRecord.json", "FenceBrokerRecord.json", "NoOpRecord.json",
+            "PartitionChangeRecord.json", "PartitionRecord.json", 
"ProducerIdsRecord.json", "RegisterBrokerRecord.json",
+            "RemoveAccessControlEntryRecord.json", "RemoveTopicRecord.json", 
"RemoveUserScramCredentialRecord.json", "TopicRecord.json",
+            "UnfenceBrokerRecord.json", "UnregisterBrokerRecord.json", 
"UserScramCredentialRecord.json", "ZkMigrationRecord.json"};
+    public static void main(String[] args) throws Exception {
+
+        try {
+            List<String> localContent = new ArrayList<>();
+            for(String jsonSchema: filesCheckMetadata) {
+                final String dir = System.getProperty("user.dir");
+                String path = dir + 
"/metadata/src/main/resources/common/metadata/" + jsonSchema;
+                BufferedReader reader = new BufferedReader(new 
FileReader(path));
+                for (int i = 0; i < 15; i++) {
+                    reader.readLine();
+                }
+                StringBuilder content = new StringBuilder();
+                for (String line = reader.readLine(); line != null; line = 
reader.readLine()) {
+                    content.append(line);
+                }
+                localContent.add(content.toString());
+            }
+
+            List<String> gitContent = GetDataFromGit();
+            if (localContent.size() != gitContent.size()) {
+                throw new IllegalStateException("missing schemas");
+            }
+            for(int i = 0; i < localContent.size(); i++) {
+                if (Objects.equals(localContent.get(i), gitContent.get(i))) {
+                    continue;
+                }
+
+                ObjectMapper objectMapper = new ObjectMapper();
+                JsonNode jsonNode1 = objectMapper.readTree(gitContent.get(i));
+                JsonNode jsonNode2 = 
objectMapper.readTree(localContent.get(i));
+
+                checkApiTypeVersions(jsonNode1, jsonNode2);
+                parser((ArrayNode) jsonNode1.get("fields"), (ArrayNode) 
jsonNode2.get("fields"));
+            }
+
+        } catch (FileNotFoundException e) {
+            System.out.println("An error occurred.");
+            e.printStackTrace();
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private static List<String> GetDataFromGit() throws IOException, 
GitAPIException {
+        List<String> gitSchemas = new ArrayList<>();
+
+        Git git = Git.open(new File(System.getProperty("user.dir") + "/.git"));
+        Repository repository = git.getRepository();
+        Ref head = 
git.getRepository().getRefDatabase().firstExactRef("refs/heads/trunk");
+
+        try (RevWalk revWalk = new RevWalk(repository)) {
+            RevCommit commit = revWalk.parseCommit(head.getObjectId());
+            RevTree tree = commit.getTree();
+            for (String jsonSchema : filesCheckMetadata) {
+                StringBuilder stringBuilder = new StringBuilder();
+                try (TreeWalk treeWalk = new TreeWalk(repository)) {
+                    treeWalk.addTree(tree);
+                    treeWalk.setRecursive(true);
+                    
treeWalk.setFilter(PathFilter.create("metadata/src/main/resources/common/metadata/"
 + jsonSchema));
+                    if (!treeWalk.next()) {
+                        throw new IllegalStateException("Did not find expected 
file /metadata/src/main/resources/common/metadata/" + jsonSchema);
+                    }
+                    ObjectId objectId = treeWalk.getObjectId(0);
+                    ObjectLoader loader = repository.open(objectId);
+
+                    String content = new String(loader.getBytes());
+                    String[] lines = content.split("\n");
+                    boolean print = false;
+                    for (int i = 15; i < lines.length; i++) {

Review Comment:
   this shouldn't be needed. just skip comments.



##########
tools/src/main/java/org/apache/kafka/tools/SchemaChecker/MetadataSchemaChecker.java:
##########
@@ -0,0 +1,359 @@
+/*
+ * 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.kafka.tools.SchemaChecker;
+
+
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.errors.GitAPIException;
+
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.ObjectLoader;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevTree;
+import org.eclipse.jgit.revwalk.RevWalk;
+import org.eclipse.jgit.treewalk.TreeWalk;
+import org.eclipse.jgit.treewalk.filter.PathFilter;
+
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+
+
+public class MetadataSchemaChecker {
+
+    static int latestTag = -1;
+    static int  latestTagVersion = -1;
+    static int oldLatestVersion = -1;
+    static int oldFirstVersion = -1;
+    static int newLatestVersion = -1;
+    static int newFirstVersion = -1;
+    static String[] filesCheckMetadata = new 
File(System.getProperty("user.dir") + 
"/metadata/src/main/resources/common/metadata/").list();
+    public static void main(String[] args) throws Exception {
+
+        try {
+            List<String> localContent = new ArrayList<>();
+            for(String fileName: filesCheckMetadata) {
+                final String dir = System.getProperty("user.dir");
+                String path = dir + 
"/metadata/src/main/resources/common/metadata/" + fileName;
+                BufferedReader reader = new BufferedReader(new 
FileReader(path));
+                for (int i = 0; i < 15; i++) {
+                    reader.readLine();
+                }
+                StringBuilder content = new StringBuilder();
+                boolean print = false;
+                for (String line = reader.readLine(); line != null; line = 
reader.readLine()) {
+                    if (line.charAt(0) == '{') {
+                        print = true;
+                    }
+                    if (print && !line.contains("//")) {
+                        content.append(line);
+                    }
+                }
+                localContent.add(content.toString());
+            }
+
+            List<String> gitContent = GetDataFromGit();
+            if (localContent.size() != gitContent.size()) {
+                throw new IllegalStateException("missing schemas");
+            }
+            for(int i = 0; i < localContent.size(); i++) {
+                if (Objects.equals(localContent.get(i), gitContent.get(i))) {
+                    continue;
+                }
+
+                ObjectMapper objectMapper = new ObjectMapper();
+                JsonNode jsonNode1 = objectMapper.readTree(gitContent.get(i));
+                JsonNode jsonNode2 = 
objectMapper.readTree(localContent.get(i));
+
+                checkApiTypeVersions(jsonNode1, jsonNode2);
+                parser((ArrayNode) jsonNode1.get("fields"), (ArrayNode) 
jsonNode2.get("fields"));
+            }
+
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private static List<String> GetDataFromGit() throws IOException, 
GitAPIException {
+        List<String> gitSchemas = new ArrayList<>();
+
+        Git git = Git.open(new File(System.getProperty("user.dir") + "/.git"));
+        Repository repository = git.getRepository();
+        Ref head = 
git.getRepository().getRefDatabase().firstExactRef("refs/heads/trunk");
+
+        try (RevWalk revWalk = new RevWalk(repository)) {
+            RevCommit commit = revWalk.parseCommit(head.getObjectId());
+            RevTree tree = commit.getTree();
+            for (String fileName : filesCheckMetadata) {
+                StringBuilder stringBuilder = new StringBuilder();
+                try (TreeWalk treeWalk = new TreeWalk(repository)) {
+                    treeWalk.addTree(tree);
+                    treeWalk.setRecursive(true);
+                    
treeWalk.setFilter(PathFilter.create("metadata/src/main/resources/common/metadata/"
 + fileName));
+                    if (!treeWalk.next()) {
+                        throw new IllegalStateException("Did not find expected 
file /metadata/src/main/resources/common/metadata/" + fileName);
+                    }
+                    ObjectId objectId = treeWalk.getObjectId(0);
+                    ObjectLoader loader = repository.open(objectId);
+
+                    String content = new String(loader.getBytes());
+                    String[] lines = content.split("\n");
+                    boolean print = false;
+                    for (int i = 15; i < lines.length; i++) {
+                        if (lines[i].charAt(0) == '{') {
+                            print = true;
+                        }
+                        if (print && !lines[i].contains("//")) {

Review Comment:
   It's not enough for the line to contain //. It needs to start with // (after 
whitespace has been trimmed)



-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to