Skip to content

Commit

Permalink
Merge pull request #240 from hhpatel14/kantra-front
Browse files Browse the repository at this point in the history
Kantra Integration
  • Loading branch information
johnsteele authored Feb 14, 2024
2 parents c85e66c + 9579176 commit 91e9d44
Show file tree
Hide file tree
Showing 52 changed files with 717 additions and 1,342 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-core:2.12.4'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.4'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.12.4'
implementation 'org.yaml:snakeyaml:1.28'


compile files(new File(buildDir, 'resources/resources')) {
builtBy 'copyResources'
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ideaVersion=2020.2
projectVersion=1.2.0
ideaVersion=2020.3
projectVersion=1.3.0
jetBrainsToken=invalid
jetBrainsChannel=stable
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
distributionUrl=https://services.gradle.org/distributions/gradle-6.9-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Binary file added kantra
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
package org.jboss.tools.intellij.windup.cli;

import com.google.common.collect.Lists;
import org.jboss.tools.intellij.windup.model.KantraConfiguration;
import org.jboss.tools.intellij.windup.model.WindupConfiguration;

import java.util.List;
import java.util.Map;

public class KantraCliParamBuilder {

public static List<String> buildParams(KantraConfiguration config, String windupHome) {
public static List<String> buildParams(WindupConfiguration config) {
List<String> params = Lists.newArrayList();
Map<String, Object> options = config.getOptions();
params.add("analyze");
Expand All @@ -26,15 +26,30 @@ public static List<String> buildParams(KantraConfiguration config, String windup
String output = (String)options.get("output");
params.add(output);

if (options.containsKey("analyze-known-libraries")) {
params.add("--analyze-known-libraries");
}

// overwrite
if (options.containsKey("overwrite")) {
params.add("--overwrite");
}

if (options.containsKey("source-only")) {
params.add("--mode");
params.add("source-only");
}

// target
List<String> target = (List<String>)options.get("target");
if (target == null || target.isEmpty()) {
target = Lists.newArrayList();
target.add("eap7");
}
params.add("--target");
// params.add("--target");

for (String aTarget : target) {
params.add("--target");
params.add(aTarget);
}
// params.add(String.join(",", target));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public static class ProgressMessage {
String task = "";
int totalWork = 0;
String value = "";

public ProgressMessage(String op, String task, int totalWork, String value) {
this.op = op;
this.task = task;
this.totalWork = totalWork;
this.value = value;
}
}

private IProgressListener progressListener;
Expand All @@ -35,6 +42,7 @@ public static class ProgressMessage {
private boolean done = false;

public ProgressMonitor(IProgressListener progressListener) {
//System.out.println("This is the progressMonitor.");
this.progressListener = progressListener;
}

Expand Down Expand Up @@ -163,7 +171,7 @@ public static JsonObject parseProgressMessage(String text) {
}

public static ProgressMessage parse(JsonObject json) throws JsonSyntaxException {
ProgressMonitor.ProgressMessage msg = new ProgressMonitor.ProgressMessage();
ProgressMonitor.ProgressMessage msg = new ProgressMonitor.ProgressMessage("","",0,"");
msg.op = json.get("op").getAsString();
if (json.has("value")) {
msg.value = json.get("value").getAsString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,103 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.jboss.tools.intellij.windup.model.KantraConfiguration;

import org.jboss.tools.intellij.windup.model.WindupConfiguration.*;
import org.jboss.tools.intellij.windup.model.WindupConfiguration;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RulesetParser {

public static List<KantraConfiguration.Ruleset> parseRuleset(String resultFilePath){
public static List<Ruleset> parseRuleset(String resultFilePath) {
Thread currentThread = Thread.currentThread();
ClassLoader originalClassLoader = currentThread.getContextClassLoader();
ClassLoader jacksonClassLoader = RulesetParser.class.getClassLoader();

try {
currentThread.setContextClassLoader(jacksonClassLoader);

ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
File yamlFile = new File(resultFilePath);
objectMapper.findAndRegisterModules();
List<KantraConfiguration.Ruleset> ruleSets = objectMapper.readValue(yamlFile, new TypeReference<List<KantraConfiguration.Ruleset>>(){});
List<WindupConfiguration.Ruleset> ruleSets = objectMapper.readValue(
yamlFile, new TypeReference<List<WindupConfiguration.Ruleset>>(){}
);
if (ruleSets != null) {
ruleSets.removeIf(ruleset -> (ruleset.getViolations() == null || ruleset.getViolations().isEmpty() ) );
//System.out.println("**************** In Parser --> Size of the ruleSet ***************" + ruleSets.size());
ruleSets.removeIf(ruleset -> (ruleset.getViolations() == null || ruleset.getViolations().isEmpty()));
return ruleSets;
} else {
System.out.println("YAML file is empty or invalid.");
}
} catch (FileNotFoundException e) {
System.err.println("File not found: " + resultFilePath);
System.err.println("File not found : " + resultFilePath);
} catch (Exception e) {
System.err.println("Error parsing YAML: " + e.getMessage());
} finally {
currentThread.setContextClassLoader(originalClassLoader);
}
return null;
}
public static void parseRulesetForKantraConfig (KantraConfiguration configuration){

public static void parseRulesetForKantraConfig (WindupConfiguration configuration){
if (configuration.getOptions() != null){
String outputLocation = configuration.getRulesetResultLocation();
configuration.setRulesets(parseRuleset(outputLocation));

if(configuration.getSummary() != null){
configuration.getSummary().setRulesets(parseRuleset(outputLocation));
parseIncidents(configuration.getRulesets(), configuration);
// System.out.println("size of the Incident: ");
// System.out.println(configuration.getSummary().getIssues().size());
}else {
System.out.println(" configuration.getSummary() is null");
}

}else{
System.out.println("Error parsing Ruleset at: " + configuration.getRulesetResultLocation());
}

}

public static void parseIncidents (List<WindupConfiguration.Ruleset> rulesets, WindupConfiguration configuration) {
if (rulesets != null){
for (WindupConfiguration.Ruleset ruleset: rulesets){
Map<String, Violation> violations = ruleset.getViolations();
if (violations != null ){
for (Map.Entry<String, WindupConfiguration.Violation> entry : violations.entrySet()) {
//for(WindupConfiguration.Violation violation : violations.values()){
WindupConfiguration.Violation violation = entry.getValue();
List<WindupConfiguration.Incident> incidents = violation.getIncidents();
for (WindupConfiguration.Incident incident : incidents ) {
incident.id = WindupConfiguration.generateUniqueId();
incident.title = violation.getDescription().split("\n", 2)[0];
ArrayList<String> inputs = (ArrayList<String>) configuration.getOptions().get("input");
String input = inputs.get(0);
String filePath = incident.getUri();;
incident.ruleId = entry.getKey();
String absolutePath = filePath.substring(filePath.indexOf("/source-code") + "/source-code".length());
// System.out.println("input: " + input);
// System.out.println("Absolute path: "+input + absolutePath);
incident.file = input + absolutePath;
incident.setUri(input + absolutePath);
// System.out.println("File path of the incidents: " + incident.file);
incident.effort = String.valueOf(violation.getEffort());
incident.links = violation.getLinks();
incident.category = violation.getCategory();
if (configuration.getSummary().completeIssues.contains(incident.id)) {
incident.complete = true;
}
incident.configuration = configuration;
configuration.getSummary().incidents.add(incident);
}
}
}
}

}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ public class RunAnalysisCommandHandler {
private WindupConsole console;

public RunAnalysisCommandHandler(Project project,
String executable,
List<String> params,
WindupConsole console,
Runnable onComplete) {
this.project = project;
this.console = console;
commandLine = new GeneralCommandLine();
commandLine.setExePath(executable);
commandLine.setExePath("kantra");
commandLine.addParameters(params);
// System.out.println("This is RunAnalysis Command handler............. constructor");
System.out.println(onComplete.toString());
this.progressMonitor = new ProgressMonitor(this.createProgressListener(onComplete));
}

Expand Down Expand Up @@ -81,6 +82,7 @@ public void run(final ProgressIndicator indicator) {
progressIndicator.setFraction(0.01);
handler.waitFor();
logTime();
System.out.println("This is the end of the runAnlysis");
}
catch (Exception e) {
e.printStackTrace();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
import com.intellij.openapi.vfs.CharsetToolkit;
import org.jboss.tools.intellij.windup.explorer.actions.RunConfigurationAction;
import org.jetbrains.annotations.NotNull;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.nio.charset.Charset;
import java.util.Arrays;


import static org.jboss.tools.intellij.windup.cli.ProgressMonitor.PROGRESS;

Expand All @@ -25,6 +26,8 @@ public class WindupCliProcessHandler extends OSProcessHandler {
private WindupConsole console;
private boolean isCancelled = false;

// int i = 0;

public WindupCliProcessHandler(
Process process,
GeneralCommandLine commandLine,
Expand All @@ -40,6 +43,11 @@ public WindupCliProcessHandler(

@Override
public final void notifyTextAvailable(@NotNull String text, @NotNull final Key outputType) {

// if(i <= 10){
// System.out.println ("This is output from Kantra -----------------------> " + text);
// }
// i++;
if (progressIndicator.isCanceled()) {
destroyProcess();
RunConfigurationAction.running = false;
Expand All @@ -61,6 +69,40 @@ else if (text.contains("Reading tags definitions")) {
else if (text.contains("Finished provider load")) {
progressIndicator.setText("Loading transformation paths...");
}
else if (text.contains("running source code analysis")) {
progressIndicator.setText("Running source code analysis...");
progressIndicator.setFraction(0.10);
}
else if (text.contains("generating analysis log in file")) {
int progress = calculateWorkDonePercentage(text);
progressIndicator.setText("Generating Analysis logs...");
// progressIndicator.setText("Generating Analysis logs (" + progress + "%)");
progressIndicator.setFraction(0.35);
}
else if (text.contains("running dependency analysis")) {
progressIndicator.setText("Running Dependency Analysis...");
progressIndicator.setFraction(0.65);
}
else if (text.contains("generating dependency log in file")) {
progressIndicator.setText("Generating Dependency logs...");
progressIndicator.setFraction(0.75);
}
else if (text.contains("generating static report")) {
progressIndicator.setText("Generating static report...");
progressIndicator.setFraction(0.95);
}
else if (text.contains("Static report created.")) {
System.out.println(text + "---------------------------------------: detected ");
ProgressMonitor.ProgressMessage msg = new ProgressMonitor.ProgressMessage("complete", "", 20, "");
progressMonitor.handleMessage(msg);
progressIndicator.setFraction(1);
}
else if (text.contains("Error")) {
System.out.println(text + "---------------------------------------: detected ");
ProgressMonitor.ProgressMessage msg = new ProgressMonitor.ProgressMessage("complete", "", 20, "");
progressMonitor.handleMessage(msg);
progressIndicator.setFraction(1);
}
else if (text.contains(PROGRESS)) {
JsonObject json = ProgressMonitor.parseProgressMessage(text);
if (json != null) {
Expand Down Expand Up @@ -89,4 +131,26 @@ else if (text.contains(PROGRESS)) {
public Charset getCharset() {
return CharsetToolkit.UTF8_CHARSET;
}

public static int calculateWorkDonePercentage(String log) {
// Regex pattern to extract the relevant numbers from the log string
String pattern = "failed=(\\d+) matched=(\\d+) total=(\\d+) unmatched=(\\d+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(log);

if (m.find()) {
// Parse numbers from the log string
int failed = Integer.parseInt(m.group(1));
int matched = Integer.parseInt(m.group(2));
int total = Integer.parseInt(m.group(3));
int unmatched = Integer.parseInt(m.group(4));

// Calculate the work done percentage
double workDone = (double) (failed + matched + unmatched) / total;
return (int) Math.round(workDone * 100);
} else {
// If the pattern does not match, return an error code or throw an exception
return -1; // Or you could throw an exception
}
}
}
Loading

0 comments on commit 91e9d44

Please sign in to comment.