private Node createProjectHierarchy(ScannerReport.Component rootComponent) { Preconditions.checkArgument(rootComponent.getType() == ScannerReport.Component.ComponentType.PROJECT, "Expected root component of type 'PROJECT'"); LinkedList<ScannerReport.Component> queue = new LinkedList<>(); rootComponent.getChildRefList() .stream() .map(scannerComponentSupplier) .forEach(queue::addLast); Node root = new Node(); root.reportComponent = rootComponent; while (!queue.isEmpty()) { ScannerReport.Component component = queue.removeFirst(); switch (component.getType()) { case FILE: addFile(root, component); break; case MODULE: case DIRECTORY: issueRelocationToRoot.relocate(rootComponent, component); component.getChildRefList().stream() .map(scannerComponentSupplier) .forEach(queue::addLast); break; default: throw new IllegalArgumentException(format("Unsupported component type '%s'", component.getType())); } } return root; }
/** * This should only be needed if we receive a report of the previous version without the path per module explicitly set * (due to blue/green deployment) * Can be removed in any future version */ private Map<String, String> collectModulesPathFromHierarchy(ScannerReport.Metadata metadata) { ScannerReport.Component root = reader.readComponent(metadata.getRootComponentRef()); Map<String, String> modulesPathByKey = new LinkedHashMap<>(); LinkedList<Integer> queue = new LinkedList<>(); queue.addAll(root.getChildRefList()); while (!queue.isEmpty()) { ScannerReport.Component component = reader.readComponent(queue.removeFirst()); if (component.getType() == ScannerReport.Component.ComponentType.MODULE) { queue.addAll(component.getChildRefList()); modulesPathByKey.put(component.getKey(), component.getProjectRelativePath()); } } return modulesPathByKey; }
private void loadChildren(Component parentComponent, DefaultMutableTreeNode parentNode) { for (int ref : parentComponent.getChildRefList()) { Component child = reader.readComponent(ref); DefaultMutableTreeNode childNode = createNode(child); parentNode.add(childNode); loadChildren(child, childNode); } }
private List<Component> buildChildren(ScannerReport.Component component, ScannerReport.Component parentModule, String projectScmPath) { return component.getChildRefList() .stream() .map(scannerComponentSupplier::apply) .map(c -> buildComponent(c, parentModule, projectScmPath)) .collect(Collectors.toList()); }
private void loadChildren(Component parentComponent, DefaultMutableTreeNode parentNode) { for (int ref : parentComponent.getChildRefList()) { Component child = reader.readComponent(ref); DefaultMutableTreeNode childNode = createNode(child); parentNode.add(childNode); loadChildren(child, childNode); } }
@Test public void write_component() { // no data yet assertThat(underTest.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isFalse(); // write data ScannerReport.Component.Builder component = ScannerReport.Component.newBuilder() .setRef(1) .setLanguage("java") .setProjectRelativePath("src/Foo.java") .setType(ComponentType.FILE) .setIsTest(false) .addChildRef(5) .addChildRef(42); underTest.writeComponent(component.build()); assertThat(underTest.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue(); File file = underTest.getFileStructure().fileFor(FileStructure.Domain.COMPONENT, 1); assertThat(file).exists().isFile(); ScannerReport.Component read = Protobuf.read(file, ScannerReport.Component.parser()); assertThat(read.getRef()).isEqualTo(1); assertThat(read.getChildRefList()).containsOnly(5, 42); assertThat(read.getName()).isEmpty(); assertThat(read.getIsTest()).isFalse(); }
private ScannerReport.Changesets getChangesets(File baseDir, String path) { File reportDir = new File(baseDir, ".sonar/scanner-report"); ScannerReportReader reader = new ScannerReportReader(reportDir); Component project = reader.readComponent(reader.readMetadata().getRootComponentRef()); for (Integer fileRef : project.getChildRefList()) { Component file = reader.readComponent(fileRef); if (file.getProjectRelativePath().equals(path)) { return reader.readChangesets(file.getRef()); } } return null; }