From 1e338c95b8620fb669169aa3fb980d37c08cbfc6 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 10 Feb 2017 12:06:04 +0100 Subject: [PATCH] SONAR-8769 Display project and organization keys in the analysis logs --- .../scanner/scan/ProjectScanContainer.java | 11 ++++- .../mediumtest/fs/FileSystemMediumTest.java | 41 ++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java index d9ddca72dbf..85450419f41 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java @@ -20,6 +20,8 @@ package org.sonar.scanner.scan; import com.google.common.annotations.VisibleForTesting; + +import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; import org.sonar.api.batch.InstantiationStrategy; import org.sonar.api.batch.fs.internal.DefaultInputModule; @@ -228,9 +230,16 @@ public class ProjectScanContainer extends ComponentContainer { @Override protected void doAfterStart() { DefaultAnalysisMode analysisMode = getComponentByType(DefaultAnalysisMode.class); + InputModuleHierarchy tree = getComponentByType(InputModuleHierarchy.class); + analysisMode.printMode(); + LOG.info("Project key: {}", tree.root().key()); + String organization = props.property("sonar.organization"); + if (StringUtils.isNotEmpty(organization)) { + LOG.info("Organization key: {}", organization); + } + LOG.debug("Start recursive analysis of project modules"); - InputModuleHierarchy tree = getComponentByType(InputModuleHierarchy.class); scanRecursively(tree, tree.root()); if (analysisMode.isMediumTest()) { getComponentByType(ScanTaskObservers.class).notifyEndOfScanTask(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java index aa095aa5c2e..61b67475086 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java @@ -86,7 +86,7 @@ public class FileSystemMediumTest { tester.stop(); logs = new LogOutputRecorder(); } - + private ImmutableMap.Builder createBuilder() { return ImmutableMap.builder() .put("sonar.task", "scan") @@ -131,6 +131,45 @@ public class FileSystemMediumTest { assertThat(result.getReportComponent(file.key())).isNotNull(); } + @Test + public void logProjectKeyAndOrganizationKey() throws IOException { + builder = createBuilder(); + builder.put("sonar.organization", "my org"); + File srcDir = new File(baseDir, "src"); + srcDir.mkdir(); + + File xooFile = new File(srcDir, "sample.xoo"); + FileUtils.write(xooFile, "Sample xoo\ncontent"); + + tester.newTask() + .properties(builder + .put("sonar.sources", "src") + .build()) + .start(); + + assertThat(logs.getAllAsString()).contains("Project key: com.foo.project"); + assertThat(logs.getAllAsString()).contains("Organization key: my org"); + } + + @Test + public void dontLogInvalidOrganization() throws IOException { + builder = createBuilder(); + File srcDir = new File(baseDir, "src"); + srcDir.mkdir(); + + File xooFile = new File(srcDir, "sample.xoo"); + FileUtils.write(xooFile, "Sample xoo\ncontent"); + + tester.newTask() + .properties(builder + .put("sonar.sources", "src") + .build()) + .start(); + + assertThat(logs.getAllAsString()).contains("Project key: com.foo.project"); + assertThat(logs.getAllAsString()).doesNotContain("Organization key"); + } + @Test public void onlyGenerateMetadataIfNeeded() throws IOException { builder = createBuilder(); -- 2.39.5