@Override
  public void publish(ScannerReportWriter writer) {
    AbstractProjectOrModule rootProject = moduleHierarchy.root();
    ScannerReport.Metadata.Builder builder = ScannerReport.Metadata.newBuilder()
      .setAnalysisDate(projectInfo.getAnalysisDate().getTime())
      // Here we want key without branch
      .setProjectKey(rootProject.key())
      .setCrossProjectDuplicationActivated(cpdSettings.isCrossProjectDuplicationEnabled())
      .setRootComponentRef(rootProject.scannerId());
    projectInfo.getProjectVersion().ifPresent(builder::setProjectVersion);
    projectInfo.getBuildString().ifPresent(builder::setBuildString);

    if (branchConfiguration.branchName() != null) {
      addBranchInformation(builder);
    }

    String newCodeReferenceBranch = referenceBranchSupplier.getFromProperties();
    if (newCodeReferenceBranch != null) {
      builder.setNewCodeReferenceBranch(newCodeReferenceBranch);
    }

    addScmInformation(builder);
    addNotAnalyzedFileCountsByLanguage(builder);

    for (QProfile qp : qProfiles.findAll()) {
      builder.putQprofilesPerLanguage(qp.getLanguage(), ScannerReport.Metadata.QProfile.newBuilder()
        .setKey(qp.getKey())
        .setLanguage(qp.getLanguage())
        .setName(qp.getName())
        .setRulesUpdatedAt(qp.getRulesUpdatedAt().getTime()).build());
    }
    for (Entry<String, ScannerPlugin> pluginEntry : pluginRepository.getPluginsByKey().entrySet()) {
      builder.putPluginsByKey(pluginEntry.getKey(), ScannerReport.Metadata.Plugin.newBuilder()
        .setKey(pluginEntry.getKey())
        .setUpdatedAt(pluginEntry.getValue().getUpdatedAt()).build());
    }

    addRelativePathFromScmRoot(builder);

    writer.writeMetadata(builder.build());
  }
@Test
  public void should_not_crash_when_scm_provider_does_not_support_relativePathFromScmRoot() {
    ScmProvider fakeScmProvider = new ScmProvider() {
      @Override
      public String key() {
        return "foo";
      }
    };
    when(scmConfiguration.provider()).thenReturn(fakeScmProvider);

    underTest.publish(writer);

    ScannerReport.Metadata metadata = reader.readMetadata();
    assertThat(metadata.getRelativePathFromScmRoot()).isEmpty();
  }