Skip to content

Commit

Permalink
Add Post for private projects for given endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosnikolaidis committed Mar 18, 2022
1 parent 27c541e commit 571f1ee
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,22 @@ public List<Issue> getIssues(@PathVariable(value = "projectKey") String projectK

@CrossOrigin(origins = "*")
@GetMapping(path="endpoints")
public HashMap<String,Report> getEndpointMetrics(@RequestParam(required = true) String url) {
public List<Report> getEndpointMetrics(@RequestParam(required = true) String url) {
return endpointAnalysisService.getEnpointMetrics(url);
}

@CrossOrigin(origins = "*")
@PostMapping(path="endpoints")
public List<Report> getEndpointMetricsPrivate(@RequestBody RequestBodyEndpoints requestBodyEndpoints) {
return endpointAnalysisService.getEndpointMetricsPrivate(requestBodyEndpoints);
}

// @CrossOrigin(origins = "*")
// @PostMapping(path="endpoints")
// public HashMap<String,Report> getEndpointMetricsLocalGitlabSonar(@RequestBody RequestBodyEndpoints requestBodyEndpoints) {
// return endpointAnalysisService.getEnpointMetricsLocal(requestBodyEndpoints);
// }

@CrossOrigin(origins = "*")
@PostMapping
@ResponseStatus(HttpStatus.OK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public class EndpointAnalysisService {
private AnalysisService analysisService;

private HashMap<MethodDeclaration, String> methodsOfStartingEndpoints= new HashMap<>();
private List<File> allJavaFiles = new ArrayList<>();

public HashMap<String,Report> getEnpointMetrics(String url) {
public List<Report> getEnpointMetrics(String url) {
try {
methodsOfStartingEndpoints.clear();

Expand All @@ -56,28 +57,90 @@ public HashMap<String,Report> getEnpointMetrics(String url) {
}

//Get Report
HashMap<String, Report> hashMap= GetAllReportForAllEndpoints(gitName, projectKey);
List<Report> reportList= GetAllReportForAllEndpoints("/"+gitName, projectKey);

//delete clone
FileSystemUtils.deleteRecursively(new File("/"+gitName));

return hashMap;
return reportList;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

public List<Report> 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<Report> reportList= GetAllReportForAllEndpoints("/"+gitName, projectKey);

//delete clone
FileSystemUtils.deleteRecursively(new File("/"+gitName));

return reportList;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

//Start method tracing and map the issues to endpoints
private HashMap<String, Report> GetAllReportForAllEndpoints(String dirName, String projectKey) {
private List<Report> GetAllReportForAllEndpoints(String dirName, String projectKey) {
System.out.println("Get all Issues");
List<Issue> allIssues= analysisService.getIssues(projectKey);
HashMap<String,Report> hashMap=new HashMap<>();
//HashMap<String,Report> hashMap=new HashMap<>();
List<Report> 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<MethodCallSet> methodCallSets = facade.start();
if (!Objects.isNull(methodCallSets)){
printResults(methodCallSets);
Expand All @@ -96,7 +159,7 @@ private HashMap<String, Report> 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);
}
}
Expand All @@ -115,17 +178,59 @@ private HashMap<String, Report> 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<RequestBodyEachEndpoint> 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<MethodDeclaration> methods = new ArrayList<MethodDeclaration>();

CompilationUnit cu = StaticJavaParser.parse(file);
VoidVisitor<List<MethodDeclaration>> 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<List<MethodDeclaration>> {
@Override
public void visit(MethodDeclaration md, List<MethodDeclaration> collector) {
super.visit(md, collector);
collector.add(md);
}
}

//For each file find endpoints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
import java.util.List;

public class Report {
private String method;
private Metric metrics;
private List<Issue> issueList;

public Report(Metric metrics, List<Issue> issueList) {
public Report(String method, Metric metrics, List<Issue> issueList) {
this.method = method;
this.metrics = metrics;
this.issueList = issueList;
}

public String getMethod() {
return method;
}

public Metric getMetrics() {
return metrics;
}
Expand All @@ -19,6 +25,10 @@ public List<Issue> getIssueList() {
return issueList;
}

public void setMethod(String method) {
this.method = method;
}

public void setMetrics(Metric metrics) {
this.metrics = metrics;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<RequestBodyEachEndpoint> requestBodyEachEndpointList;

public RequestBodyEndpoints(String sonarQubeProjectKey, String gitUrl, String gitToken, List<RequestBodyEachEndpoint> 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<RequestBodyEachEndpoint> 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<RequestBodyEachEndpoint> requestBodyEachEndpointList) {
this.requestBodyEachEndpointList = requestBodyEachEndpointList;
}
}

0 comments on commit 571f1ee

Please sign in to comment.