声纳插件,如何保存违规行为

时间:2014-04-04 15:48:55

标签: java plugins sonarqube save

我正在开发一个声纳插件来使用tslint分析TrueScript代码。

我从[github.com/SonarSource/sonar-examples]下载了exampled插件并编辑了ExampleSensor.java [github.com/SonarSource/sonar-examples/tree/master/plugins/sonar-reference-plugin/src/main / JAVA / COM / myCompany的/声纳/参考/批。现在我的传感器看起来像在这个文件中

package com.mycompany.sonar.reference.batch;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;

import org.apache.commons.io.IOUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.Violation;

import pl.sollers.utils.FileSearcher;

public class ExampleSensor implements Sensor {

        private static final Logger LOG = LoggerFactory.getLogger(ExampleSensor.class);

        private Settings settings;

        /**
         * Use of IoC to get Settings
         */
        public ExampleSensor(Settings settings) {
                this.settings = settings;
        }

        public boolean shouldExecuteOnProject(Project project) {
                // This sensor is executed only for ts project type
                return project.getLanguageKey().equals("js");
        }

        public void analyse(Project project, SensorContext sensorContext) {
                // getting all files for analyzing
                Collection<File> files = FileSearcher.getByExtensionRecursively("ts");

                Process tslintProces;
                JSONParser jsonParser = new JSONParser();
                JSONArray warnings;
                Object jsonObj;

                for (File file : files) {
                        try {
                                // run tslint process and analyze file
                                tslintProces = new ProcessBuilder("tslint.cmd", "-c", "./tslint.json", "-t",
                                                "json", "-f", file.getCanonicalPath()).start();

                                // copy tslint output
                                StringWriter writer = new StringWriter();
                                IOUtils.copy(tslintProces.getInputStream(), writer, "UTF-8");
                                String json = writer.toString();

                                // parse output and extract violation message, ruleName, line
                                jsonObj = jsonParser.parse(json);
                                if (jsonObj instanceof JSONArray) {
                                        warnings = (JSONArray) (jsonObj);
                                } else {
                                        throw new Exception("Oczekiwano obiektu klasy JSONArray");
                                }

                                for (int i = 0; i < warnings.size(); i++) {
                                        JSONObject warning = (JSONObject) warnings.get(i);

                                        String message = (String) warning.get("failure");
                                        String ruleName = (String) warning.get("ruleName");

                                        JSONObject position = (JSONObject) warning.get("startPosition");
                                        Long line = (Long) position.get("line");
                                        // nie widzę pola w Sonarze aby użyć numeru znaku w linii
                                        Long character = (Long) position.get("character");

                                        // HELP! I DO NOT KNOW HOW TO STORE VIOLATION IN SONAR
                                        // FOLLOWING LINES DOES NOT WORK
                                        // Rule rule = Rule.create("repositoryKey",
                                                        String.format("%s-%s", "key", ruleName), ruleName);
                                        // org.sonar.api.resources.File resource = new org.sonar.api.resources.File(file
                                                        .getParentFile().getCanonicalPath(), file.getName());

                                        // Violation violation = Violation.create(rule, resource);
                                        // violation.setLineId(line.intValue());
                                        // violation.setMessage(message);
                                        // sensorContext.saveViolation(violation);
                                }
                        } catch (IOException e) {
                                LOG.error(e.getMessage());
                                e.printStackTrace();
                        } catch (ParseException e) {
                                LOG.error(e.getMessage());
                                e.printStackTrace();
                        } catch (Exception e) {
                                LOG.error(e.getMessage());
                        }
                }
        }

        @Override
        public String toString() {
                return getClass().getSimpleName();
        }
}

我在保存Sonar数据库中的违规方面遇到了问题。这部分代码应该在第80-90行。任何人都可以帮我存储违规行为。

1 个答案:

答案 0 :(得分:0)

您应该使用Issueable而非违规行为(因为它已被弃用,将在4.3中删除)。

 import org.sonar.api.component.ResourcePerspectives;
 public class MySensor extends Sensor {
   private final ResourcePerspectives perspectives;

   public MySensor(ResourcePerspectives p) {
     this.perspectives = p;
   }

   public void analyse(Project project, SensorContext context) {
     Resource myResource; // to be set
     Issuable issuable = perspectives.as(Issuable.class, myResource);
     if (issuable != null) {
       // can be used
       Issue issue = issuable.newIssueBuilder()
         .setRuleKey(RuleKey.of("pmd", "AvoidArrayLoops")
         .setLine(10)
         .build();
       issuable.addIssue(issue);
     }
   }
 }