@Test public void testScmMeasure() throws IOException, URISyntaxException { File baseDir = prepareProject(); tester.newAnalysis() .properties(ImmutableMap.<String, String>builder() .put("sonar.task", "scan") .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) .put("sonar.projectKey", "com.foo.project") .put("sonar.projectName", "Foo Project") .put("sonar.projectVersion", "1.0-SNAPSHOT") .put("sonar.projectDescription", "Description of Foo Project") .put("sonar.sources", "src") .put("sonar.scm.provider", "xoo") .build()) .execute(); ScannerReport.Changesets fileScm = getChangesets(baseDir, "src/sample.xoo"); assertThat(fileScm.getChangesetIndexByLineList()).hasSize(5); Changeset changesetLine1 = fileScm.getChangeset(fileScm.getChangesetIndexByLine(0)); assertThat(changesetLine1.getAuthor()).isEmpty(); Changeset changesetLine2 = fileScm.getChangeset(fileScm.getChangesetIndexByLine(1)); assertThat(changesetLine2.getAuthor()).isEqualTo(getNonAsciiAuthor().toLowerCase()); Changeset changesetLine3 = fileScm.getChangeset(fileScm.getChangesetIndexByLine(2)); assertThat(changesetLine3.getAuthor()).isEqualTo("julien"); Changeset changesetLine4 = fileScm.getChangeset(fileScm.getChangesetIndexByLine(3)); assertThat(changesetLine4.getAuthor()).isEqualTo("julien"); Changeset changesetLine5 = fileScm.getChangeset(fileScm.getChangesetIndexByLine(4)); assertThat(changesetLine5.getAuthor()).isEqualTo("simon"); }
public BatchReportReaderRule putChangesets(ScannerReport.Changesets changesets) { this.changesets.put(changesets.getComponentRef(), changesets); return this; }
assertThat(getChangesets(baseDir, CHANGED_CONTENT_SCM_ON_SERVER_XOO).getCopyFromPrevious()).isFalse(); assertThat(getChangesets(baseDir, SAME_CONTENT_SCM_ON_SERVER_XOO).getCopyFromPrevious()).isTrue(); assertThat(getChangesets(baseDir, SAME_CONTENT_NO_SCM_ON_SERVER_XOO).getCopyFromPrevious()).isFalse();
@Override public synchronized void blameResult(InputFile file, List<BlameLine> lines) { Preconditions.checkNotNull(file); Preconditions.checkNotNull(lines); Preconditions.checkArgument(allFilesToBlame.contains(file), "It was not expected to blame file %s", file); if (lines.size() != file.lines()) { LOG.debug("Ignoring blame result since provider returned {} blame lines but file {} has {} lines", lines.size(), file, file.lines()); return; } Builder scmBuilder = ScannerReport.Changesets.newBuilder(); DefaultInputFile inputFile = (DefaultInputFile) file; scmBuilder.setComponentRef(inputFile.scannerId()); Map<String, Integer> changesetsIdByRevision = new HashMap<>(); int lineId = 1; for (BlameLine line : lines) { validateLine(line, lineId, file); Integer changesetId = changesetsIdByRevision.get(line.revision()); if (changesetId == null) { addChangeset(scmBuilder, line); changesetId = scmBuilder.getChangesetCount() - 1; changesetsIdByRevision.put(line.revision(), changesetId); } scmBuilder.addChangesetIndexByLine(changesetId); lineId++; } writer.writeComponentChangesets(scmBuilder.build()); allFilesToBlame.remove(file); count++; progressReport.message(count + "/" + total + " files analyzed"); }
private void updateScm(Component component) { scmEditor.setText(""); Changesets changesets = reader.readChangesets(component.getRef()); if (changesets == null) { return; } List<Integer> changesetIndexByLine = changesets.getChangesetIndexByLineList(); try { int index = 0; for (Changeset changeset : changesets.getChangesetList()) { scmEditor.getDocument().insertString(scmEditor.getDocument().getEndPosition().getOffset(), Integer.toString(index) + "\n", null); scmEditor.getDocument().insertString(scmEditor.getDocument().getEndPosition().getOffset(), changeset + "\n", null); index++; } scmEditor.getDocument().insertString(scmEditor.getDocument().getEndPosition().getOffset(), "\n", null); int line = 1; for (Integer idx : changesetIndexByLine) { scmEditor.getDocument().insertString(scmEditor.getDocument().getEndPosition().getOffset(), Integer.toString(line) + ": " + idx + "\n", null); line++; } } catch (Exception e) { throw new IllegalStateException("Can't read SCM for " + getNodeName(component), e); } }
public LineIndexToChangeset(ScannerReport.Changesets changesets) { this.changesets = changesets; changesetCache = new HashMap<>(changesets.getChangesetCount()); }
private static void askToCopyDataFromPreviousAnalysis(DefaultInputFile f, ScannerReportWriter writer) { Builder scmBuilder = ScannerReport.Changesets.newBuilder(); scmBuilder.setComponentRef(f.scannerId()); scmBuilder.setCopyFromPrevious(true); writer.writeComponentChangesets(scmBuilder.build()); }
private Optional<ScmInfo> getScmInfoForComponent(Component component) { ScannerReport.Changesets changesets = scannerReportReader.readChangesets(component.getReportAttributes().getRef()); if (changesets == null) { LOGGER.trace("No SCM info for file '{}'", component.getKey()); // SCM not available. It might have been available before - copy information for unchanged lines but don't keep author and revision. return generateAndMergeDb(component, false); } // will be empty if the flag "copy from previous" is set, or if the file is empty. if (changesets.getChangesetCount() == 0) { return generateAndMergeDb(component, changesets.getCopyFromPrevious()); } return getScmInfoFromReport(component, changesets); }
private static ScmInfo convertToScmInfo(ScannerReport.Changesets changesets) { return new ScmInfoImpl(IntStream.rangeClosed(1, changesets.getChangesetIndexByLineCount()) .boxed() .collect(Collectors.toMap(x -> x, new LineIndexToChangeset(changesets), MoreCollectors.mergeNotSupportedMerger(), LinkedHashMap::new))); }
@CheckForNull public ScannerReport.Changesets readChangesets(int componentRef) { File file = fileStructure.fileFor(FileStructure.Domain.CHANGESETS, componentRef); if (fileExists(file)) { return Protobuf.read(file, ScannerReport.Changesets.parser()); } return null; }
public File writeComponentChangesets(ScannerReport.Changesets changesets) { File file = fileStructure.fileFor(FileStructure.Domain.CHANGESETS, changesets.getComponentRef()); Protobuf.write(changesets, file); return file; }
@Override public Changeset apply(Integer lineNumber) { int changesetIndex = changesets.getChangesetIndexByLine(lineNumber - 1); return changesetCache.computeIfAbsent(changesetIndex, idx -> convert(changesets.getChangeset(changesetIndex), lineNumber)); }
@Override public synchronized void blameResult(InputFile file, List<BlameLine> lines) { Preconditions.checkNotNull(file); Preconditions.checkNotNull(lines); Preconditions.checkArgument(allFilesToBlame.contains(file), "It was not expected to blame file %s", file); if (lines.size() != file.lines()) { LOG.debug("Ignoring blame result since provider returned {} blame lines but file {} has {} lines", lines.size(), file, file.lines()); return; } Builder scmBuilder = ScannerReport.Changesets.newBuilder(); DefaultInputFile inputFile = (DefaultInputFile) file; scmBuilder.setComponentRef(inputFile.scannerId()); Map<String, Integer> changesetsIdByRevision = new HashMap<>(); int lineId = 1; for (BlameLine line : lines) { validateLine(line, lineId, file); Integer changesetId = changesetsIdByRevision.get(line.revision()); if (changesetId == null) { addChangeset(scmBuilder, line); changesetId = scmBuilder.getChangesetCount() - 1; changesetsIdByRevision.put(line.revision(), changesetId); } scmBuilder.addChangesetIndexByLine(changesetId); lineId++; } writer.writeComponentChangesets(scmBuilder.build()); allFilesToBlame.remove(file); count++; progressReport.message(count + "/" + total + " files analyzed"); }
@Test public void return_changeset_for_a_given_line() { ScmInfo scmInfo = new ReportScmInfo(ScannerReport.Changesets.newBuilder() .setComponentRef(FILE_REF) .addChangeset(ScannerReport.Changesets.Changeset.newBuilder() .setAuthor("john") .setDate(123456789L) .setRevision("rev-1") .build()) .addChangeset(ScannerReport.Changesets.Changeset.newBuilder() .setAuthor("henry") .setDate(1234567810L) .setRevision("rev-2") .build()) .addChangesetIndexByLine(0) .addChangesetIndexByLine(1) .addChangesetIndexByLine(1) .addChangesetIndexByLine(0) .build()); assertThat(scmInfo.getAllChangesets()).hasSize(4); Changeset changeset = scmInfo.getChangesetForLine(4); assertThat(changeset.getAuthor()).isEqualTo("john"); assertThat(changeset.getDate()).isEqualTo(123456789L); assertThat(changeset.getRevision()).isEqualTo("rev-1"); }
@Test public void return_latest_changeset() { ScmInfo scmInfo = new ReportScmInfo(ScannerReport.Changesets.newBuilder() .setComponentRef(FILE_REF) .addChangeset(ScannerReport.Changesets.Changeset.newBuilder() .setAuthor("john") .setDate(123456789L) .setRevision("rev-1") .build()) // Older changeset .addChangeset(ScannerReport.Changesets.Changeset.newBuilder() .setAuthor("henry") .setDate(1234567810L) .setRevision("rev-2") .build()) .addChangesetIndexByLine(0) .addChangesetIndexByLine(1) .addChangesetIndexByLine(0) .build()); Changeset latestChangeset = scmInfo.getLatestChangeset(); assertThat(latestChangeset.getAuthor()).isEqualTo("henry"); assertThat(latestChangeset.getDate()).isEqualTo(1234567810L); assertThat(latestChangeset.getRevision()).isEqualTo("rev-2"); }
@Test public void create_scm_info_with_some_changesets() { ScmInfo scmInfo = new ReportScmInfo(ScannerReport.Changesets.newBuilder() .setComponentRef(FILE_REF) .addChangeset(ScannerReport.Changesets.Changeset.newBuilder() .setAuthor("john") .setDate(123456789L) .setRevision("rev-1") .build()) .addChangeset(ScannerReport.Changesets.Changeset.newBuilder() .setAuthor("henry") .setDate(1234567810L) .setRevision("rev-2") .build()) .addChangesetIndexByLine(0) .addChangesetIndexByLine(1) .addChangesetIndexByLine(0) .addChangesetIndexByLine(0) .build()); assertThat(scmInfo.getAllChangesets()).hasSize(4); }
private void updateScm(Component component) { scmEditor.setText(""); Changesets changesets = reader.readChangesets(component.getRef()); if (changesets == null) { return; } List<Integer> changesetIndexByLine = changesets.getChangesetIndexByLineList(); try { int index = 0; for (Changeset changeset : changesets.getChangesetList()) { scmEditor.getDocument().insertString(scmEditor.getDocument().getEndPosition().getOffset(), Integer.toString(index) + "\n", null); scmEditor.getDocument().insertString(scmEditor.getDocument().getEndPosition().getOffset(), changeset + "\n", null); index++; } scmEditor.getDocument().insertString(scmEditor.getDocument().getEndPosition().getOffset(), "\n", null); int line = 1; for (Integer idx : changesetIndexByLine) { scmEditor.getDocument().insertString(scmEditor.getDocument().getEndPosition().getOffset(), Integer.toString(line) + ": " + idx + "\n", null); line++; } } catch (Exception e) { throw new IllegalStateException("Can't read SCM for " + getNodeName(component), e); } }
private void addCopyFromPrevious() { reportReader.putChangesets(Changesets.newBuilder().setComponentRef(FILE_REF).setCopyFromPrevious(true).build()); }
@Test public void fail_with_ISE_when_changeset_has_no_revision() { thrown.expect(IllegalStateException.class); thrown.expectMessage("Changeset on line 1 must have a revision"); new ReportScmInfo(ScannerReport.Changesets.newBuilder() .setComponentRef(FILE_REF) .addChangeset(ScannerReport.Changesets.Changeset.newBuilder() .setAuthor("john") .setDate(123456789L) .build()) .addChangesetIndexByLine(0) .build()); }
private void addChangesetInReport(String author, Long date, String revision, boolean copyFromPrevious) { reportReader.putChangesets(ScannerReport.Changesets.newBuilder() .setComponentRef(FILE_REF) .setCopyFromPrevious(copyFromPrevious) .addChangeset(ScannerReport.Changesets.Changeset.newBuilder() .setAuthor(author) .setDate(date) .setRevision(revision) .build()) .addChangesetIndexByLine(0) .build()); }