|
| 1 | +/* |
| 2 | + * SonarLint CLI |
| 3 | + * Copyright (C) 2016-2017 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonarlint.cli.report; |
| 21 | + |
| 22 | +import com.google.gson.JsonObject; |
| 23 | +import com.google.gson.JsonArray; |
| 24 | +import java.io.PrintStream; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.Date; |
| 27 | +import java.util.function.Function; |
| 28 | +import org.sonarlint.cli.util.Logger; |
| 29 | +import org.sonarsource.sonarlint.core.client.api.common.RuleDetails; |
| 30 | +import org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults; |
| 31 | +import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue; |
| 32 | +import org.sonarsource.sonarlint.core.tracking.Trackable; |
| 33 | + |
| 34 | +public class JsonReport implements Reporter { |
| 35 | + |
| 36 | + private static final Logger LOGGER = Logger.get(); |
| 37 | + |
| 38 | + JsonReport() { |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void execute(String projectName, Date date, Collection<Trackable> trackables, AnalysisResults result, Function<String, RuleDetails> ruleDescriptionProducer) { |
| 44 | + for (Trackable trackable : trackables) { |
| 45 | + Issue issue = trackable.getIssue(); |
| 46 | + RuleDetails ruleDetails = ruleDescriptionProducer.apply(issue.getRuleKey()); |
| 47 | + |
| 48 | + JsonObject json = new JsonObject(); |
| 49 | + json.addProperty("type", "issue"); |
| 50 | + json.addProperty("check_name", issue.getRuleKey()); |
| 51 | + |
| 52 | + JsonArray categories = new JsonArray(); |
| 53 | + json.add("categories", categories); |
| 54 | + categories.add("Bug Risk"); |
| 55 | + |
| 56 | + json.addProperty("description", issue.getMessage()); |
| 57 | + |
| 58 | + JsonObject content = new JsonObject(); |
| 59 | + json.add("content", content); |
| 60 | + content.addProperty("body", ruleDetails.getHtmlDescription()); |
| 61 | + // // ruleDetails.getExtendedDescription(); |
| 62 | + |
| 63 | + JsonObject location = new JsonObject(); |
| 64 | + json.add("location", location); |
| 65 | + location.addProperty("path", issue.getInputFile().getPath().replaceFirst("^/code-rw/", "")); |
| 66 | + |
| 67 | + JsonObject lines = new JsonObject(); |
| 68 | + location.add("lines", lines); |
| 69 | + |
| 70 | + if (issue.getStartLine() != null) { |
| 71 | + lines.addProperty("begin", issue.getStartLine()); |
| 72 | + |
| 73 | + if (issue.getStartLine() != null) { |
| 74 | + lines.addProperty("end", issue.getEndLine()); |
| 75 | + } else { |
| 76 | + lines.addProperty("end", 1); |
| 77 | + } |
| 78 | + } else { |
| 79 | + lines.addProperty("begin", 1); |
| 80 | + lines.addProperty("end", 1); |
| 81 | + } |
| 82 | + |
| 83 | + System.out.println(json.toString() + "\0"); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments