diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2019-11-07 15:21:33 -0600 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-12-09 20:46:15 +0100 |
commit | a30ba90a56f2abb857af67bb96d9cb1339afdd24 (patch) | |
tree | 569df3393338d6a4e1a4c29f0c018e28d7d13bfb /plugins | |
parent | 63c9fb57a0f00f06c782cec8d2ffdf8d9e8b2f1b (diff) | |
download | sonarqube-a30ba90a56f2abb857af67bb96d9cb1339afdd24.tar.gz sonarqube-a30ba90a56f2abb857af67bb96d9cb1339afdd24.zip |
SONAR-12627 Issues are 'new' if they involve a location on a new line
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooBlameCommand.java | 2 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooScmProvider.java | 111 |
2 files changed, 111 insertions, 2 deletions
diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooBlameCommand.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooBlameCommand.java index 9a803dd3b4c..e32e0b0c856 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooBlameCommand.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooBlameCommand.java @@ -77,7 +77,7 @@ public class XooBlameCommand extends BlameCommand { } private static BlameLine convertToBlameLine(CSVRecord csvRecord) { - checkState(csvRecord.size() == 3, "Not enough fields on line %s", csvRecord); + checkState(csvRecord.size() >= 3, "Not enough fields on line %s", csvRecord); String revision = trimToNull(csvRecord.get(0)); String author = trimToNull(csvRecord.get(1)); BlameLine blameLine = new BlameLine().revision(revision).author(author); diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooScmProvider.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooScmProvider.java index 856e6c829f9..ae2744c4abe 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooScmProvider.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooScmProvider.java @@ -19,18 +19,37 @@ */ package org.sonar.xoo.scm; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import javax.annotation.CheckForNull; +import org.apache.commons.lang.StringUtils; import org.sonar.api.batch.scm.BlameCommand; import org.sonar.api.batch.scm.IgnoreCommand; import org.sonar.api.batch.scm.ScmProvider; import java.io.File; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; public class XooScmProvider extends ScmProvider { + private static final Logger LOG = Loggers.get(XooScmProvider.class); + private static final String SCM_EXTENSION = ".scm"; private final XooBlameCommand blame; private XooIgnoreCommand ignore; - public XooScmProvider(XooBlameCommand blame, XooIgnoreCommand ignore) { + public XooScmProvider(XooBlameCommand blame, XooIgnoreCommand ignore) { this.blame = blame; this.ignore = ignore; } @@ -54,4 +73,94 @@ public class XooScmProvider extends ScmProvider { public IgnoreCommand ignoreCommand() { return ignore; } + + @Override + public String revisionId(Path path) { + return "fakeSha1FromXoo"; + } + + @CheckForNull + public Set<Path> branchChangedFiles(String targetBranchName, Path rootBaseDir) { + Set<Path> changedFiles = new HashSet<>(); + Set<Path> scmFiles = new HashSet<>(); + + try { + Files.walkFileTree(rootBaseDir, new SimpleFileVisitor<Path>() { + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { + if (file.getFileName().toString().endsWith(".scm")) { + scmFiles.add(file); + } + return FileVisitResult.CONTINUE; + } + }); + + if (scmFiles.isEmpty()) { + return null; + } + + for (Path scmFilePath : scmFiles) { + String fileName = scmFilePath.getFileName().toString(); + Path filePath = scmFilePath.resolveSibling(fileName.substring(0, fileName.length() - 4)); + if (!Files.exists(filePath)) { + continue; + } + Set<Integer> newLines = loadNewLines(scmFilePath); + if (newLines == null) { + return null; + } + if (!newLines.isEmpty()) { + changedFiles.add(filePath); + } + } + + } catch (IOException e) { + throw new IllegalStateException("Failed to find scm files", e); + } + + return changedFiles; + } + + @CheckForNull + public Map<Path, Set<Integer>> branchChangedLines(String targetBranchName, Path rootBaseDir, Set<Path> files) { + Map<Path, Set<Integer>> map = new HashMap<>(); + for (Path filePath : files) { + Path scmFilePath = filePath.resolveSibling(filePath.getFileName() + SCM_EXTENSION); + try { + Set<Integer> newLines = loadNewLines(scmFilePath); + if (newLines != null && !newLines.isEmpty()) { + map.put(filePath, newLines); + } + } catch (IOException e) { + throw new IllegalStateException("Failed to parse scm file", e); + } + + } + return map.isEmpty() ? null : map; + } + + // return null when any of the scm files lack the is-new flag (column 4) + @CheckForNull + private Set<Integer> loadNewLines(Path filePath) throws IOException { + if (!Files.isRegularFile(filePath)) { + return Collections.emptySet(); + } + LOG.debug("Processing " + filePath.toAbsolutePath()); + Set<Integer> newLines = new HashSet<>(); + List<String> lines = Files.readAllLines(filePath, StandardCharsets.UTF_8); + int lineNum = 0; + boolean foundNewLineFlag = false; + for (String line : lines) { + lineNum++; + String[] fields = StringUtils.splitPreserveAllTokens(line, ','); + if (fields.length < 4) { + continue; + } + foundNewLineFlag = true; + + if (Boolean.parseBoolean(fields[3])) { + newLines.add(lineNum); + } + } + return foundNewLineFlag ? newLines : null; + } } |