Skip to content

Commit

Permalink
update ruby analyzer.
Browse files Browse the repository at this point in the history
  • Loading branch information
catchcatchus committed Sep 5, 2024
1 parent 0ae5e82 commit 1073e2a
Show file tree
Hide file tree
Showing 11 changed files with 3,797 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package io.codety.scanner.analyzer.rubocop;

import io.codety.scanner.analyzer.rubocop.dto.RubocopFile;
import io.codety.scanner.analyzer.rubocop.dto.RubocopIssueLocation;
import io.codety.scanner.analyzer.rubocop.dto.RubocopOffense;
import io.codety.scanner.analyzer.rubocop.dto.RubocopRoot;
import io.codety.scanner.reporter.dto.CodeAnalysisIssueDto;
import io.codety.scanner.util.JsonFactoryUtil;

import java.util.ArrayList;
import java.util.List;

public class RubocopAnalyzerConverter {
public static List<CodeAnalysisIssueDto> convertResult(String errorOutput, String basePath) throws Exception {
ArrayList<CodeAnalysisIssueDto> codeAnalysisIssueDtos = new ArrayList<>();

RubocopRoot rubocopRoot = JsonFactoryUtil.objectMapper.readValue(errorOutput, RubocopRoot.class);
ArrayList<RubocopFile> files = rubocopRoot.getFiles();
if(files == null){
return codeAnalysisIssueDtos;
}

for(RubocopFile rubocopFile : files){

ArrayList<RubocopOffense> rubocopIssues = rubocopFile.getOffenses();
if(rubocopIssues == null || rubocopIssues.isEmpty()){
continue;
}

String path = rubocopFile.getPath();
if(path.startsWith(basePath)){
path = path.substring(basePath.length()+1);
}


for(RubocopOffense rubocopOffense : rubocopIssues){
CodeAnalysisIssueDto issueDto = new CodeAnalysisIssueDto();

String externalRuleId = rubocopOffense.getCop_name();
String[] split = externalRuleId.split("/");
String category = split[0].toLowerCase();
RubocopIssueLocation location = rubocopOffense.getLocation();
issueDto.setStartLineNumber(location.getStart_line());
issueDto.setEndLineNumber(location.getLast_line());
issueDto.setIssueCode(externalRuleId);
issueDto.setIssueCategory(category);
issueDto.setDescription(rubocopOffense.getMessage());
issueDto.setPriority(convertPriority(rubocopOffense.getSeverity()));
issueDto.setFilePath(path);
codeAnalysisIssueDtos.add(issueDto);
}
}


return codeAnalysisIssueDtos;
}

private static Integer convertPriority(String severity) {
Integer priority = 3;
if(severity.equalsIgnoreCase("info")){
priority = 1;
}else if(severity.equalsIgnoreCase("refactor")){
priority = 2;
}else if(severity.equalsIgnoreCase("convention")){
priority = 2;
}else if(severity.equalsIgnoreCase("warning")){
priority = 3;
}else if(severity.equalsIgnoreCase("error")){
priority = 4;
}else if(severity.equalsIgnoreCase("fatal")){
priority = 5;
}
return priority;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import io.codety.common.dto.LanguageType;
import io.codety.scanner.analyzer.CodeAnalyzerInterface;
import io.codety.scanner.analyzer.dto.AnalyzerConfigurationDetailDto;
import io.codety.scanner.reporter.dto.CodeAnalysisIssueDto;
import io.codety.scanner.reporter.dto.CodeAnalysisResultDto;
import io.codety.scanner.service.dto.AnalyzerRequest;
import io.codety.scanner.util.CodetyConsoleLogger;
import io.codety.scanner.util.RuntimeExecUtil;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -16,6 +20,39 @@ public class RubocopCodeAnalyzer implements CodeAnalyzerInterface {
@Override
public List<CodeAnalysisResultDto> analyzeCode(AnalyzerConfigurationDetailDto runnerConfiguration, AnalyzerRequest request) {
ArrayList<CodeAnalysisResultDto> codeAnalysisResultDtos = new ArrayList<>();

File file = runnerConfiguration.getFile();
//cppcheck . --suppressions-list=suppression.txt --xml 2>error1.txt
ArrayList<CodeAnalysisResultDto> list = new ArrayList();

String[] command;

String localGitRepoPath = request.getLocalGitRepoPath();
if(runnerConfiguration.getPayload() == null || runnerConfiguration.getPayload().isEmpty()){
command = new String[]{"rubocop", "--format", "json", localGitRepoPath};

}else{
String enabledRules = runnerConfiguration.getPayload();
command = new String[]{"rubocop", "--format", "json", "--only", enabledRules, localGitRepoPath};
}
try {

RuntimeExecUtil.RuntimeExecResult runtimeExecResult = RuntimeExecUtil.exec(command, null, 60, false, null);

String errorOutput = runtimeExecResult.getErrorOutput();
String successOutput = runtimeExecResult.getSuccessOutput();

CodeAnalysisResultDto resultDto = new CodeAnalysisResultDto(runnerConfiguration.getLanguage(), runnerConfiguration.getCodeAnalyzerType());
list.add(resultDto);

List<CodeAnalysisIssueDto> codeAnalysisIssueDtoList = RubocopAnalyzerConverter.convertResult(errorOutput, localGitRepoPath);
resultDto.addIssues(codeAnalysisIssueDtoList);

} catch (Exception e) {
CodetyConsoleLogger.debug("Failed to run Rubocop analyzer ", e);
CodetyConsoleLogger.info("Failed to run Rubocop analyzer " + e.getMessage());
}

return codeAnalysisResultDtos;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.codety.scanner.analyzer.rubocop.dto;

import java.util.ArrayList;

// import com.fasterxml.jackson.databind.ObjectMapper; // version 2.11.1
// import com.fasterxml.jackson.annotation.JsonProperty; // version 2.11.1
/* ObjectMapper om = new ObjectMapper();
Root root = om.readValue(myJsonString, Root.class); */
public class RubocopFile {
private String path;
private ArrayList<RubocopOffense> offenses;

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public ArrayList<RubocopOffense> getOffenses() {
return offenses;
}

public void setOffenses(ArrayList<RubocopOffense> offenses) {
this.offenses = offenses;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.codety.scanner.analyzer.rubocop.dto;

public class RubocopIssueLocation {
private Integer start_line;
private Integer start_column;
private Integer last_line;
private Integer last_column;
private Integer length;
private Integer line;
private Integer column;

public Integer getStart_line() {
return start_line;
}

public void setStart_line(Integer start_line) {
this.start_line = start_line;
}

public Integer getStart_column() {
return start_column;
}

public void setStart_column(Integer start_column) {
this.start_column = start_column;
}

public Integer getLast_line() {
return last_line;
}

public void setLast_line(Integer last_line) {
this.last_line = last_line;
}

public Integer getLast_column() {
return last_column;
}

public void setLast_column(Integer last_column) {
this.last_column = last_column;
}

public Integer getLength() {
return length;
}

public void setLength(Integer length) {
this.length = length;
}

public Integer getLine() {
return line;
}

public void setLine(Integer line) {
this.line = line;
}

public Integer getColumn() {
return column;
}

public void setColumn(Integer column) {
this.column = column;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.codety.scanner.analyzer.rubocop.dto;

public class RubocopMetadata {
private String rubocop_version;
private String ruby_engine;
private String ruby_version;
private String ruby_patchlevel;
private String ruby_platform;

public String getRubocop_version() {
return rubocop_version;
}

public void setRubocop_version(String rubocop_version) {
this.rubocop_version = rubocop_version;
}

public String getRuby_engine() {
return ruby_engine;
}

public void setRuby_engine(String ruby_engine) {
this.ruby_engine = ruby_engine;
}

public String getRuby_version() {
return ruby_version;
}

public void setRuby_version(String ruby_version) {
this.ruby_version = ruby_version;
}

public String getRuby_patchlevel() {
return ruby_patchlevel;
}

public void setRuby_patchlevel(String ruby_patchlevel) {
this.ruby_patchlevel = ruby_patchlevel;
}

public String getRuby_platform() {
return ruby_platform;
}

public void setRuby_platform(String ruby_platform) {
this.ruby_platform = ruby_platform;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.codety.scanner.analyzer.rubocop.dto;

public class RubocopOffense {
private String severity;
private String message;
private String cop_name;
private boolean corrected;
private boolean correctable;
private RubocopIssueLocation location;

public String getSeverity() {
return severity;
}

public void setSeverity(String severity) {
this.severity = severity;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getCop_name() {
return cop_name;
}

public void setCop_name(String cop_name) {
this.cop_name = cop_name;
}

public boolean isCorrected() {
return corrected;
}

public void setCorrected(boolean corrected) {
this.corrected = corrected;
}

public boolean isCorrectable() {
return correctable;
}

public void setCorrectable(boolean correctable) {
this.correctable = correctable;
}

public RubocopIssueLocation getLocation() {
return location;
}

public void setLocation(RubocopIssueLocation location) {
this.location = location;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.codety.scanner.analyzer.rubocop.dto;

import java.util.ArrayList;

public class RubocopRoot {
private RubocopMetadata metadata;
private ArrayList<RubocopFile> files;
private RubocopSummary summary;

public RubocopMetadata getMetadata() {
return metadata;
}

public void setMetadata(RubocopMetadata metadata) {
this.metadata = metadata;
}

public ArrayList<RubocopFile> getFiles() {
return files;
}

public void setFiles(ArrayList<RubocopFile> files) {
this.files = files;
}

public RubocopSummary getSummary() {
return summary;
}

public void setSummary(RubocopSummary summary) {
this.summary = summary;
}
}
Loading

0 comments on commit 1073e2a

Please sign in to comment.