diff --git a/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/AnalysisController.java b/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/AnalysisController.java index 48ccad0..3b827bc 100644 --- a/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/AnalysisController.java +++ b/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/AnalysisController.java @@ -43,10 +43,22 @@ public List getIssues(@PathVariable(value = "projectKey") String projectK @CrossOrigin(origins = "*") @GetMapping(path="endpoints") - public HashMap getEndpointMetrics(@RequestParam(required = true) String url) { + public List getEndpointMetrics(@RequestParam(required = true) String url) { return endpointAnalysisService.getEnpointMetrics(url); } + @CrossOrigin(origins = "*") + @PostMapping(path="endpoints") + public List getEndpointMetricsPrivate(@RequestBody RequestBodyEndpoints requestBodyEndpoints) { + return endpointAnalysisService.getEndpointMetricsPrivate(requestBodyEndpoints); + } + +// @CrossOrigin(origins = "*") +// @PostMapping(path="endpoints") +// public HashMap getEndpointMetricsLocalGitlabSonar(@RequestBody RequestBodyEndpoints requestBodyEndpoints) { +// return endpointAnalysisService.getEnpointMetricsLocal(requestBodyEndpoints); +// } + @CrossOrigin(origins = "*") @PostMapping @ResponseStatus(HttpStatus.OK) diff --git a/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/EndpointAnalysisService.java b/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/EndpointAnalysisService.java index 60412aa..6322b70 100644 --- a/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/EndpointAnalysisService.java +++ b/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/EndpointAnalysisService.java @@ -28,8 +28,9 @@ public class EndpointAnalysisService { private AnalysisService analysisService; private HashMap methodsOfStartingEndpoints= new HashMap<>(); + private List allJavaFiles = new ArrayList<>(); - public HashMap getEnpointMetrics(String url) { + public List getEnpointMetrics(String url) { try { methodsOfStartingEndpoints.clear(); @@ -56,12 +57,73 @@ public HashMap getEnpointMetrics(String url) { } //Get Report - HashMap hashMap= GetAllReportForAllEndpoints(gitName, projectKey); + List reportList= GetAllReportForAllEndpoints("/"+gitName, projectKey); //delete clone FileSystemUtils.deleteRecursively(new File("/"+gitName)); - return hashMap; + return reportList; + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + public List getEndpointMetricsPrivate(RequestBodyEndpoints requestBodyEndpoints) { + try { + methodsOfStartingEndpoints.clear(); + allJavaFiles.clear(); + String projectKey= requestBodyEndpoints.getSonarQubeProjectKey(); + String gitUrl= requestBodyEndpoints.getGitUrl().replace("https://","").replace(".git",""); + String gitToken= requestBodyEndpoints.getGitToken(); + String url= "https://oauth2:" + gitToken + "@" + gitUrl; + String[] temp= gitUrl.split("/"); + String gitName= temp[temp.length-1]; + + // Git configuration + System.out.println("Clone"); + ProcessBuilder pbuilderGit = new ProcessBuilder("bash", "-c", "git config --global http.sslverify \"false\""); + Process pGit = pbuilderGit.start(); + BufferedReader inputReaderGit = new BufferedReader(new InputStreamReader(pGit.getInputStream())); + String inputLineGit; + while ((inputLineGit = inputReaderGit.readLine()) != null) { + System.out.println("! " + inputLineGit); + } + + // Clone + System.out.println("Clone"); + ProcessBuilder pbuilder1 = new ProcessBuilder("bash", "-c", "git clone " + url); + Process p1 = pbuilder1.start(); + BufferedReader errorReader = new BufferedReader(new InputStreamReader(p1.getErrorStream())); + String errorLine; + while ((errorLine = errorReader.readLine()) != null) { + System.out.println("~ " + errorLine); + } + BufferedReader inputReader = new BufferedReader(new InputStreamReader(p1.getInputStream())); + String inputLine; + while ((inputLine = inputReader.readLine()) != null) { + System.out.println("! " + inputLine); + } + + // Get All Java Files + System.out.println("Get all files"); + getAllFiles(new File("/"+gitName)); + + // Get Mappings + System.out.println("Get all endpoints"); + try { + getGivenEndpointsFromAllFiles(requestBodyEndpoints.getRequestBodyEachEndpointList()); + } catch (IOException e) { + e.printStackTrace(); + } + + //Get Report + List reportList= GetAllReportForAllEndpoints("/"+gitName, projectKey); + + //delete clone + FileSystemUtils.deleteRecursively(new File("/"+gitName)); + + return reportList; } catch (IOException e) { e.printStackTrace(); } @@ -69,15 +131,16 @@ public HashMap getEnpointMetrics(String url) { } //Start method tracing and map the issues to endpoints - private HashMap GetAllReportForAllEndpoints(String dirName, String projectKey) { + private List GetAllReportForAllEndpoints(String dirName, String projectKey) { System.out.println("Get all Issues"); List allIssues= analysisService.getIssues(projectKey); - HashMap hashMap=new HashMap<>(); + //HashMap hashMap=new HashMap<>(); + List reportList = new ArrayList<>(); //For each mapping method System.out.println("Start method trace"); for(MethodDeclaration md: methodsOfStartingEndpoints.keySet()){ //parse and find call tree - InvestigatorFacade facade = new InvestigatorFacade("/"+ dirName, methodsOfStartingEndpoints.get(md), md); + InvestigatorFacade facade = new InvestigatorFacade(dirName, methodsOfStartingEndpoints.get(md), md); Set methodCallSets = facade.start(); if (!Objects.isNull(methodCallSets)){ printResults(methodCallSets); @@ -96,7 +159,7 @@ private HashMap GetAllReportForAllEndpoints(String dirName, Stri for(Issue issue: filteredList) { int startIssueLine = Integer.parseInt(issue.getIssueStartLine()); if(methodCall.getCodeRange().getStartLine() <= startIssueLine && - methodCall.getCodeRange().getEndLine() >= startIssueLine){ + methodCall.getCodeRange().getEndLine() >= startIssueLine){ endpointIssues.add(issue); } } @@ -115,17 +178,59 @@ private HashMap GetAllReportForAllEndpoints(String dirName, Stri total += Integer.parseInt(debtInString.replace("min", "")); } - String annotationPath=""; + /*String annotationPath=""; for(AnnotationExpr n: md.getAnnotations()){ if(n.getName().asString().contains("Mapping")){ annotationPath= n.toString(); } + }*/ + + //hashMap.put(annotationPath+" | "+md.getName().toString(),new Report(new Metric("TD", total), endpointIssues)); + reportList.add(new Report(md.getName().toString(), new Metric("TD", total), endpointIssues)); + } + } + return reportList; + } + + //For each file find given endpoints + private void getGivenEndpointsFromAllFiles(List requestBodyEachEndpointList) throws FileNotFoundException, IOException { + for(RequestBodyEachEndpoint eachEndpoint:requestBodyEachEndpointList) { + for(File file: allJavaFiles) { + if (file.getName().equals(eachEndpoint.getFileName()) || file.getName().equals(eachEndpoint.getFileName()+".java")) { + List methods = new ArrayList(); + + CompilationUnit cu = StaticJavaParser.parse(file); + VoidVisitor> methodNameVisitor = new MethodNamePrinterALL(); + methodNameVisitor.visit(cu, methods); + + methods.forEach(n -> { + if (n.getDeclarationAsString().equals(eachEndpoint.getEndpointMethod())) { + System.out.println(n.getDeclarationAsString()); + methodsOfStartingEndpoints.put(n, file.getAbsolutePath()); + } + }); } + } + } + } - hashMap.put(annotationPath+" | "+md.getName().toString(),new Report(new Metric("TD", total), endpointIssues)); + private void getAllFiles(File folder){ + for (final File fileEntry : Objects.requireNonNull(folder.listFiles())) { + if (fileEntry.isDirectory()) { + getAllFiles(fileEntry); + } + else if(fileEntry.getName().endsWith(".java")){ + allJavaFiles.add(fileEntry); } } - return hashMap; + } + + private static class MethodNamePrinterALL extends VoidVisitorAdapter> { + @Override + public void visit(MethodDeclaration md, List collector) { + super.visit(md, collector); + collector.add(md); + } } //For each file find endpoints diff --git a/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/Report.java b/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/Report.java index 825e61a..8598651 100644 --- a/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/Report.java +++ b/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/Report.java @@ -3,14 +3,20 @@ import java.util.List; public class Report { + private String method; private Metric metrics; private List issueList; - public Report(Metric metrics, List issueList) { + public Report(String method, Metric metrics, List issueList) { + this.method = method; this.metrics = metrics; this.issueList = issueList; } + public String getMethod() { + return method; + } + public Metric getMetrics() { return metrics; } @@ -19,6 +25,10 @@ public List getIssueList() { return issueList; } + public void setMethod(String method) { + this.method = method; + } + public void setMetrics(Metric metrics) { this.metrics = metrics; } diff --git a/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/RequestBodyEachEndpoint.java b/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/RequestBodyEachEndpoint.java new file mode 100644 index 0000000..b2d668c --- /dev/null +++ b/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/RequestBodyEachEndpoint.java @@ -0,0 +1,27 @@ +package gr.nikos.smartclideTDPrincipal.Analysis; + +public class RequestBodyEachEndpoint { + private String fileName; + private String endpointMethod; + + public RequestBodyEachEndpoint(String fileDir, String endpointMethod) { + this.fileName = fileDir; + this.endpointMethod = endpointMethod; + } + + public String getFileName() { + return fileName; + } + + public String getEndpointMethod() { + return endpointMethod; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public void setEndpointMethod(String endpointMethod) { + this.endpointMethod = endpointMethod; + } +} diff --git a/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/RequestBodyEndpoints.java b/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/RequestBodyEndpoints.java new file mode 100644 index 0000000..2965cf4 --- /dev/null +++ b/src/main/java/gr/nikos/smartclideTDPrincipal/Analysis/RequestBodyEndpoints.java @@ -0,0 +1,49 @@ +package gr.nikos.smartclideTDPrincipal.Analysis; + +import java.util.List; + +public class RequestBodyEndpoints { + private String sonarQubeProjectKey; + private String gitUrl; + private String gitToken; + private List requestBodyEachEndpointList; + + public RequestBodyEndpoints(String sonarQubeProjectKey, String gitUrl, String gitToken, List requestBodyEachEndpointList) { + this.sonarQubeProjectKey = sonarQubeProjectKey; + this.gitUrl = gitUrl; + this.gitToken = gitToken; + this.requestBodyEachEndpointList= requestBodyEachEndpointList; + } + + public String getSonarQubeProjectKey() { + return sonarQubeProjectKey; + } + + public String getGitUrl() { + return gitUrl; + } + + public String getGitToken() { + return gitToken; + } + + public List getRequestBodyEachEndpointList() { + return requestBodyEachEndpointList; + } + + public void setSonarQubeProjectKey(String sonarQubeProjectKey) { + this.sonarQubeProjectKey = sonarQubeProjectKey; + } + + public void setGitUrl(String folderName) { + this.gitUrl = folderName; + } + + public void setGitToken(String gitToken) { + this.gitToken = gitToken; + } + + public void setRequestBodyEachEndpointList(List requestBodyEachEndpointList) { + this.requestBodyEachEndpointList = requestBodyEachEndpointList; + } +}