|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2020 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.sonar.plugins.python.flake8; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.LinkedList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Scanner; |
| 28 | +import java.util.Set; |
| 29 | +import javax.annotation.Nullable; |
| 30 | +import org.sonar.api.batch.fs.FileSystem; |
| 31 | +import org.sonar.api.batch.fs.InputFile; |
| 32 | +import org.sonar.api.batch.rule.ActiveRule; |
| 33 | +import org.sonar.api.batch.sensor.SensorContext; |
| 34 | +import org.sonar.api.batch.sensor.SensorDescriptor; |
| 35 | +import org.sonar.api.batch.sensor.issue.NewIssue; |
| 36 | +import org.sonar.api.config.Configuration; |
| 37 | +import org.sonar.api.rule.RuleKey; |
| 38 | +import org.sonar.api.utils.log.Logger; |
| 39 | +import org.sonar.api.utils.log.Loggers; |
| 40 | +import org.sonar.plugins.python.PythonReportSensor; |
| 41 | +import org.sonar.plugins.python.warnings.AnalysisWarningsWrapper; |
| 42 | + |
| 43 | +public class Flake8ImportSensor extends PythonReportSensor { |
| 44 | + public static final String REPORT_PATH_KEY = "sonar.python.flake8.reportPath"; |
| 45 | + private static final String DEFAULT_REPORT_PATH = "flake8-reports/flake8-result-*.txt"; |
| 46 | + |
| 47 | + private static final Logger LOG = Loggers.get(Flake8ImportSensor.class); |
| 48 | + private static final Flake8RuleParser flake8Rules = new Flake8RuleParser(Flake8RuleRepository.RULES_FILE); |
| 49 | + private static final Set<String> warningAlreadyLogged = new HashSet<>(); |
| 50 | + |
| 51 | + public Flake8ImportSensor(Configuration conf, AnalysisWarningsWrapper analysisWarnings) { |
| 52 | + super(conf, analysisWarnings, "Flake8"); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void describe(SensorDescriptor descriptor) { |
| 57 | + super.describe(descriptor); |
| 58 | + descriptor |
| 59 | + .createIssuesForRuleRepository(Flake8RuleRepository.REPOSITORY_KEY) |
| 60 | + .onlyWhenConfiguration(conf -> conf.hasKey(REPORT_PATH_KEY)); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected String reportPathKey() { |
| 65 | + return REPORT_PATH_KEY; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected String defaultReportPath() { |
| 70 | + return DEFAULT_REPORT_PATH; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void processReports(final SensorContext context, List<File> reports) { |
| 75 | + List<Issue> issues = new LinkedList<>(); |
| 76 | + for (File report : reports) { |
| 77 | + try { |
| 78 | + issues.addAll(parse(report, context.fileSystem())); |
| 79 | + } catch (java.io.FileNotFoundException e) { |
| 80 | + LOG.error("Report '{}' cannot be found, details: '{}'", report, e); |
| 81 | + } catch (IOException e) { |
| 82 | + LOG.error("Report '{}' cannot be read, details: '{}'", report, e); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + saveIssues(issues, context); |
| 87 | + } |
| 88 | + |
| 89 | + private static List<Issue> parse(File report, FileSystem fileSystem) throws IOException { |
| 90 | + List<Issue> issues = new LinkedList<>(); |
| 91 | + |
| 92 | + Flake8ReportParser parser = new Flake8ReportParser(); |
| 93 | + Scanner sc; |
| 94 | + for (sc = new Scanner(report.toPath(), fileSystem.encoding().name()); sc.hasNext(); ) { |
| 95 | + String line = sc.nextLine(); |
| 96 | + Issue issue = parser.parseLine(line); |
| 97 | + if (issue != null) { |
| 98 | + issues.add(issue); |
| 99 | + } |
| 100 | + } |
| 101 | + sc.close(); |
| 102 | + return issues; |
| 103 | + } |
| 104 | + |
| 105 | + private static void saveIssues(List<Issue> issues, SensorContext context) { |
| 106 | + FileSystem fileSystem = context.fileSystem(); |
| 107 | + for (Issue flake8Issue : issues) { |
| 108 | + String filepath = flake8Issue.getFilename(); |
| 109 | + InputFile pyfile = fileSystem.inputFile(fileSystem.predicates().hasPath(filepath)); |
| 110 | + if (pyfile != null) { |
| 111 | + ActiveRule rule = context.activeRules().find(RuleKey.of(Flake8RuleRepository.REPOSITORY_KEY, flake8Issue.getRuleId())); |
| 112 | + processRule(flake8Issue, pyfile, rule, context); |
| 113 | + } else { |
| 114 | + LOG.warn("Cannot find the file '{}' in SonarQube, ignoring violation", filepath); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public static void processRule(Issue flake8Issue, InputFile pyfile, @Nullable ActiveRule rule, SensorContext context) { |
| 120 | + if (rule != null) { |
| 121 | + NewIssue newIssue = context |
| 122 | + .newIssue() |
| 123 | + .forRule(rule.ruleKey()); |
| 124 | + newIssue.at( |
| 125 | + newIssue.newLocation() |
| 126 | + .on(pyfile) |
| 127 | + .at(pyfile.selectLine(flake8Issue.getLine())) |
| 128 | + .message(flake8Issue.getDescription())); |
| 129 | + newIssue.save(); |
| 130 | + } else if (!flake8Rules.hasRuleDefinition(flake8Issue.getRuleId())) { |
| 131 | + logUnknownRuleWarning(flake8Issue.getRuleId()); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static void logUnknownRuleWarning(String ruleId) { |
| 136 | + if (!warningAlreadyLogged.contains(ruleId)) { |
| 137 | + warningAlreadyLogged.add(ruleId); |
| 138 | + LOG.warn("Flake8 rule '{}' is unknown in Sonar", ruleId); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // Visible for testing |
| 143 | + static void clearLoggedWarnings() { |
| 144 | + warningAlreadyLogged.clear(); |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments